package tars.commons.util;

import tars.commons.core.Config;
import tars.commons.core.LogsCenter;
import tars.commons.exceptions.DataConversionException;

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

/**
 * A class for accessing the Config File.
 */
public class ConfigUtil {

    private static final Logger logger = LogsCenter.getLogger(ConfigUtil.class);

    /**
     * Returns the Config object from the given file or {@code Optional.empty()} object if the file is not found.
     *   If any values are missing from the file, default values will be used, as long as the file is a valid json file.
     * @param configFilePath cannot be null.
     * @throws DataConversionException if the file format is not as expected.
     */
    public static Optional<Config> readConfig(String configFilePath) throws DataConversionException {

        assert configFilePath != null;

        File configFile = new File(configFilePath);

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

        Config config;

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

        return Optional.of(config);
    }

    /**
     * Saves the Config object to the specified file.
     *   Overwrites existing file if it exists, creates a new file if it doesn't.
     * @param config cannot be null
     * @param configFilePath cannot be null
     * @throws IOException if there was an error during writing to the file
     */
    public static void saveConfig(Config config, String configFilePath) throws IOException {
        assert config != null;
        assert configFilePath != null;

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

}

@@ -91,16 +91,146 @@
 Center;%0A
+import tars.commons.events.storage.DataSavingExceptionEvent;%0Aimport tars.commons.events.storage.TarsStorageDirectoryChangedEvent;%0A
 import t
@@ -388,16 +388,62 @@
 ogger;%0A%0A
+import com.google.common.eventbus.Subscribe;%0A%0A
 /**%0A * A
@@ -591,16 +591,80 @@
 .class);
+%0A    private static final String configFilePath = %22config.json%22;
 %0A%0A    /*
@@ -745,16 +745,23 @@
 empty()%7D
+%0A     *
  object 
@@ -785,25 +785,16 @@
 t found.
-%0A     *  
  If any 
@@ -826,16 +826,23 @@
 he file,
+%0A     *
  default
@@ -901,24 +901,32 @@
  json file.%0A
+     * %0A
      * @para
@@ -933,32 +933,50 @@
 m configFilePath
+%0A     *           
  cannot be null.
@@ -1010,24 +1010,43 @@
 ionException
+%0A     *            
  if the file
@@ -1351,17 +1351,16 @@
 g file %22
- 
  + confi
@@ -1841,25 +1841,16 @@
 ed file.
-%0A     *  
  Overwri
@@ -1862,24 +1862,31 @@
 xisting file
+%0A     *
  if it exist
@@ -1922,16 +1922,24 @@
 oesn't.%0A
+     * %0A
      * @
@@ -1950,16 +1950,34 @@
 m config
+%0A     *           
  cannot 
@@ -2012,16 +2012,34 @@
 FilePath
+%0A     *           
  cannot 
@@ -2072,16 +2072,35 @@
 xception
+%0A     *            
  if ther
@@ -2396,12 +2396,415 @@
 );%0A    %7D
+%0A    %0A    private void updateConfig(Config newConfig) throws IOException %7B%0A        saveConfig(newConfig, configFilePath);%0A    %7D%0A%0A    // @@author A0124333U%0A    @Subscribe%0A    private void handleTarsStorageDirectoryChangedEvent(TarsStorageDirectoryChangedEvent event) throws IOException %7B%0A        logger.info(LogsCenter.getEventHandlingLogMessage(event));%0A        updateConfig(event.getNewConfig());%0A    %7D
 %0A%0A%7D%0A

Add ConfigUtil handler for TarsStorageDirectoryChangedEvent