using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace MEU.API.Config
{
    public class ConfigEnvironment 
    {
        /// <summary>
        /// development/production
        /// </summary>
        /// <param name="environmentName"></param>
        public static void load(string environmentName) 
        {
            //On prodction we will use all config from environment
            if (environmentName.ToLower().Trim().Equals("production"))
            {   
                return;
            }
            var envPath = Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Config/.env." + environmentName + ".json");
            if (!File.Exists(envPath))
            {
                return;
            }
            
            using (StreamReader file = File.OpenText(envPath))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                JObject o2 = (JObject)JToken.ReadFrom(reader);
                foreach (var property in o2.Properties())
                {
                    Environment.SetEnvironmentVariable(property.Name, property.Value.ToString());
                }
            }
        }

        public static string STORAGE_ROOT = Environment.GetEnvironmentVariable("STORAGE_ROOT");
        public static string STORAGE_RELATIVE_DRAFT_PATH = "/drafts"; //files_draftPath //STORAGE_RELATIVE_DRAFT_PATH
        public static string STORAGE_RELATIVE_USERS = "/users";  //files_userPath //STORAGE_RELATIVE_USERS
        public static string STORAGE_RALATIVE_AVATAR = "/avatars"; //files_avatarPath //STORAGE_RALATIVE_AVATAR
        public static string STORAGE_RELATIVE_ATTACHMENT = "/general_attchments"; //files_generalAttachment //STORAGE_RELATIVE_ATTACHMENT
        public static string STORAGE_PUBLIC = "/public"; //files_publicPath //STORAGE_PUBLIC
        public static string STORAGE_RALATIVE_COMPANYINFO = "/company_info";

        public static string STORAGE_RELATIVE_CV = "/cvs";
        public static string STORAGE_RELATIVE_CONTRACT = "/contracts"; //files_publicPath     //file_contractPath
        public static string STORAGE_RELATIVE_SUBFORM_TEMPLATE = "/subforms"; //files_publicPath     //file_subformTemplatePath
        public static string STORAGE_RELATIVE_COMMENT_ATTACHMENT = "/comment_attachments"; //files_publicPath     //file_commentAttachment
        public static string STORAGE_RELATIVE_WORKFLOW_ATTACHMENT = "/workflow_attachments"; //files_publicPath     //file_workflowAttachment
        public static string STORAGE_RELATIVE_SIGNATURE_ATTACHMENT = "/signature_attachments"; //files_publicPath     //file_signaturePath
        public static string STORAGE_RELATIVE_SALARY_EXCEL_FILE = "/salary_excel_files"; //files_publicPath     //salary_excel_file
        public static string STORAGE_RELATIVE_HR_APPLICATION_TEMPLATE = "/hr_application_templates"; //files_publicPath     //hr_application_templates



        public static string MAILSERVER_SMTPHOST = Environment.GetEnvironmentVariable("MAILSERVER_SMTPHOST");// Environment.GetEnvironmentVariable("MAILSERVER_SMTPHOST"); //mailServer_smtpHost //MAILSERVER_SMTPHOST
        public static string MAILSERVER_SMTPPORT = Environment.GetEnvironmentVariable("MAILSERVER_SMTPPORT");// Environment.GetEnvironmentVariable("MAILSERVER_SMTPPORT"); //mailServer_smtpPort //MAILSERVER_SMTPPORT
        public static string MAILSERVER_USERNAME = Environment.GetEnvironmentVariable("MAILSERVER_USERNAME");// Environment.GetEnvironmentVariable("MAILSERVER_USERNAME"); //mailAdminAccount_Username //MAILSERVER_USERNAME
        public static string MAILSERVER_PASSWORD = Environment.GetEnvironmentVariable("MAILSERVER_PASSWORD");// Environment.GetEnvironmentVariable("MAILSERVER_PASSWORD"); //mailAdminAccount_Passwordword //MAILSERVER_PASSWORD
        public static string FRONTEND_HOST = Environment.GetEnvironmentVariable("FRONTEND_HOST"); //frontend_host //FRONTEND_HOST
        public static string TOKEN_ENCRYPTED_KEY = "sah@kk#nn#v4,^121.,!@{}psassaasa";// Environment.GetEnvironmentVariable("TOKEN_ENCRYPTED_KEY");//  encryptKeys_tokenKey;
        public static string DATA_ENCRYPTED_KEY = "B546C8DF278CD5931069B522E695D4FA";// Environment.GetEnvironmentVariable("DATA_ENCRYPTED_KEY");// encryptKeys_dataKey;
        public static int DEFAULT_PAGE_SIZE = 10;// Int32.Parse(Environment.GetEnvironmentVariable("DEFAULT_PAGE_SIZE"));

        //DB Connection
        public static string PSQL_HOST = Environment.GetEnvironmentVariable("PSQL_HOST");
        public static string PSQL_PORT = Environment.GetEnvironmentVariable("PSQL_PORT");
        public static string PSQL_PASSWORD = Environment.GetEnvironmentVariable("PSQL_PASSWORD");
        public static string PSQL_USER = Environment.GetEnvironmentVariable("PSQL_USER");
        public static string PSQL_DBNAME = Environment.GetEnvironmentVariable("PSQL_DBNAME");

        public static string ENVIRONMENT = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        public static string CONECTION_STRING = "Persist Security Info=True;Password="
                + ConfigEnvironment.PSQL_PASSWORD + ";Username="
                + ConfigEnvironment.PSQL_USER + ";Database="
                + ConfigEnvironment.PSQL_DBNAME + ";Host="
                + ConfigEnvironment.PSQL_HOST + ";Port="
                + ConfigEnvironment.PSQL_PORT;

    }
}
