using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JiShe.CollectBus.Protocol3761
{

    /// <summary>
    /// 解析3761报文
    /// </summary>
    public class TB3761
    {
        /// <summary>
        /// 报文
        /// </summary>
        public BaseHexMessage BaseHexMessage { get; set;}=new BaseHexMessage();

        /// <summary>
        /// 报文ID
        /// </summary>
        public string? MessageId { get; set; }

        /// <summary>
        /// 消息时间
        /// </summary>
        public DateTime ReceivedTime { get; set; }=DateTime.Now;

        /// <summary>
        /// 控制域C
        /// </summary>
        public C C { get; set; } = new C();

        /// <summary>
        /// 地址域A
        /// </summary>
        public A A { get; set; } = new A();

        /// <summary>
        /// 帧序列域 SEQ
        /// </summary>
        public SEQ SEQ { get; set; } = new SEQ();

        /// <summary>
        ///  用户数据区
        ///  功能码
        /// </summary>
        public AFN_FC AFN_FC { get; set; } = new AFN_FC();

        /// <summary>
        ///  用户数据区
        ///  信息点DA Pn
        /// </summary>
        public DA DA { get; set; } = new DA();

        /// <summary>
        /// 用户数据区
        /// 信息类DT Fn
        /// </summary>
        public DT DT { get; set; } = new DT();

        /// <summary>
        /// 数据单元标识和数据单元格式
        /// </summary>
        public UnitData UnitData { get; set; } = new UnitData();
    }

    #region

    /// <summary>
    /// 报文信息
    /// </summary>
    public class BaseHexMessage
    {
        /// <summary>
        /// 报文
        /// </summary>
        public string? HexMessageString { get; set; }

        /// <summary>
        /// 报文数组
        /// </summary>
        public List<string>? HexMessageList { get; set; }
    }

    /// <summary>
    /// 控制域C
    /// </summary>
    public class C
    {
        /// <summary>
        /// 控制域C报文
        /// </summary>
        public BaseHexMessage? BaseHexMessage { get; set; }

        /// <summary>
        /// 传输方向位D7 DIR=0，表示此帧报文是由主站发出的下行报文；DIR=1，表示此帧报文是由终端发出的上行报文。
        /// </summary>
        public int DIR { get; set; }
        /// <summary>
        /// D6启动标志位 0:表示此帧报文来自从动站(终端),1:表示此帧报文来自启动站(服务端)
        /// </summary>
        public int PRM { get; set; }
        /// <summary>
        /// D5下行：帧计数位(FCB)/上行(ACD)：要求访问位(终端有重要事件等待访问),
        /// </summary>
        public int FCB { get; set; }
        /// <summary>
        ///  下行：帧计数有效位(决定FCB位有效/无效)/上行：保留 D4
        /// </summary>
        public int FCV { get; set; }
        /// <summary>
        /// 功能码 D0-D3
        /// </summary>
        public int FC { get; set; }

    }


    /// <summary>
    /// 地址域A
    /// </summary>
    public class A
    {
        /// <summary>
        /// 地址域报文
        /// </summary>
        public BaseHexMessage? BaseHexMessage { get; set; }

        /// <summary>
        /// 集中器/终端编码
        /// </summary>
        public string? Code { get; set; }
    
        /// <summary>
        /// 行政区划码A1
        /// </summary>
        public string? A1 { get; set; }
        /// <summary>
        ///  终端地址A2
        /// </summary>
        public int A2 { get; set; }
        /// <summary>
        /// 站地址和组地址标志A3
        /// </summary>
        public A3? A3 { get; set; }
    }

    /// <summary>
    /// 站地址和组地址标志A3
    /// </summary>
    public class A3
    {
        /// <summary>
        /// 地址域A3报文
        /// </summary>
        public BaseHexMessage? BaseHexMessage { get; set; }

        /// <summary>
        /// 终端组地址标志，D0=0即False 表示终端地址A2 为单地址
        /// </summary>
        public int D0 { get; set; }
        /// <summary>
        /// 主站地址 MSA D1～D7 组成 0～127
        /// </summary>
        public int D1_D7 { get; set; }
    }


    /// <summary>
    /// 帧序列域 SEQ
    /// </summary>
    public class SEQ
    {
        /// <summary>
        /// 帧序列域报文
        /// </summary>
        public BaseHexMessage? BaseHexMessage { get; set; }
        /// <summary>
        /// 响应帧序号
        /// </summary>
        public int PSEQ { get; set; }
        /// <summary>
        /// CON为“1”，表示需要对该帧报文进行确认；置“0”，表示不需要对该帧报文进行确认。
        /// </summary>
        public int CON { get; set; }
        /// <summary>
        /// 末帧标志
        /// </summary>
        public int FIN { get; set; }
        /// <summary>
        /// 首帧标志
        /// </summary>
        public int FIR { get; set; }
        /// <summary>
        /// 帧时间标签有效位，TpV=0，表示在附加信息域中无时间标签Tp；TpV=1，表示在附加信息域中带有时间标签Tp
        /// </summary>
        public int TpV { get; set; }
    }



    /// <summary>
    /// 用户数据区
    /// 功能码
    /// </summary>
    public class AFN_FC
    {
        public BaseHexMessage? BaseHexMessage { get; set; }

        /// <summary>
        /// 功能码
        /// </summary>
        public int AFN { get; set; }
    }


    /// <summary>
    /// 用户数据区
    /// 信息点DA Pn
    /// </summary>
    public class DA
    {
        public BaseHexMessage? BaseHexMessage { get; set; }

        /// <summary>
        /// 信息点 DA Pn
        /// </summary>
        public int Pn { get; set; }
    }


    /// <summary>
    /// 用户数据区
    /// 信息类DT Fn
    /// </summary>
    public class DT
    {
        public BaseHexMessage? BaseHexMessage { get; set; }
        /// <summary>
        /// 信息类 DT Fn
        /// </summary>
        public int Fn { get; set; }
    }


    /// <summary>
    /// 数据单元标识和数据单元格式
    /// </summary>
    public class UnitData: BaseHexMessage{ }

    #endregion

}
