
import java.util.logging.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.io.PrintWriter;


class SearchSettings
{
   private static final Logger LOGGER = Logger.getLogger("SearchSettings");

   public static class DefaultParamValues
   {
      String responseFormat;
      String skySystem;
      String specSystem;
      boolean showDuration;
   }


/*
   public static class DBConn
   {
      private String uri;
      private String schema;
      private String user_name;
      private String password;

      public String uri() {return uri;}
      public String schema() {return schema;}
      public String userName() {return user_name;}
      public String password() {return password;}

      public String toString()
      {
         return uri + "  schema[" + schema +  "] " + user_name + " / " + password;
      }
   }
*/
    public DBConn     dbConn;
    public DefaultParamValues defaults;

   // will not start without config-file; no reasonable code-defaults can be invented
   public static SearchSettings getInstance(String settingsFileName)
   {
      try
      {
         InputStream ins = SearchSettings.class.getClassLoader().getResourceAsStream(settingsFileName);

         if (ins != null)
         {
            Properties properties = new Properties();
            properties.load(ins);

            DBConn      dbConn      = loadDBConn(properties);
            DefaultParamValues defaults = loadDefaults(properties);

            return new SearchSettings(dbConn, defaults);
         }
         else
         {
            throw new IllegalStateException(settingsFileName + " not found in classpath");
         }

      }
      catch(IOException ex)
      {
         throw new IllegalStateException("Error while loading " + settingsFileName + " file", ex);
      }
   }




   private SearchSettings(DBConn dbConn,  DefaultParamValues defaults)
   {
      this.dbConn      = dbConn;
      this.defaults  = defaults;
   }


   private static DBConn loadDBConn(Properties properties)
   {
      DBConn dbConn = new DBConn();
      dbConn.uri       = properties.getProperty("db_uri","jdbc:postgresql://localhost:5432/vialactea").strip();
      dbConn.schema    = properties.getProperty("db_schema","datasets").strip();
      dbConn.user_name = properties.getProperty("db_user_name","").strip();
      dbConn.password  = properties.getProperty("db_password","").strip();

      return dbConn;
   }

   private static DefaultParamValues loadDefaults(Properties properties)
   {
      DefaultParamValues defaults = new DefaultParamValues();
      defaults.responseFormat = properties.getProperty("default_response_format", "application/fits").strip();
      defaults.skySystem      = properties.getProperty("default_sky_system", "ICRS").strip();
      defaults.specSystem     = properties.getProperty("default_spec_system", "WAVE_Barycentric").strip();
      defaults.showDuration   = "yes".equals(properties.getProperty("show_duration", "no").strip());
      return defaults;
   }

}

