package net.egemsoft.metric.commons;

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

import java.io.IOException;
import java.util.Arrays;

/**
 * Api settings holder
 *
 * @author Cenk Bircanoglu
 * @version 0.2.0 12/03/15.
 */
public class ApiSettings {
  static Logger logger = LoggerFactory.getLogger(ApiSettings.class);
  public static String configFile = "api.properties";

  public static String ES_HOSTS;
  public static int ES_PORT;
  public static String ES_CLUSTER_NAME;
  public static String ES_INDICE_NAME;
  public static String ES_INDICE_TYPE;
  public static int ES_FETCH_SIZE;
  public static String[] CASSANDRA_HOSTS;
  public static int CASSANDRA_PORT;
  public static String CASSANDRA_KEYSPACE;
  public static String CASSANDRA_TABLE;
  public static String CASSANDRA_USER;
  public static String CASSANDRA_PASSWORD;
  public static String[] METRIC_ROLLUPS;
  public static String CASSANDRA_CLUSTER_NAME;


  static {
    try {
      loadSettings();
    } catch (IOException e) {
      logger.error("Upppsss!!! IO error spawned while reading settings. " + e.getMessage());
      e.printStackTrace();
    } catch (ConfigurationException e) {
      logger.error("Upppsss!!! Configuration error spawned while reading settings. " + e.getMessage());
      e.printStackTrace();
    }
  }

  /**
   * Access the settings file, read all lines and initialize to variables of this class.
   *
   * @throws java.io.IOException
   * @throws org.apache.commons.configuration.ConfigurationException
   */
  private static void loadSettings() throws IOException, ConfigurationException {
    logger.debug("Loading settings from " + configFile);

    PropertiesConfiguration config = new PropertiesConfiguration();
    try {
      config.load(configFile);
    } catch (Exception e) {
      config.save(configFile);
    }

    CASSANDRA_HOSTS = config.getStringArray("cassandra.hosts");
    logger.debug("Cassandra host set as " + Arrays.toString(CASSANDRA_HOSTS));

    CASSANDRA_PORT = config.getInt("cassandra.port", 9042);
    logger.debug("Cassandra port set as " + CASSANDRA_PORT);

    CASSANDRA_KEYSPACE = config.getString("cassandra.keyspace", "metrics");
    logger.debug("Cassandra keyspace set as " + CASSANDRA_KEYSPACE);

    CASSANDRA_TABLE = config.getString("cassandra.table", "metric");
    logger.debug("Cassandra table set as " + CASSANDRA_TABLE);

    CASSANDRA_USER = config.getString("cassandra.auth.username", null);
    logger.debug("Cassandra authentication name set as " + CASSANDRA_USER);

    CASSANDRA_PASSWORD = config.getString("cassandra.auth.password", null);
    logger.debug("Cassandra authentication password set as " + CASSANDRA_PASSWORD);

    CASSANDRA_CLUSTER_NAME = config.getString("cassandra.cluster.name", null);
    logger.debug("Cassandra cluster name set as " + CASSANDRA_CLUSTER_NAME);

    ES_HOSTS = config.getString("es.hosts", "localhost");
    logger.debug("Elastic host set as " + ES_HOSTS);

    ES_PORT = config.getInt("es.port", 9300);
    logger.debug("Elastic port set as " + ES_PORT);

    ES_CLUSTER_NAME = config.getString("es.cluster.name", "local");
    logger.debug("Elastic cluster name set as " + ES_CLUSTER_NAME);

    ES_INDICE_NAME = config.getString("es.indice.name", "metrics");
    logger.debug("Name of elastic indice set as " + ES_INDICE_NAME);

    ES_INDICE_TYPE = config.getString("es.indice.type", "local");
    logger.debug("Type of elastic indice set as " + ES_INDICE_TYPE);

    METRIC_ROLLUPS = config.getStringArray("metric.rollups");
    logger.debug("Metric rollups set as " + Arrays.toString(METRIC_ROLLUPS));

    ES_FETCH_SIZE = config.getInt("es.fetch.size", 10);
    logger.debug("Elastic fetch size set as " + ES_FETCH_SIZE);


  }


  /**
   * Access the settings file and change value of the one property or add new property.
   *
   * @param propertyName key of property
   * @param property     value of property
   * @throws org.apache.commons.configuration.ConfigurationException
   */
  public static void setProperty(String propertyName, Object property) throws ConfigurationException, IOException {
    logger.debug("Set new property to file named " + configFile);
    logger.debug("Property Name: " + propertyName + " Value: " + property);
    PropertiesConfiguration config = new PropertiesConfiguration();
    config.load(configFile);
    config.setProperty(propertyName, property);
    config.save(configFile);
    loadSettings();
    logger.debug("Property set");
  }

  /**
   * Try to load settings file and if not exists going to be create one.
   *
   * @throws org.apache.commons.configuration.ConfigurationException
   * @throws java.io.IOException
   */
  public static void createPropertyFile() throws ConfigurationException, IOException {
    try {
      loadSettings();
    } catch (Exception e) {
      logger.error("The settings named " + configFile + " do not exists. Going to be created now.");
      PropertiesConfiguration config = new PropertiesConfiguration();
      config.save(configFile);
    }

  }


}
