package kz.cafe.constants;


import java.io.*;
import java.net.*;
import java.util.*;
import kz.cafe.exceptions.*;

public class ServerConstants{

   private static final String SERVER_CONFIG_PATH = "server.properties";

   private static final Properties properties;


//   private static final String sEmailTemplate;

   static{
      properties = new Properties();
      URL url = ServerConstants.class.getResource("/" + SERVER_CONFIG_PATH);

      System.out.println("Loading the property server file at the path " + url);

      InputStream fileProperties = null;
      try{
         fileProperties = ServerConstants.class.getResourceAsStream("/" + SERVER_CONFIG_PATH);
         properties.load(fileProperties);
      }
      catch (Exception e){
         throw new RuntimeException("Can not open the properties file: " + fileProperties);
      }



//      InputStream fileEmailTemplate = null;
//      try{
//         fileEmailTemplate = ServerConstants.class.getResourceAsStream("/email.template");
//         Reader reader = new BufferedReader(new InputStreamReader(fileEmailTemplate, "UTF-8"));
//         StringWriter writer = new StringWriter();
//
//         char[] buffer = new char[1024];
//         int n;
//         while ((n = reader.read(buffer)) != -1){
//            writer.write(buffer, 0, n);
//         }
//         sEmailTemplate = writer.toString();
//
//      }
//      catch (Exception e){
//         throw new RuntimeException("Can not open the email template file: " + fileEmailTemplate);
//      }

      System.out.println("ok, loaded.");
   }







   public static String getMongoURL(){
      String sMongoURL = properties.getProperty("database_ip");
      
      if(sMongoURL == null){
         throw new ServiceConfigurationException(
                     "'database_ip' is missed in the server.properties"
                  );
      }
      else{
         return sMongoURL;
      }
   }
   
   
   
   public static String getDatabase(){
      String sMongoURL = properties.getProperty("database_name");
      
      if(sMongoURL == null){
         throw new ServiceConfigurationException(
                     "'database_name' is missed in the server.properties"
                  );
      }
      else{
         return sMongoURL;
      }
   }
   
   
   public static boolean isDebug(){
      return properties.getProperty("debug") != null;
   }
   


   
   

   public static String getNodeIP(){
      return properties.getProperty("ip");
   }





   public static String getEmailSmtpServer(){
      return properties.getProperty("email_smtp_host");
   }
   public static String getEmailSmtpPort(){
      return properties.getProperty("email_smtp_port");
   }
   public static String getEmailSmtpUser(){
      return properties.getProperty("email_smtp_user");
   }
   public static String getEmailSmtpPassword(){
      return properties.getProperty("email_smtp_password");
   }
   public static String getRobot(){
      return properties.getProperty("email_robot");
   }
   public static String getEmailAddressSystemAdmin(){
      return properties.getProperty("email_address_feedback");
   }
   
   



   public static String getEmailConfirmationLink(){
      return properties.getProperty("email_confirmation_link");
   }














   public static String getImageFolder() throws ServiceConfigurationException{
      String sImageFolder = properties.getProperty("image_directory");

      if(sImageFolder == null){
         throw new ServiceConfigurationException(
                     "'image_directory' is missed in the server.properties"
                  );
      }
      else{
         if(sImageFolder.endsWith("/")){
         }
         else{
            sImageFolder += '/';
         }
         
         
         return sImageFolder;
      }
   }






   public static String getImageURL() throws ServiceConfigurationException{
      String sImageURL = properties.getProperty("image_url");

      if(sImageURL == null){
         throw new ServiceConfigurationException(
                     "'image_url' is missed in the server.properties"
                  );
      }
      else{
         if(sImageURL.endsWith("/")){
         }
         else{
            sImageURL += '/';
         }
         return sImageURL;
      }
   }





   public static String getAudioURL() throws ServiceConfigurationException{
      String sImageURL = properties.getProperty("audio_url");

      if(sImageURL == null){
         throw new ServiceConfigurationException(
                     "'audio_url' is missed in the server.properties"
                  );
      }
      else{
         if(sImageURL.endsWith("/")){
         }
         else{
            sImageURL += '/';
         }
         return sImageURL;
      }
   }







   public static int getCommercialFreeWordLimit() throws ServiceConfigurationException{
      String sFreeWordLimit = properties.getProperty("commercial_free_word_limit");

      if(sFreeWordLimit == null || "".equals(sFreeWordLimit)){
         throw new ServiceConfigurationException(
                     "'commercial_free_word_limit' is missed in the server.properties"
                  );
      }
      else{
         try{
            return Integer.parseInt(sFreeWordLimit);
         }
         catch (NumberFormatException e){
            throw new ServiceConfigurationException(
                                                          "'commercial_free_word_limit' must be int type"
                                                        );
         }
      }
   }











   public static String getLogPattern(){
      return properties.getProperty("internal_logger_format");
   }





   public static String getLogPaths(){
      return properties.getProperty("log_paths");
   }
}
