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;

/**
 * 폼 데이터 표출 요청
 */
@Slf4j
public class VmsReqFormDataDisplay extends VmsReqFramePacket {

    private final VmsFramePacket framePacket;

    public VmsReqFormDataDisplay(TbVmsCtlrDto vmsObj, byte[] schedule) {
        this.opCode = eVmsOpCode.OP_VMS_FORM_DATA_DISPLAY;
        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);
        }

        makeBody(schedule);
    }

    public void makeBody(byte[] schedule) {
        byte[] body = new byte[schedule.length];
        System.arraycopy(schedule, 0, body, 0, body.length);
        this.framePacket.setBody(body);
    }

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

}
