/* $Id: KernelContext.java,v 1.12 2007/05/10 00:37:50 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2007,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

package org.qedeq.kernel.context;

import java.io.File;
import java.io.IOException;

import org.qedeq.kernel.base.module.Specification;
import org.qedeq.kernel.bo.module.Kernel;
import org.qedeq.kernel.bo.module.KernelState;
import org.qedeq.kernel.bo.module.ModuleAddress;
import org.qedeq.kernel.bo.module.ModuleFactory;
import org.qedeq.kernel.bo.module.ModuleProperties;
import org.qedeq.kernel.bo.module.QedeqBo;
import org.qedeq.kernel.common.XmlFileExceptionList;
import org.qedeq.kernel.config.QedeqConfig;
import org.qedeq.kernel.log.QedeqLog;
import org.qedeq.kernel.trace.Trace;
import org.qedeq.kernel.utility.IoUtility;


/**
 * This class provides static access methods for basic informations.
 *
 * @version $Revision: 1.12 $
 * @author  Michael Meyling
 */
public final class KernelContext implements Kernel {

    /** Version of this kernel. */
    private static final String KERNEL_VERSION = "0.03.04";

    /** Version dependent directory of this kernel. */
    private static final String KERNEL_VERSION_DIRECTORY = KERNEL_VERSION.replace('.', '_');

    /** Descriptive version information of this kernel. */
    private static final String DESCRIPTIVE_KERNEL_VERSION
        = "Hilbert II - Version " + KERNEL_VERSION + " (mongaga)";

    /** Maximal supported rule version of this kernel. */
    private static final String MAXIMAL_RULE_VERSION = "1.00.00";

    /** One and only instance of this class. */
    private static final KernelContext INSTANCE = new KernelContext();

    /** Initial kernel state. */
    private final KernelState initialState = new KernelState() {

        public void startup(final ModuleFactory moduleFactory) throws IOException {
            QedeqLog.getInstance().logMessage("This is "
                + KernelContext.getInstance().getDescriptiveKernelVersion());
            QedeqLog.getInstance().logMessage("  see \"http://www.qedeq.org\" for more "
                + "information");
            QedeqLog.getInstance().logMessage("  supports rules till version "
                + KernelContext.getInstance().getMaximalRuleVersion());
            config = new QedeqConfig(
                new File(IoUtility.getStartDirectory("qedeq"), "config"),
                "org.qedeq.properties",
                "This file is part of the project *Hilbert II* - http://www.qedeq.org");
            QedeqLog.getInstance().logMessage("QEDEQ kernel opened.");
            KernelContext.this.moduleFactory = moduleFactory;
            currentState = readyState;
            moduleFactory.init();
        }

        public boolean isReady() {
            return false;
        }

        public QedeqConfig getConfig() {
            throw new IllegalStateException("Kernel not initialized");
        }

        public void shutdown() {
        }

        public void init() {
            throw new IllegalStateException("Kernel not initialized");
        }

        public void removeAllModules() {
            throw new IllegalStateException("Kernel not initialized");
        }

        public void clearLocalBuffer() throws IOException {
            throw new IllegalStateException("Kernel not initialized");
        }

        public QedeqBo loadModule(final String address) throws XmlFileExceptionList {
            throw new IllegalStateException("Kernel not initialized");
        }

        public QedeqBo loadModule(final ModuleAddress moduleAddress) throws XmlFileExceptionList {
            throw new IllegalStateException("Kernel not initialized");
        }

        public QedeqBo loadModule(final QedeqBo module, final Specification spec)
                throws XmlFileExceptionList {
            throw new IllegalStateException("Kernel not initialized");
        }

        public boolean loadAllModulesFromQedeq() {
            throw new IllegalStateException("Kernel not initialized");
        }

        public String[] getAllLoadedModules() {
            throw new IllegalStateException("Kernel not initialized");
        }

        public String getBufferDirectory() {
            throw new IllegalStateException("Kernel not initialized");
        }

        public String getGenerationDirectory() {
            throw new IllegalStateException("Kernel not initialized");
        }

        public ModuleProperties getModuleProperties(final String address) {
            throw new IllegalStateException("Kernel not initialized");
        }

    };

