using PlcSiemens.Protocol.Common;
using ByteBuffer = PlcSiemens.O.ByteBuffer;

namespace PlcSiemens.Protocol.Szl
{
    public class SzlDataResponse
    {
        public ResultCode Ret { get; set; }
        public byte Ts { get; set; }
        public ushort DLen { get; set; }
        public ushort ID { get; set; }
        public ushort Index { get; set; }
        public ushort ListLen { get; set; }
        public ushort ListCount { get; set; }
        public byte[] Data { get; set; }
        public int ReadLength { get; set; }

        public int RealLength
        { get { return ListLen * ListCount; } }

        public void Build(ByteBuffer buffer)
        {
            Ret = (ResultCode)buffer.PopByte();
            Ts = buffer.PopByte();
            DLen = buffer.PopUshort();
            ID = buffer.PopUshort();
            Index = buffer.PopUshort();
            ListLen = buffer.PopUshort();
            ListCount = buffer.PopUshort();
            ReadLength = buffer.WriteIndex - buffer.ReadIndex;
            Data = new byte[RealLength];
            buffer.PopBytes(Data, 0, ReadLength);
        }

        public void AddNextData(byte[] data, ushort length)
        {
            Array.Copy(data, 0, Data, ReadLength, length);
            ReadLength += length;
        }
    }
}
