package com.its.vms.xnettcp.vms.process.response.impl;

import com.its.vms.dto.TbVmsCtlrDto;
import com.its.vms.xnettcp.vms.process.TcpServerSendData;
import com.its.vms.xnettcp.vms.process.response.VmsResponse;
import com.its.vms.xnettcp.vms.protocol.VmsFramePacket;
import com.its.vms.xnettcp.vms.protocol.enums.eVmsOpCode;
import com.its.vms.xnettcp.vms.protocol.impl.dle.VmsDleFramePacket;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

@Slf4j
@RequiredArgsConstructor
public class VmsResPowerModuleStatus implements VmsResponse {

    private final TbVmsCtlrDto vmsObj;
    private final VmsFramePacket resFramePacket;
    private int moduleCnt;
    private String stts;

    @Override
    public boolean process() {

        this.moduleCnt = 0;
        this.stts = "";

        byte[] body = this.resFramePacket.getBody();
        log.info("VmsResPowerModuleStatus.process: VMS {}, body length: {}.", this.vmsObj.getVmsCtlrNmbr(), body != null ? body.length : 0);
        if (body == null || body.length < 2) {
            log.error("VmsResPowerModuleStatus.process: VMS {}, body length error: {}.", this.vmsObj.getVmsCtlrNmbr(), body != null ? body.length : 0);
            return false;
        }

        // Byte.toUnsignedInt(
        ByteBuffer byteBuffer = ByteBuffer.wrap(body);
        byteBuffer.order(VmsDleFramePacket.BYTE_ORDER);
        this.moduleCnt = byteBuffer.get(0) & 0xFF;
        if (this.moduleCnt <= 0) {
            log.error("VmsResPowerModuleStatus.process: VMS {}, Module Count Error {}.", this.vmsObj.getVmsCtlrNmbr(), this.moduleCnt);
            return false;
        }
        if (body.length < this.moduleCnt + 2) {
            log.error("VmsResPowerModuleStatus.process: VMS {}, Module Count and Body Length Error {}, {}.", this.vmsObj.getVmsCtlrNmbr(), this.moduleCnt, body.length);
            return false;
        }

        StringBuilder data = new StringBuilder();
        for (int ii = 0; ii < this.moduleCnt; ii++) {
            byte status = (byte) (byteBuffer.get(2+ii) & 0xFF);
            if (status == (byte)0x00) {
                data.append("1");   // ON
            }
            else if (status == (byte)0x01) {
                data.append("0");   // OFF
            }
            else {
                data.append("9");   // 0x02, Unknown
            }
        }
        this.stts = data.toString();

        log.info("{}", toString());

        this.vmsObj.getStts().updatePowerModuleStts(this.stts);

        // 제어기 표출모듈상태 요청
        return this.vmsObj.addRequestData(new TcpServerSendData(eVmsOpCode.OP_VMS_DISPLAY_MODULE_STATUS_REQ, null));
    }

    public String toString() {
        StringBuilder data = new StringBuilder();
        data.append(String.format("RESPONSE-%s. VMS %d", eVmsOpCode.OP_VMS_POWER_MODULE_STATUS_REQ, this.vmsObj.getVmsCtlrNmbr()));
        data.append(System.getProperty("line.separator"));
        data.append(String.format("Power Modules (%d), %s", this.moduleCnt, this.stts));
        return data.toString();
    }

}
