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",
            "SecurityProfileLevel",
            "SecurityPassword"
        };

        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 int SecurityProfileLevel = 0;

        public static string SecurityPassword = 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 List<int> WSS_Ports = null;

        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];

                                if (string.IsNullOrEmpty(value))
                                {
                                    WS_Port = 80;
                                    break;
                                }

                                WS_Port = Int32.TryParse(value, out WS_Port) ? WS_Port : 80;
                            }
                            break;
                        case "WSSPort":
                            {
                                var value = configuration[key];

                                if (string.IsNullOrEmpty(value))
                                {
                                    WSS_Ports = new List<int>();
                                    break;
                                }

                                var ports = value.Split(',');
                                WSS_Ports = new List<int>();
                                foreach (var port in ports)
                                {
                                    WSS_Ports.Add(Convert.ToInt32(port));
                                }
                            }
                            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;
                        case "SecurityProfileLevel":
                            {
                                var value = configuration[key];

                                SecurityProfileLevel = Convert.ToInt32(value);
                            }
                            break;
                        case "SecurityPassword":
                            {
                                var value = configuration[key];

                                SecurityPassword = 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 List<int> GetWSS_Ports()
        {
            return WSS_Ports;
        }

        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);





    }
}