    /** State for ready kernel. */
    private final KernelState readyState = new KernelState() {

        public void startup(final ModuleFactory moduleFactory) throws IOException {
            // we are already ready
        }

        public boolean isReady() {
            return false;
        }

        public QedeqConfig getConfig() {
            return config;
        }

        public void shutdown() {
            try {
                config.setLoadedModules(moduleFactory.getAllLoadedModules());
                config.store();
                config = null;
                QedeqLog.getInstance().logMessage("Current config file successfully saved.");
            } catch (IOException e) {
                Trace.trace(this, "shutdown()", e);
                QedeqLog.getInstance().logMessage("Saving current config file failed.");
            }
            currentState = initialState;
            QedeqLog.getInstance().logMessage("QEDEQ Kernel closed.");
        }

        public void init() {
            throw new IllegalStateException("Kernel is already initialized");
        }

        public void removeAllModules() {
            moduleFactory.removeAllModules();
        }

        public void clearLocalBuffer() throws IOException {
            moduleFactory.clearLocalBuffer();
        }

        public QedeqBo loadModule(final String address) throws XmlFileExceptionList {
            return moduleFactory.loadModule(address);
        }

        public QedeqBo loadModule(final ModuleAddress moduleAddress) throws XmlFileExceptionList {
            return moduleFactory.loadModule(moduleAddress);
        }

        public QedeqBo loadModule(final QedeqBo module, final Specification spec)
                throws XmlFileExceptionList {
            return moduleFactory.loadModule(module, spec);
        }

        public boolean loadAllModulesFromQedeq() {
            return moduleFactory.loadAllModulesFromQedeq();
        }

        public String[] getAllLoadedModules() {
            return moduleFactory.getAllLoadedModules();
        }

        public String getBufferDirectory() {
            return moduleFactory.getBufferDirectory();
        }

        public String getGenerationDirectory() {
            return moduleFactory.getGenerationDirectory();
        }

        public ModuleProperties getModuleProperties(final String address) {
            return moduleFactory.getModuleProperties(address);
        }

    };

    /** Initial kernel state. */
    private KernelState currentState = initialState;

    /** For config access. */
    private QedeqConfig config;

    /** This factory can produce QEDEQ modules. */
    private ModuleFactory moduleFactory;

    /**
     * Constructor.
     */
    private KernelContext() {
        // nothing to do
    }

    /**
     * Get instance of kernel context.
     *
     * @return  Singleton, which is responsible for the kernel access.
     */
    public static final KernelContext getInstance() {
        return INSTANCE;
    }

    /**
     * Get version of this kernel.
     *
     * @return  Kernel version.
     */
    public final String getKernelVersion() {
        return KERNEL_VERSION;
    }

    /**
     * Get relative version directory of this kernel.
     *
     * @return  Version sub directory.
     */
    public final String getKernelVersionDirectory() {
        return KERNEL_VERSION_DIRECTORY;
    }

    /**
     * Get descriptive version information of this kernel.
     *
     * @return  Version Information.
     */
    public final String getDescriptiveKernelVersion() {
        return DESCRIPTIVE_KERNEL_VERSION;
    }

    /**
     * Get maximal supported rule version of this kernel.
     *
     * @return  Maximal supported rule version.
     */
    public final String getMaximalRuleVersion() {
        return MAXIMAL_RULE_VERSION;
    }

    /**
     * Is a given rule version supported?
     *
     * @param   ruleVersion Check this one.
     * @return  Is the given rule version supported?
     */
    public final boolean isRuleVersionSupported(final String ruleVersion) {
        return MAXIMAL_RULE_VERSION.equals(ruleVersion);
    }

    public void startup(final ModuleFactory moduleFactory) throws IOException {
        currentState.startup(moduleFactory);
    }

    public boolean isReady() {
        return currentState.isReady();
    }

    public QedeqConfig getConfig() {
        return currentState.getConfig();
    }

    public void shutdown() {
        currentState.shutdown();
    }

    public void init() {
        currentState.init();
    }

    public void removeAllModules() {
        currentState.removeAllModules();
    }

    public void clearLocalBuffer() throws IOException {
        currentState.clearLocalBuffer();
    }

    public QedeqBo loadModule(final String address) throws XmlFileExceptionList {
        return currentState.loadModule(address);
    }

    public QedeqBo loadModule(final ModuleAddress moduleAddress) throws XmlFileExceptionList {
        return currentState.loadModule(moduleAddress);
    }

    public QedeqBo loadModule(final QedeqBo module, final Specification spec)
            throws XmlFileExceptionList {
        return currentState.loadModule(module, spec);
    }

    public boolean loadAllModulesFromQedeq() {
        return currentState.loadAllModulesFromQedeq();
    }

    public String[] getAllLoadedModules() {
        return currentState.getAllLoadedModules();
    }

    public String getBufferDirectory() {
        return currentState.getBufferDirectory();
    }

    public String getGenerationDirectory() {
        return currentState.getGenerationDirectory();
    }

    public ModuleProperties getModuleProperties(final String address) {
        return currentState.getModuleProperties(address);
    }

}
