using System.Reflection;
using JiShe.CollectBus.Common.BuildSendDatas;
using JiShe.CollectBus.Common.Enums;
using JiShe.CollectBus.Common.Models;

namespace JiShe.CollectBus.Protocol.T37612012.SendData
{
    /// <summary>
    /// 构建3761下发报文
    /// </summary>
    public static class Telemetry3761PacketBuilder
    {
        /// <summary>
        /// 构建报文的委托
        /// </summary> 
        public delegate Telemetry3761PacketResponse T3761Delegate(Telemetry3761PacketRequest request);

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

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

        #region AFN_00H 确认∕否认
        public static Telemetry3761PacketResponse AFN00_Fn_Send(Telemetry3761PacketRequest request)
        {
            var reqParameter = new ReqParameter2()
            {
                AFN = AFN.确认或否认,
                FunCode = (int)CMasterStationFunCode.请求2级数据,
                A = request.FocusAddress,
                Seq = new Seq()
                {
                    TpV = TpV.附加信息域中无时间标签,
                    FIRFIN = FIRFIN.单帧,
                    CON = CON.需要对该帧进行确认,
                    PRSEQ = 0,
                },
                MSA = Build3761SendData.GetMSA(request.FocusAddress),
                Pn = request.Pn,
                Fn = request.Fn
            };
            var bytes = Build3761SendData.BuildSendCommandBytes(reqParameter);
            return new Telemetry3761PacketResponse() { Seq = reqParameter.Seq.PRSEQ, Data = bytes, MSA = reqParameter.MSA, };
        }
        #endregion


        #region AFN_01H 复位命令
        public static Telemetry3761PacketResponse AFN01_Fn_Send(Telemetry3761PacketRequest request)
        {
            var reqParameter = new ReqParameter2()
            {
                AFN = AFN.复位,
                FunCode = (int)CMasterStationFunCode.复位命令,
                A = request.FocusAddress,
                Seq = new Seq()
                {
                    TpV = TpV.附加信息域中无时间标签,
                    FIRFIN = FIRFIN.单帧,
                    CON = CON.需要对该帧进行确认,
                    PRSEQ = 10,
                },
                MSA = Build3761SendData.GetMSA(request.FocusAddress),
                Pn = request.Pn,
                Fn = request.Fn
            };
            var bytes = Build3761SendData.BuildSendCommandBytes(reqParameter);
            return new Telemetry3761PacketResponse() { Seq = reqParameter.Seq.PRSEQ, Data = bytes, MSA = reqParameter.MSA, };
        }
        #endregion


        #region AFN_02H 链路接口检测
        public static Telemetry3761PacketResponse AFN02_Fn_Send(Telemetry3761PacketRequest request)
        {
            var reqParameter = new ReqParameter2()
            {
                AFN = AFN.链路接口检测,
                FunCode = (int)CMasterStationFunCode.请求2级数据,
                A = request.FocusAddress,
                Seq = new Seq()
                {
                    TpV = TpV.附加信息域中无时间标签,
                    FIRFIN = FIRFIN.单帧,
                    CON = CON.不需要对该帧进行确认,
                    PRSEQ = 0,
                },
                MSA = Build3761SendData.GetMSA(request.FocusAddress),
                Pn = request.Pn,
                Fn = request.Fn
            };
            var bytes = Build3761SendData.BuildSendCommandBytes(reqParameter);
            return new Telemetry3761PacketResponse() { Seq = reqParameter.Seq.PRSEQ, Data = bytes, MSA = reqParameter.MSA, };
        }
        #endregion

        #region AFN_04H 设置参数
        public static Telemetry3761PacketResponse AFN04_Fn_Send(Telemetry3761PacketRequest request)
        {
            var reqParameter = new ReqParameter2()
            {
                AFN = AFN.设置参数,
                FunCode = (int)CMasterStationFunCode.请求1级数据,
                A = request.FocusAddress,
                Seq = new Seq()
                {
                    TpV = TpV.附加信息域中无时间标签,
                    FIRFIN = FIRFIN.单帧,
                    CON = CON.需要对该帧进行确认,
                    PRSEQ = 10,
                },
                MSA = Build3761SendData.GetMSA(request.FocusAddress),
                Pn = request.Pn,
                Fn = request.Fn
            };
            var bytes = Build3761SendData.BuildSendCommandBytes(reqParameter);
            return new Telemetry3761PacketResponse() { Seq = reqParameter.Seq.PRSEQ, Data = bytes, MSA = reqParameter.MSA, };
        }

        #endregion

        #region AFN_05H 控制命令
        public static Telemetry3761PacketResponse AFN05_Fn_Send(Telemetry3761PacketRequest request)
        {
            var reqParameter = new ReqParameter2()
            {
                AFN = AFN.控制命令,
                FunCode = (int)CMasterStationFunCode.请求1级数据,
                A = request.FocusAddress,
                Seq = new Seq()
                {
                    TpV = TpV.附加信息域中无时间标签,
                    FIRFIN = FIRFIN.单帧,
                    CON = CON.需要对该帧进行确认,
                    PRSEQ = 10,
                },
                MSA = Build3761SendData.GetMSA(request.FocusAddress),
                Pn = request.Pn,
                Fn = request.Fn
            };
            var bytes = Build3761SendData.BuildSendCommandBytes(reqParameter);
            return new Telemetry3761PacketResponse() { Seq = reqParameter.Seq.PRSEQ, Data = bytes, MSA = reqParameter.MSA, };
        }
        #endregion

