package org.bff.javampd.server;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.bff.javampd.MPDException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * I handle loading the properties from the properties file
 *
 * @author bill
 */
public abstract class MPDProperties implements PropertyLoader {
  private static final Logger LOGGER = LoggerFactory.getLogger(MPDProperties.class);

  private final Properties prop;

  private static final String PROP_FILE = "/mpd.properties";
  private static final String PROP_OVERRIDE_FILE = "/javampd.properties";

  protected MPDProperties() {
    prop = new Properties();
    loadValues(PROP_FILE);
    loadOverrideValues();
  }

  @Override
  public String getPropertyString(String property) {
    return prop.getProperty(property);
  }

  protected void loadValues(String propertiesResourceLocation) {
    try (InputStream is = MPDProperties.class.getResourceAsStream(propertiesResourceLocation)) {
      loadProperties(is);
    } catch (NullPointerException | IOException e) {
      throw new MPDException("Could not load mpd properties", e);
    }
  }

  protected void loadOverrideValues() {
    try (InputStream is =
        MPDProperties.class.getResourceAsStream(MPDProperties.PROP_OVERRIDE_FILE)) {
      loadProperties(is);
    } catch (NullPointerException | IOException e) {
      LOGGER.info("Override properties file not on classpath");
    }
  }

  protected void loadProperties(InputStream inputStream) throws IOException {
    prop.load(inputStream);
  }
}
