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

namespace PlcSiemens.Protocol.ListBlocks
{
    public class DataBlockInfoRequest : IBuildRequest
    {
        public HeaderPacket Header { get; set; }
        public ParamsRequest Params { get; set; }
        public DataBlockInfoDataRequest Data { get; set; }

        public BlockType BlockType { get; set; }
        public ushort DB { get; set; }

        public DataBlockInfoRequest()
        {
            Header = new HeaderPacket();
            Params = new ParamsRequest();
            Data = new DataBlockInfoDataRequest();
        }

        public void Build()
        {
            Data.Build(BlockType, DB);
            Params.Build(true, 0x00, GrType.GrBlocksInfo, PduFuncType.SFunBlkInfo);
            Header.Build(PduType.Userdata, Params.PacketLength, Data.PacketLength);
        }

        public ByteBuffer GetBuffer()
        {
            ByteBuffer buffer = ByteBuffer.Allocate();

            Header.GetByteBuffer(buffer);
            Params.GetByteBuffer(buffer);
            Data.GetByteBuffer(buffer);

            return buffer;
        }
    }
}
