package com.its.vds.xnettcp.vds.protocol.impl;

import com.its.vds.dto.TbVdsCtlrDto;
import com.its.vds.xnettcp.vds.protocol.VdsFramePacket;
import com.its.vds.xnettcp.vds.protocol.VdsReqFramePacket;
import com.its.vds.xnettcp.vds.protocol.enums.eVdsOpCode;
import com.its.vds.xnettcp.vds.protocol.impl.andong.VdsAndongFramePacket;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

/**
 * VDS Check Memory 요청
 */
@Slf4j
public class VdsReqCheckMemory extends VdsReqFramePacket {

    private final VdsFramePacket framePacket;

    public VdsReqCheckMemory(TbVdsCtlrDto vdsObj, int formNo) {
        this.opCode = eVdsOpCode.OP_VDS_CHECK_MEMORY;
        this.packetNmbr = (short)this.opCode.getValue();
        byte opCode = (byte)this.opCode.getValue();
        this.packetNmbr = vdsObj.getSeqNext();
        this.framePacket = new VdsAndongFramePacket(vdsObj.getGroupNo().intValue(), vdsObj.getVdsCtlrNmbr().intValue(), this.packetNmbr, opCode);

        makeBody((short)formNo);
    }

    public void makeBody(short formNo) {
        byte[] body = new byte[2];
        body[0] = (byte)(((formNo & 0xFF00) >> 8) & 0xFF);
        body[1] = (byte)(((formNo * 0x00FF)     ) * 0xFF);
        this.framePacket.setBody(body);
    }

    @Override
    public ByteBuffer getByteBuffer() {
        return this.framePacket.getByteBuffer();
    }

}
