/**
 * Multimud Helper.
 * 
 * Copyright (C) 2009 Benjamin Lerman<br>
 * Copyright (C) 2009 Jacques ???
 * 
 * This program 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 3 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.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package net.ambre.multimud_helper.config;

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

import org.apache.commons.configuration.CombinedConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * contains method used to configure the application.
 * 
 */
public class ConfigurationImpl implements Configuration {

    /**
     * the default name for the directory that will contain the database.
     */
    private static final String DEFAULT_DB_DIRECTORY_NAME = "db";

    /**
     * the logger to use.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationImpl.class);

    /**
     * the directory that will contain the user configuration.
     */
    static final String CONFIGURATION_DIRECTORY_NAME = ".multimud_helper";

    /**
     * the entry, in the configuration, for the location of the database.
     */
    static final String DATABASE_DIRECTORY = "database.directory";

    /**
     * the name of the configuration containing the mutable properties for the user.
     */
    static final String USER_PREFERENCES_PROPERTY_NAME = "userPreferences";

    /**
     * the singleton configuration for the user.
     */
    private volatile CombinedConfiguration configuration;

    /**
     * {@inheritDoc}
     */
    public File getDatabaseDirectory() throws ConfigurationException {
        String fileName = getConfiguration().getString(DATABASE_DIRECTORY);
        if (fileName == null) {
            try {
                final File defaultDatabaseDirectory = getDefaultDatabaseDirectory();
                fileName = defaultDatabaseDirectory.getCanonicalPath();
                setDatabaseDirectory(defaultDatabaseDirectory);
            } catch (IOException e) {
                LOGGER.warn("Unable to save user configuration.");
            }
        }
        File file = new File(fileName);
        file.getParentFile().mkdirs();
        return file;
    }

    /**
     * {@inheritDoc}
     */
    public String getJdbcUrl() throws ConfigurationException, IOException {
        return "jdbc:derby:" + getDatabaseDirectory().getCanonicalPath() + ";create=true";
    }

    /**
     * {@inheritDoc}
     */
    public void setDatabaseDirectory(File file) throws ConfigurationException, IOException {
        getMutableConfiguration().setProperty(DATABASE_DIRECTORY, getDefaultDatabaseDirectory().getCanonicalPath());
        saveConfiguration();
    }

    /**
     * save the user configuration to disk.
     * 
     * @throws ConfigurationException if an error occurred while trying to save the user's configuration.
     * 
     */
    private void saveConfiguration() throws ConfigurationException {
        getMutableConfiguration().save();

    }

    /**
     * flush the configuration. Should only be used by tests.
     */
    void flushConfiguration() {
        synchronized (ConfigurationImpl.class) {
            configuration = null;
        }
    }

    /**
     * retrieve the configuration of the user.
     * 
     * @return the configuration of the user.
     * @throws ConfigurationException if an error occurred while trying to retrieve the user's configuration.
     */
    CombinedConfiguration getConfiguration() throws ConfigurationException {
        if (configuration == null) {
            synchronized (ConfigurationImpl.class) {
                if (configuration == null) {
                    DefaultConfigurationBuilder configurationBuilder = new DefaultConfigurationBuilder(ConfigurationImpl.class
                                    .getResource("config.xml"));
                    configurationBuilder.clearErrorListeners();
                    // TODO add our own error listener.
                    configuration = (CombinedConfiguration) configurationBuilder.getConfiguration();
                }
            }
        }
        return configuration;
    }

    /**
     * return the directory that contain the database.
     * 
     * @return the directory that contain the database.
     */
    File getDefaultDatabaseDirectory() {
        return new File(new File(System.getProperty("user.home"), CONFIGURATION_DIRECTORY_NAME), DEFAULT_DB_DIRECTORY_NAME);
    }

    /**
     * returns a {@link PropertiesConfiguration} that can be modified and saved back to disk.
     * 
     * @return a {@link PropertiesConfiguration} that can be modified and saved back to disk.
     * @throws ConfigurationException if an error occurred while trying to retrieve the user's configuration.
     */
    PropertiesConfiguration getMutableConfiguration() throws ConfigurationException {
        return ((PropertiesConfiguration) (getConfiguration()).getConfiguration(USER_PREFERENCES_PROPERTY_NAME));
    }

}
