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

import com.its.vms.dto.TbVmsCtlrDto;
import com.its.vms.xnettcp.vms.protocol.VmsFramePacket;
import com.its.vms.xnettcp.vms.protocol.VmsReqFramePacket;
import com.its.vms.xnettcp.vms.protocol.enums.eVmsOpCode;
import com.its.vms.xnettcp.vms.protocol.enums.eVmsProtocolVersion;
import com.its.vms.xnettcp.vms.protocol.impl.dle.VmsDleFramePacket;
import com.its.vms.xnettcp.vms.protocol.impl.std.VmsStdFramePacket;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

/**
 * Stop Image 요청
 */
@Slf4j
public class VmsReqLocalUserCommand extends VmsReqFramePacket {

    private final VmsFramePacket framePacket;
    private final short command;
    private final short value;

    public VmsReqLocalUserCommand(TbVmsCtlrDto vmsObj, short command, short value) {
        this.opCode = eVmsOpCode.OP_VMS_STOP_IMAGE_REQ;
        this.packetNmbr = (short)this.opCode.getValue();
        byte opCode = (byte)this.opCode.getValue();
        if (vmsObj.getProtocolVer() == eVmsProtocolVersion.PROTOCOL_ANDONG.getValue()) {
            this.packetNmbr = vmsObj.getSeqNext();
            this.framePacket = new VmsStdFramePacket(vmsObj.getGroupNo().intValue(), vmsObj.getVmsCtlrNmbr().intValue(), this.packetNmbr, opCode);
        }
        else if (vmsObj.getProtocolVer() == eVmsProtocolVersion.PROTOCOL_EX_FIGURE.getValue()) {
            this.framePacket = new VmsDleFramePacket(vmsObj.getGroupNo().intValue(), vmsObj.getCtlrLocalNo(), opCode);
        }
        else {
            this.framePacket = new VmsDleFramePacket(vmsObj.getGroupNo().intValue(), vmsObj.getCtlrLocalNo(), 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();
    }

}
