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

namespace PlcSiemens.Protocol.ListBlocks
{
    public class DataBlockOfTypeDataRequest
    {
        public byte Ret { get; set; }  // 0xFF
        public byte Size { get; set; }    // Octet (0x09)
        public ushort Length { get; set; }   // 0x0002
        public byte Zero { get; set; }    // Ascii '0' (0x30)
        public BlockType BlockType { get; set; }
        public bool First { get; private set; }

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

        public void Build(bool first, BlockType blockType)
        {
            First = first;
            if (first)
            {
                Ret = 0xFF;
                Size = (byte)TsType.TsResOctet;
                Length = 0x0002;
                Zero = 0x30;
                BlockType = blockType;
            }
            else
            {
                Ret = 0x0A;
                Size = 0x00;
                Length = 0x0000;
                Zero = 0x00;
                BlockType = 0x00;
            }
        }

        public void GetByteBuffer(ByteBuffer buffer)
        {
            buffer.Push(Ret);
            buffer.Push(Size);
            buffer.Push(Length);
            //如果是第一次需要添加Zero和BlockType
            if (!First) return;

            buffer.Push(Zero);
            buffer.Push((byte)BlockType);
        }
    }
}