        #region AFN_09H 请求终端配置及信息
        public static Telemetry3761PacketResponse AFN09_Fn_Send(Telemetry3761PacketRequest request)
        {
            var reqParameter = new ReqParameter2()
            {
                AFN = AFN.请求终端配置,
                FunCode = (int)CMasterStationFunCode.请求2级数据,
                A = request.FocusAddress,
                Seq = new Seq()
                {
                    TpV = TpV.附加信息域中无时间标签,
                    FIRFIN = FIRFIN.单帧,
                    CON = CON.不需要对该帧进行确认,
                    PRSEQ = 0,
                },
                MSA = Build3761SendData.GetMSA(request.FocusAddress),
                Pn = request.Pn,
                Fn = request.Fn
            };
            var bytes = Build3761SendData.BuildSendCommandBytes(reqParameter);
            return new Telemetry3761PacketResponse() { Seq = reqParameter.Seq.PRSEQ, Data = bytes, MSA = reqParameter.MSA, };
        }

        #endregion

        #region AFN_0AH 查询参数
        public static Telemetry3761PacketResponse AFN0A_Fn_Send(Telemetry3761PacketRequest request)
        {
            var reqParameter = new ReqParameter2()
            {
                AFN = AFN.查询参数,
                FunCode = (int)CMasterStationFunCode.请求2级数据,
                A = request.FocusAddress,
                Seq = new Seq()
                {
                    TpV = TpV.附加信息域中无时间标签,
                    FIRFIN = FIRFIN.单帧,
                    CON = CON.不需要对该帧进行确认,
                    PRSEQ = 0,
                },
                MSA = Build3761SendData.GetMSA(request.FocusAddress),
                Pn = request.Pn,
                Fn = request.Fn
            };
            var bytes = Build3761SendData.BuildSendCommandBytes(reqParameter);
            return new Telemetry3761PacketResponse() { Seq = reqParameter.Seq.PRSEQ, Data = bytes, MSA = reqParameter.MSA, };
        }
        #endregion

        #region AFN_0CH 请求一类数据
        public static Telemetry3761PacketResponse AFN0C_Fn_Send(Telemetry3761PacketRequest request)
        {
            var reqParameter = new ReqParameter2()
            {
                AFN = AFN.请求实时数据,
                FunCode = (int)CMasterStationFunCode.请求2级数据,
                A = request.FocusAddress,
                Seq = new Seq()
                {
                    TpV = TpV.附加信息域中无时间标签,
                    FIRFIN = FIRFIN.单帧,
                    CON = CON.不需要对该帧进行确认,
                    PRSEQ = 2,
                },
                MSA = Build3761SendData.GetMSA(request.FocusAddress),
                Pn = request.Pn,
                Fn = request.Fn
            };
            var bytes = Build3761SendData.BuildSendCommandBytes(reqParameter);
            return new Telemetry3761PacketResponse() { Seq = reqParameter.Seq.PRSEQ, Data = bytes, MSA = reqParameter.MSA, };
        }
        #endregion

        #region AFN_0DH 请求二类数据
        public static Telemetry3761PacketResponse AFN0D_Fn_Send(Telemetry3761PacketRequest request)
        {
            var reqParameter = new ReqParameter2()
            {
                AFN = AFN.请求历史数据,
                FunCode = (int)CMasterStationFunCode.请求2级数据,
                A = request.FocusAddress,
                Seq = new Seq()
                {
                    TpV = TpV.附加信息域中无时间标签,
                    FIRFIN = FIRFIN.单帧,
                    CON = CON.不需要对该帧进行确认,
                    PRSEQ = 2,
                },
                MSA = Build3761SendData.GetMSA(request.FocusAddress),
                Pn = request.Pn,
                Fn = request.Fn
            };
            var bytes = Build3761SendData.BuildSendCommandBytes(reqParameter);
            return new Telemetry3761PacketResponse() { Seq = reqParameter.Seq.PRSEQ, Data = bytes, MSA = reqParameter.MSA, };
        }
        #endregion

        #region AFN10H 数据转发
        public static Telemetry3761PacketResponse AFN10_Fn_Send(Telemetry3761PacketRequest request)
        {
            var reqParameter = new ReqParameter2()
            {
                AFN = AFN.数据转发,
                FunCode = (int)CMasterStationFunCode.请求2级数据,
                A = request.FocusAddress,
                Seq = new Seq()
                {
                    TpV = TpV.附加信息域中无时间标签,
                    FIRFIN = FIRFIN.单帧,
                    CON = CON.不需要对该帧进行确认,
                    PRSEQ = 0,
                },
                MSA = Build3761SendData.GetMSA(request.FocusAddress),
                Pn = request.Pn,
                Fn = request.Fn
            };
            var bytes = Build3761SendData.BuildSendCommandBytes(reqParameter, request.DataUnit);
            return new Telemetry3761PacketResponse() { Seq = reqParameter.Seq.PRSEQ, Data = bytes, MSA = reqParameter.MSA, };
        }

        #region SpecialAmmeter 特殊电表转发
        //TODO 特殊电表处理

        #endregion

        #endregion
    }
}
