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

namespace PlcSiemens.Protocol.Szl
{
    public class SzlDataRequest
    {
        public static ushort Len = 0x08;
        public byte Ret { get; set; }  // 0xFF for request
        public byte Ts { get; set; }   // 0x09 Transport size
        public ushort DLen { get; set; } // Data len 0x04
        public ushort ID { get; set; }  // First 需要的参数
        public ushort Index { get; set; }// First 需要的参数
        public bool First = true;

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

        public void GetByteBuffer(ByteBuffer buffer)
        {
            buffer.Push(Ret);
            buffer.Push(Ts);
            buffer.Push(DLen);
            if (!First) return;
            buffer.Push(ID);
            buffer.Push(Index);
        }

        public void Build(bool first, ushort id, ushort index)
        {
            First = first;
            if (First)
            {
                Ret = 0xFF;
                Ts = (byte)TsType.TsResOctet;
                DLen = 0x04;
                ID = id;
                Index = index;
            }
            else
            {
                Ret = 0x0A;
                Ts = 0x00;
                DLen = 0x0000;
                ID = 0x0000;
                Index = 0x0000;
            }
        }
    }
}
