
import java.util.logging.Logger;

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


class Settings
{
   private static final Logger LOGGER = Logger.getLogger(Settings.class.getName());

   static final String CUTOUT_PROPERTIES = "cutout.properties";


   public static class FITSPaths
   {
      private String FITSpath;
      private String FITScutpath;
      private String FITSRemoteUrlCutouts;
      private String surveysMetadataAbsPathname;

      public String surveys() {return FITSpath;};
      public String cutouts() {return FITScutpath;};
      public String cutoutsUrl() {return FITSRemoteUrlCutouts;};
      public String surveysMetadataAbsPathname() {return surveysMetadataAbsPathname;};

      public String toString()
      {
         return FITSpath + " " + FITScutpath + " " + FITSRemoteUrlCutouts + " " + surveysMetadataAbsPathname;
      }
   }


   // DEPRECATED used only in Resolver / resolveByDb
   public static class DBConn
   {
      private String uri;
      private String user_name;
      private String password;

      public String toString()
      {
         return uri() + " " + user_name;
      }

      public boolean isDbUriEmpty()
      {
         return ((uri == null) || uri.trim().isEmpty());
      }

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


   public FITSPaths  fitsPaths;
   public DBConn     dbConn;


   // will not start without config-file
   // no reasonable code-defaults can be invented
   public static Settings getInstance()
   {
      LOGGER.config("Settings loading from: " + CUTOUT_PROPERTIES);
      try
      {
         InputStream ins =
            Settings.class.getClassLoader().getResourceAsStream(CUTOUT_PROPERTIES);

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

            FITSPaths fitsPaths = loadFITSPaths(properties);
            DBConn    dbConn    = loadDBConn(properties);

            return new Settings(dbConn, fitsPaths);
         }
         else
         {
            throw new IllegalStateException(CUTOUT_PROPERTIES + " not found in classpath");
         }

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



   private Settings(DBConn dbConn, FITSPaths fitsPaths)
   {
      this.fitsPaths = fitsPaths;
      this.dbConn    = dbConn;
   }



   private static FITSPaths loadFITSPaths(Properties properties)
   {
      FITSPaths fitspaths = new FITSPaths();
      fitspaths.FITSpath             = properties.getProperty("fits_path_surveys", "/srv/datasets").strip();
      fitspaths.FITScutpath          = properties.getProperty("fits_path_cutouts", "/srv/cutouts").strip();
      fitspaths.FITSRemoteUrlCutouts = properties.getProperty("fits_url_cutouts",  "").strip();
      fitspaths.surveysMetadataAbsPathname = properties.getProperty("surveys_metadata_abs_pathname", "").strip();
      return fitspaths;
   }

   private static DBConn loadDBConn(Properties properties)
   {
      DBConn dbconn = new Settings.DBConn();
      dbconn.uri       = properties.getProperty("db_uri", "").strip();
      dbconn.user_name = properties.getProperty("db_user_name", "").strip();
      dbconn.password  = properties.getProperty("db_password", "").strip();
      return dbconn;
   }

}

