package com.groupon.seleniumgridextras.config;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import com.groupon.seleniumgridextras.utilities.FileIOUtility;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ConfigFileReader {

  private String filePath;
  private String configString = "";
  private Map parsedConfig;


  private static Logger logger = Logger.getLogger(ConfigFileReader.class);

  public ConfigFileReader(String file) {
    this.filePath = file;
    readConfigFile();

  }

  public boolean hasContent() {
    if (this.configString.equals("")) {
      return false;
    } else {
      return true;
    }
  }

  public Map toHashMap() {
    if (hasContent()) {
      return this.parsedConfig;
    } else {
      return new HashMap();
    }
  }

  public void readConfigFile() {
    //Reason to use the "" instead of checking if the config already exists is because
    //if the file does exist but there is any formatting error, or parsing error, etc..
    //We assume file does not exist and overwrite it with good configs.
    String readString = "";
    try {
      readString = FileIOUtility.getAsString(this.filePath);
    } catch (FileNotFoundException error) {
      logger.info("File " + this.filePath + " does not exist, going to use default configs");
    } catch (IOException error) {
      logger.info("Error reading" + this.filePath + ". Going with default configs");
    } catch (Exception error){
      error.printStackTrace();
      logger.warn(error.getMessage());
    }

    this.configString = readString;
    this.parsedConfig = new Gson().fromJson(this.configString, HashMap.class);
  }

  public void overwriteExistingConfig(Map outputMap) {
    try {
      File f = new File(filePath);
      FileUtils.writeStringToFile(f, new GsonBuilder().setPrettyPrinting().create()
          .toJson(outputMap));

      readConfigFile();
    } catch (Exception error) {
      logger.fatal(error.toString());
      System.exit(1);
    }
  }


}

@@ -308,36 +308,8 @@
 on;%0A
-import java.io.IOException;%0A
 impo
@@ -1447,126 +1447,8 @@
   %7D 
-catch (IOException error) %7B%0A      logger.info(%22Error reading%22 + this.filePath + %22. Going with default configs%22);%0A    %7D
  cat

delete unused catch