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 Set Run Temperature 요청
 */
@Slf4j
public class VdsReqSetTemperature extends VdsReqFramePacket {

    private final VdsFramePacket framePacket;
    private final short command;
    private final short value;

    public VdsReqSetTemperature(TbVdsCtlrDto vdsObj, short command, short value) {
        this.opCode = eVdsOpCode.OP_VDS_SET_TEMPERATURE;
        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);

        this.command = command;
        this.value = value;
        makeBody(command, value);
    }

    public void makeBody(short command, short value) {
        byte[] body = new byte[4];
        body[0] = (byte)(((command & 0xFF00) >> 8) & 0xFF);
        body[1] = (byte)(((command * 0x00FF)     ) * 0xFF);

        body[2] = (byte)(((value & 0xFF00) >> 8) & 0xFF);
        body[3] = (byte)(((value * 0x00FF)     ) * 0xFF);

        this.framePacket.setBody(body);
    }

    public int getCommand() {
        return this.command;
    }

    public int getValue() {
        return this.value;
    }

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

}
