/* $Id: QedeqConfig.java,v 1.2 2007/05/10 00:37:53 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.config;

import java.io.File;
import java.io.IOException;
import java.util.List;


/**
 * This class gives a type save access to properties of the application.
 *
 * FIXME mime 20070328: for directory files are different methods used:
 *   1. look relative to getStartDirectory
 *   2. try absolute path
 *   Only one way should be used for all.
 *
 *
 * @version $Revision: 1.2 $
 * @author  Michael Meyling
 */
public final class QedeqConfig {

    /** Default location for newly created QEDEQ modules. */
    private static final String DEFAULT_LOCAL_MODULES_DIRECTORY
        = "local";

    /** Default location for locally buffered module files. */
    private static final String DEFAULT_LOCAL_BUFFER
        = "buffer";

    /** Default location for generated module and document files. */
    private static final String DEFAULT_GENERATED
        = "generated";

    /** Default log file path. */
    private static final String DEFAULT_LOG_FILE
        = "log/log.txt";

    /** This class organizes the access to the config parameters. */
    private final ConfigAccess configAccess;

    /**
     * Constructor.
     *
     * @param   configDirectory         Directory location of config file.
     * @param   configFileName          Name of config file.
     * @param   description             Config file description
     * @throws  IOException             Config file couldn't be loaded.
     */
    public QedeqConfig(final File configDirectory, final String configFileName,
            final String description) throws IOException {
        configAccess = new ConfigAccess(configDirectory, configFileName, description);
    }

    /**
     * Store properties in config file.
     *
     * @throws  IOException
     */
    public final void store() throws IOException {
        configAccess.store();
    }

    /**
     * Get local file directory to save generated files in.
     *
     * @return  Generation directory.
     */
    public final String getGenerationDirectory() {
        final String location = configAccess.getString("generationLocation");
        if (location == null) {
            return QedeqConfig.DEFAULT_GENERATED;
        }
        return location;
    }

    /**
     * Set local file directory for generated files.
     *
     * @param   location    generation directory.
     */
    public final void setGenerationDirectory(final String location) {
        configAccess.setString("generationLocation", location);
    }

    /**
     * Get local file directory for module buffering.
     *
     * @return  Buffer directory.
     */
    public final String getBufferDirectory() {
        final String location = configAccess.getString("bufferLocation");
        if (location == null) {
            return QedeqConfig.DEFAULT_LOCAL_BUFFER;
        }
        return location;
    }


    /**
     * Set local file directory for module buffering.
     * After changing this location the buffer should eventually be cleared.
     *
     * @param   location    buffer directory.
     */
    public final void setBufferDirectory(final String location) {
        configAccess.setString("bufferLocation", location);
    }

    /**
     * Get local file location for log file.
     *
     * @return  Log file path.
     */
    public final String getLogFilePath() {
        final String location = configAccess.getString("logLocation");
        if (location == null) {
            return QedeqConfig.DEFAULT_LOG_FILE;
        }
        return location;
    }

    /**
     * Get history of modules, which were tried to load.
     *
     * @return  list of modules.
     */
    public final String[] getModuleHistory() {
        return configAccess.getStringProperties("moduleHistory.");
    }

    /**
     * Save history of modules, which were tried to load.
     *
     * @param  modules  list of modules.
     */
    public final void saveModuleHistory(final List modules) {
        configAccess.removeProperties(("moduleHistory."));
        for (int i = 0; i < modules.size(); i++) {
            configAccess.setString("moduleHistory." + (i + 101),
            modules.get(i).toString());
        }
    }

    /**
     * Get list of previously checked modules.
     *
     * @return  list of modules.
     */
    public final String[] getPreviouslyCheckedModules() {
        return configAccess.getStringProperties("checkedModule.");
    }

    /**
     * Set successfully list of successfully loaded QEDEQ modules.
     *
     * @param   moduleAddresses     This modules were successfully checked.
     */
    public final void setLoadedModules(final String[] moduleAddresses) {
        configAccess.removeProperties("checkedModule.");
        for (int i = 0; i < moduleAddresses.length; i++) {
            configAccess.setString("checkedModule." + (i + 1), moduleAddresses[i]);
        }
    }

    /**
     * Get start directory of this application.
     *
     * TODO mime 20070326: ok?
     *
     * @return  Start directory.
     */
    public final File getStartDirectory() {
        return new File(".");
    }

    /**
     * Get autostart html mode.
     *
     * @return  list of modules.
     */
    public final boolean isAutoStartHtmlBrowser() {
        return "true".equals(
            configAccess.getString("autoStartHtmlBrowser", "true"));
    }


    /**
     * Set auto start HTML browser.
     *
     * @param  mode Auto start?
     */
    public final void setAutoStartHtmlBrowser(final boolean mode) {
        configAccess.setString("autoStartHtmlBrowser", (mode ? "true" : "false"));
    }


    /**
     * Get direct response mode.
     *
     * @return  Direct response mode.
     */
    public final boolean isDirectResponse() {
        return "true".equals(
            configAccess.getString("directResponse", "true"));
    }


    /**
     * Set direct response mode.
     *
     * @param  mode     enable direct response?
     */
    public final void setDirectResponse(final boolean mode) {
        configAccess.setString("directResponse", (mode ? "true" : "false"));
    }

    /**
     * Get auto reload of last session successfully loaded modules.
     *
     * @return auto reload enabled?
     */
    public boolean isAutoReloadLastSessionChecked() {
        return "true".equals(
            configAccess.getString("sessionAutoReload", "true"));
    }


    /**
     * Set auto reload checked modules of last session mode.
     *
     * @param  mode     enable auto reload?
     */
    public final void setAutoReloadLastSessionChecked(final boolean mode) {
        configAccess.setString("sessionAutoReload", (mode ? "true" : "false"));
    }


    /**
     * Should old html code be generated?
     *
     * @return  old html code?
     */
    public final boolean isOldHtml() {
        return "true".equals(configAccess.getString("oldHtml", "true"));
    }

    /**
     * Set old html code generation flag.
     *
     * @param  mode     set old html code generation?
     */
    public final void setOldHtml(final boolean mode) {
        configAccess.setString("oldHtml", (mode ? "true" : "false"));
    }


    /**
     * Get directory for newly created QEDEQ module files.
     *
     * @return  Directory for newly created QEDEQ modules.
     */
    public final String getLocalModulesDirectory() {
        final String location = configAccess.getString("localModulesDirectory");
        if (location == null) {
            return QedeqConfig.DEFAULT_LOCAL_MODULES_DIRECTORY;
        }
        return location;
    }


    /**
     * Set directory for newly created module files.
     * After changing this location the buffer should eventually be cleared.
     *
     * @param   location    Buffer directory.
     */
    public final void setLocalModulesDirectory(final String location) {
        configAccess.setString("localModulesDirectory", location);
    }


}
