package seedu.taskell.storage;

import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Logger;

import seedu.taskell.commons.core.Config;
import seedu.taskell.commons.core.LogsCenter;
import seedu.taskell.commons.exceptions.DataConversionException;
import seedu.taskell.commons.util.FileUtil;

/**
 * A class to access Config stored in the hard disk as a json file
 */

public class JsonConfigStorage implements ConfigStorage {

    private static final Logger logger = LogsCenter.getLogger(JsonConfigStorage.class);
    
    private String filePath;
    
    public JsonConfigStorage(String filePath){
        this.filePath = filePath;
    }
    
    @Override
    public Optional<Config> readConfigFile() throws DataConversionException, IOException {
        return readConfig(filePath);
    }

    @Override
    public void saveConfigFile(Config config) throws IOException {
        saveConfig(config, filePath);
    }
    
    /**
     * Similar to {@link #readConfigFile()}
     * @param configFilePath location of the data. Cannot be null.
     * @throws DataConversionException if the file format is not as expected.
     */
    public Optional<Config> readConfig(String configFilePath) throws DataConversionException {
        assert configFilePath != null;

        File prefsFile = new File(configFilePath);

        if (!prefsFile.exists()) {
            logger.info("Prefs file "  + prefsFile + " not found");
            return Optional.empty();
        }

        Config config;

        try {
            config = FileUtil.deserializeObjectFromJsonFile(prefsFile, Config.class);
        } catch (IOException e) {
            logger.warning("Error reading from prefs file " + prefsFile + ": " + e);
            throw new DataConversionException(e);
        }

        return Optional.of(config);
    }

    /**
     * Similar to {@link #saveConfigFile(UserPrefs)}
     * @param configFilePath location of the data. Cannot be null.
     */
    private void saveConfig(Config config, String configFilePath) throws IOException {
        assert config != null;
        assert configFilePath != null;

        FileUtil.serializeObjectToJsonFile(new File(configFilePath), config);
    }

}

@@ -1319,21 +1319,22 @@
    File 
-prefs
+config
 File = n
@@ -1372,21 +1372,22 @@
    if (!
-prefs
+config
 File.exi
@@ -1420,21 +1420,22 @@
 r.info(%22
-Prefs
+Config
  file %22 
@@ -1437,21 +1437,22 @@
 le %22  + 
-prefs
+config
 File + %22
@@ -1611,21 +1611,22 @@
 sonFile(
-prefs
+config
 File, Co
@@ -1719,21 +1719,22 @@
 ng from 
-prefs
+config
  file %22 
@@ -1739,13 +1739,14 @@
 %22 + 
-prefs
+config
 File
@@ -1916,17 +1916,14 @@
 ile(
-UserPrefs
+Config
 )%7D%0A 

edit userprefs references to config file