using PlcSiemens.O;
using PlcSiemens.Protocol.Common;

namespace PlcSiemens.Protocol
{
    /// <summary>
    /// 请求数据包参数
    /// </summary>
    public class ParamsRequest
    {
        public byte[] Head { get; set; }// 0x00 0x01 0x12
        public byte PLen { get; set; }  // par len 0x04
        public byte Uk { get; set; }    // unknown
        public byte Tg { get; set; }    // type and group  (4 bits type and 4 bits group)
        public byte SubFun { get; set; } // subfunction
        public byte Seq { get; set; }   // sequence
        public ushort Rsvd { get; set; }//Next所需的参数
        public ushort ErrNo { get; set; }//Next所需的参数

        public bool First = true;

        public ushort PacketLength
        {
            get { return First ? (ushort)0x08 : (ushort)0x0C; }
        }

        public ParamsRequest()
        {
            Head = new byte[3];
        }

        public virtual void GetByteBuffer(ByteBuffer buffer)
        {
            buffer.Push(Head);
            buffer.Push(PLen);
            buffer.Push(Uk);
            buffer.Push(Tg);
            buffer.Push(SubFun);
            buffer.Push(Seq);
            if (First) return;

            buffer.Push(Rsvd);
            buffer.Push(ErrNo);
        }

        public void Build(bool first, byte seq, GrType grType, PduFuncType pduFuncType)
        {
            First = first;
            Head = new byte[] { 0x00, 0x01, 0x12 };
            PLen = first ? (byte)0x04 : (byte)0x08;
            Uk = 0x11;//用处未知 有时会有0x11
            Tg = (byte)grType;
            SubFun = (byte)pduFuncType;
            Seq = seq;
            if (first) return;
            Rsvd = 0x0000;
            ErrNo = 0x0000;
        }
    }
}
