package tv.pluto.utils;

import com.plutotv.test.lib.LogUtils;
import java.io.IOException;
import java.util.Properties;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class PropertiesHolder {
  private Properties properties;
  private static PropertiesHolder propertiesHolder;

  private static synchronized PropertiesHolder getInstance() {
    if (propertiesHolder == null) {
      propertiesHolder = new PropertiesHolder();

      try {
        propertiesHolder.properties =
            PropertiesLoaderUtils.loadAllProperties("projectconfig.properties");
      } catch (IOException e) {
        LogUtils.logSevereMessage("Cannot load 'config.properties' property file.");
      }
    }

    return propertiesHolder;
  }

  public static String getProperty(String property) {
    return getInstance().properties.getProperty(property);
  }

  public static Properties setPropertiesPath(String path) {
    try {
      LogUtils.logInfoMessage("Loaded Properties from " + path);

      return PropertiesLoaderUtils.loadAllProperties(path);

    } catch (IOException e) {
      LogUtils.logSevereMessage(
          "Cannot load property file. " + path + " incorrect or missing from your project.");
      throw new NullPointerException("File not found exception");
    }
  }
}
