using DeviceRepair.Utils;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TsSFCDevice.Client.Biz.Base.Service
{
    public class ParametersObject
    {
        private static readonly ParametersObject instance = new ParametersObject();
        private IDictionary<string, string> systemParameters = new Dictionary<string, string>();
        public IDictionary<string, string> SystemParameters { get { return this.systemParameters; } }

        public static ParametersObject GetInstance(string cOperator = "")
        {
            #region  附加SAP基本参数

            if (instance.systemParameters == null)
            {
                instance.systemParameters = new Dictionary<string, string>();
            }
            if (instance.systemParameters.Count == 0)
            {
                if (!instance.systemParameters.ContainsKey("OPERATORAUTOID"))
                {
                    instance.systemParameters.Add("OPERATORAUTOID", ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.Id.ToString()) ?? "0"));
                }
                else
                {
                    instance.systemParameters["OPERATORAUTOID"] = ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.Id.ToString()) ?? "0");
                }
                if (!instance.systemParameters.ContainsKey("OPERATORGUID"))
                {
                    instance.systemParameters.Add("OPERATORGUID", ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.GUID.ToString()) ?? "SYSADMIN"));
                }
                else
                {
                    instance.systemParameters["OPERATORGUID"] = ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.GUID.ToString()) ?? "SYSADMIN");
                }
                if (!instance.systemParameters.ContainsKey("OPERATOR"))
                {
                    instance.systemParameters.Add("OPERATOR", string.IsNullOrEmpty(cOperator) ? ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.UserCode) ?? "SYSADMIN") : cOperator);
                }
                else
                {
                    instance.systemParameters["OPERATOR"] = string.IsNullOrEmpty(cOperator) ? ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.UserCode) ?? "SYSADMIN") : cOperator;
                }
                if (!instance.systemParameters.ContainsKey("CLIENTIP"))
                {
                    instance.systemParameters.Add("CLIENTIP", ComputerHelper.GetIPAddress);
                }
                if (!instance.systemParameters.ContainsKey("CLIENTMAC"))
                {
                    instance.systemParameters.Add("CLIENTMAC", ComputerHelper.GetMacAddress);
                }
                if (!instance.systemParameters.ContainsKey("CLIENTNAME"))
                {
                    instance.systemParameters.Add("CLIENTNAME", ComputerHelper.GetComputerName);
                }
                //添加客户端版本 Myl20211125
                if (!instance.systemParameters.ContainsKey("APPVERSION"))
                {
                    instance.systemParameters.Add("APPVERSION", Application.ProductVersion);
                }
            }
            #endregion
            if (!instance.systemParameters.ContainsKey("OPERATORAUTOID"))
            {
                instance.systemParameters.Add("OPERATORAUTOID", ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.Id.ToString()) ?? "0"));
            }
            else
            {
                instance.systemParameters["OPERATORAUTOID"] = ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.Id.ToString()) ?? "0");
            }
            if (!instance.systemParameters.ContainsKey("OPERATOR"))
            {
                instance.systemParameters.Add("OPERATOR", string.IsNullOrEmpty(cOperator) ? ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.UserCode) ?? "SYSADMIN") : cOperator);
            }
            else
            {
                instance.systemParameters["OPERATOR"] = string.IsNullOrEmpty(cOperator) ? ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.UserCode) ?? "SYSADMIN") : cOperator;
            }
            if (!instance.systemParameters.ContainsKey("OPERATORGUID"))
            {
                instance.systemParameters.Add("OPERATORGUID", ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.GUID.ToString()) ?? "SYSADMIN"));
            }
            else
            {
                instance.systemParameters["OPERATORGUID"] = ((Utils.Utility.SystemRuntimeInfo?.CurrentUser?.GUID.ToString()) ?? "SYSADMIN");
            }
            return instance;
        }

        protected IDictionary<string, string> CombineParameters(IDictionary<string, string> apiParameters)
        {
            IDictionary<string, string> parameters = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> entry in systemParameters)
            {
                parameters.Add(entry);
            }
            if (apiParameters != null)
            {
                foreach (KeyValuePair<string, string> entry in apiParameters)
                {
                    if (!parameters.ContainsKey(entry.Key))
                    {
                        parameters.Add(entry);
                    }
                }
            }
            return parameters;
        }

        public string GetJsonSerialized(IDictionary<string, string> apiParameters)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(CombineParameters(apiParameters));
        }

    }


}
