using System.Reflection;
using JiShe.CollectBus.Common.BuildSendDatas;
using JiShe.CollectBus.Common.Extensions;

namespace JiShe.CollectBus.Protocol.T6452007.SendData
{
    /// <summary>
    /// 构建645-2007下发报文
    /// </summary>
    public static class Telemetry6452007PacketBuilder
    {
        /// <summary>
        /// 构建报文的委托
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public delegate Telemetry6452007PacketResponse T6452007Delegate(Telemetry6452007PacketRequest request);

        /// <summary>
        /// 编码与方法的映射表
        /// </summary>
        public static readonly Dictionary<string, T6452007Delegate> T645ControlHandlers = new();

        static Telemetry6452007PacketBuilder()
        {
            // 初始化时自动注册所有符合命名规则的方法
            var methods = typeof(Telemetry6452007PacketBuilder).GetMethods(BindingFlags.Static | BindingFlags.Public);
            foreach (var method in methods)
            {
                if (method.Name.StartsWith("C") && method.Name.EndsWith("_Send"))
                {
                    string code = method.Name;
                    var delegateInstance = (T6452007Delegate)Delegate.CreateDelegate(typeof(T6452007Delegate), method);
                    T645ControlHandlers[code] = delegateInstance;
                }
            }
        }

        #region 1CH 跳合闸、报警、保电

        /// <summary>
        /// 1CH 跳合闸
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static Telemetry6452007PacketResponse C1C_01_Send(Telemetry6452007PacketRequest request)
        {
            var itemCodeArr = request.ItemCode.Split('_');
            var c_data = itemCodeArr[0];
            var n_data = itemCodeArr[1];
            string password = request.Password;
            string pwdLevel = "02"; 

            if (!string.IsNullOrWhiteSpace(password) && password.Contains("|"))
            {
                var sp = password.Split('|');
                password = sp[0];
                pwdLevel = sp[1];
            }

            var strDate = DateTime.Now.AddYears(3).ToString("000012ddMMyy").StrAddSpan();//命令有效截止时间
          
            var strP = password.StrAddSpan().StrReverseOrder();
            var strSJY = " " + pwdLevel + " " + strP + " 01 00 00 00 " + n_data + " 00 " + strDate;
            var dataUnit = strSJY.Replace(" ", "").StringToPairs();

            var dataList = Build645SendData.Build645SendCommand(request.MeterAddress, c_data, dataUnit);
            return new Telemetry6452007PacketResponse() { Data = dataList };
        }


        /// <summary>
        /// 1CH 保电
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static Telemetry6452007PacketResponse C1C_03_Send(Telemetry6452007PacketRequest request)
        {
            var itemCodeArr = request.ItemCode.Split('_');
            var c_data = itemCodeArr[0];
            var n_data = itemCodeArr[1];
            string password = request.Password;

            if (!string.IsNullOrWhiteSpace(password) && password.Contains("|"))
            {
                var sp = password.Split('|');
                password = sp[0];
            }

            var strDate = (n_data + DateTime.Now.AddDays(1).ToString("00000012ddMMyy")).StrAddSpan();

            var strP = password.StrAddSpan().StrReverseOrder();

            var strSJY = " 02 " + strP + " 01 00 00 00 " + strDate;

            var dataUnit = strSJY.Replace(" ", "").StringToPairs();

            var dataList = Build645SendData.Build645SendCommand(request.MeterAddress, c_data, dataUnit);
            return new Telemetry6452007PacketResponse() { Data = dataList };
        }
        #endregion 
    }
}
