using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;

namespace EVCB_OCPP.WSServer
{
    public static class GlobalConfig
    {
        public static List<string> ConfigKeys = new List<string>()
        {
            "WSPort",
            "WSSPort",
            "OCPP20_WSUrl",
            "OCPP20_WSSUrl"
        };

        public static List<string> DenyModelNames = new List<string>();

        public static  string UTC_DATETIMEFORMAT = "yyyy-MM-dd'T'HH':'mm':'ss'Z'";

        public static string TCC_API_URL = string.Empty;

        public static string TCC_SALTKEY = string.Empty;

        public static string OCPP20_WSUrl = string.Empty;

        public static string OCPP20_WSSUrl = string.Empty;

        public static JsonSerializerSettings JSONSERIALIZER_FORMAT = new JsonSerializerSettings()
        {
            NullValueHandling = NullValueHandling.Ignore,
            Formatting = Formatting.None,
            DateFormatString = UTC_DATETIMEFORMAT
        };

        /// <summary>
        /// 預設心跳間隔時間 單位:秒
        /// </summary>
        private static int DEFAULT_HEARTBEAT_INTERVAL = 60;



        /// <summary>
        ///WS Port
        /// </summary>
        private static int WS_Port = 80;


        /// <summary>
        ///WSS Port
        /// </summary>
        private static int WSS_Port = 443;

        public static bool LoadAPPConfig(IConfiguration configuration)
        {
            bool result = false;
            string key = string.Empty;
            try
            {

                for (int i = 0; i < ConfigKeys.Count; i++)
                {
                    key = ConfigKeys[i];
                    switch (key)
                    {
                        case "WSPort":// convert to int type                       
                            {
                                var value = configuration[key];

                                WS_Port = string.IsNullOrEmpty(value) ? WS_Port : Convert.ToInt32(value);
                            }
                            break;
                        case "WSSPort":
                            {
                                var value = configuration[key];

                                WSS_Port = string.IsNullOrEmpty(value) ? WS_Port : Convert.ToInt32(value);
                            }
                            break;
                        case "OCPP20_WSUrl":// convert to int type                       
                            {
                                var value = configuration[key];

                                OCPP20_WSUrl = value;
                            }
                            break;
                        case "OCPP20_WSSUrl":// convert to int type                       
                            {
                                var value = configuration[key];

                                OCPP20_WSSUrl = value;
                            }
                            break;
                        default://convert to string type                             
                            break;

                    }
                }
                result = true;

            }
            catch (Exception ex)
            {
                Console.WriteLine(key + " Load from APPConfig " + ex.ToString());

            }

            return result;
        }

        public static int GetWS_Port()
        {
            return WS_Port;
        }


        public static int GetWSS_Port()
        {
            return WSS_Port;
        }

        public static int GetHEARTBEAT_INTERVAL()
        {
            return DEFAULT_HEARTBEAT_INTERVAL;
        }

        public static readonly int DB_DefaultConnectionTimeout = 60;
   
        /// <summary>
        /// 預設 Null的 DateTime 
        /// </summary>
        public static DateTime DefaultNullTime = new DateTime(1991, 1, 1);





    }
}
