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

import com.its.app.utils.SysUtils;
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.enums.eVmsStatusControl;
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;
import java.nio.ByteOrder;

/**
 * Status Control 요청
 */
@Slf4j
public class VmsReqStatusControl extends VmsReqFramePacket {

    private final VmsFramePacket framePacket;

    private byte[] body;
    public VmsReqStatusControl(TbVmsCtlrDto vmsObj) {
        this.opCode = eVmsOpCode.OP_VMS_STATUS_CONTROL;
        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);
        }
    }

    public void controlSignboardPower(byte onOff) {
        this.body = new byte[2];
        this.body[0] = (byte) eVmsStatusControl.CONTROL_SIGNBOARD_POWER.getByteValue();
        this.body[1] = onOff;
        this.framePacket.setBody(this.body);
    }
    public void controlReset() {
        this.body = new byte[2];
        this.body[0] = (byte) eVmsStatusControl.CONTROL_RESET.getByteValue();
        this.body[1] = 0x00;
        this.framePacket.setBody(this.body);
    }
    public void controlRetryCount(byte count) {
        this.body = new byte[2];
        this.body[0] = (byte) eVmsStatusControl.CONTROL_RETRY_COUNT.getByteValue();
        this.body[1] = (byte)(count & 0xFF);
        this.framePacket.setBody(this.body);
    }
    public void controlSystemTime() {
        String sysTime = SysUtils.getSysTime();
        byte[] tempTime = sysTime.getBytes();

        this.body = new byte[1 + tempTime.length];

        ByteBuffer buffer = ByteBuffer.allocate(this.body.length);
        buffer.order(ByteOrder.BIG_ENDIAN);

        buffer.put((byte) eVmsStatusControl.CONTROL_SYSTEM_TIME.getByteValue());
        buffer.put(tempTime);

        buffer.get(this.body, 0, this.body.length);

        this.framePacket.setBody(this.body);
    }
    public void controlBlinkCycle(byte count) {
        this.body = new byte[2];
        this.body[0] = (byte) eVmsStatusControl.CONTROL_BLINK_CYCLE.getByteValue();
        this.body[1] = (byte)(count & 0xFF);
        this.framePacket.setBody(this.body);
    }
    public void controlBright(byte type, byte value) {
        this.body = new byte[3];
        this.body[0] = (byte) eVmsStatusControl.CONTROL_BRIGHT.getByteValue();
        this.body[1] = (byte)(type & 0xFF);
        this.body[2] = (byte)(value & 0xFF);
        this.framePacket.setBody(this.body);
    }
    public void controlFan(byte type, byte temp) {
        this.body = new byte[3];
        this.body[0] = (byte) eVmsStatusControl.CONTROL_FAN.getByteValue();
        this.body[1] = (byte)(type & 0xFF);
        this.body[2] = (byte)(temp & 0xFF);
        this.framePacket.setBody(this.body);
    }
    public void controlHeater(byte type, byte temp) {
        this.body = new byte[3];
        this.body[0] = (byte) eVmsStatusControl.CONTROL_HEATER.getByteValue();
        this.body[1] = (byte)(type & 0xFF);
        this.body[2] = (byte)(temp & 0xFF);
        this.framePacket.setBody(this.body);
    }
    public void controlScreenColor(byte color) {
        // 0x01:Red, 0x02:Green, 0x03:Amber
        this.body = new byte[2];
        this.body[0] = (byte) eVmsStatusControl.CONTROL_SCREEN_COLOR.getByteValue();
        this.body[1] = (byte)(color & 0xFF);
        this.framePacket.setBody(this.body);
    }
    public void controlModuleErrorRatio(byte percent) {
        this.body = new byte[2];
        this.body[0] = (byte) eVmsStatusControl.CONTROL_MODULE_ERROR_RATIO.getByteValue();
        this.body[1] = (byte)(percent & 0xFF);
        this.framePacket.setBody(this.body);
    }
    public void controlDefaultScenarioRunTime(byte seconds) {
        this.body = new byte[2];
        this.body[0] = (byte) eVmsStatusControl.CONTROL_DEFAULT_SCENARIO_RUN_TIME.getByteValue();
        this.body[1] = (byte)(seconds & 0xFF);
        this.framePacket.setBody(this.body);
    }

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

}
