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

import com.its.vds.dto.TbVdsCtlrDto;
import com.its.vds.xnettcp.vds.process.TcpServerSendData;
import com.its.vds.xnettcp.vds.process.response.VdsResponse;
import com.its.vds.xnettcp.vds.protocol.VdsFramePacket;
import com.its.vds.xnettcp.vds.protocol.enums.eVdsOpCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@Slf4j
@RequiredArgsConstructor
public class VdsResDataSpeed implements VdsResponse {

    private final TbVdsCtlrDto vdsObj;
    private final VdsFramePacket resFramePacket;
    private int moduleCnt;
    private String stts;

    @Override
    public boolean process() {

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

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

        // Byte.toUnsignedInt(
        ByteBuffer byteBuffer = ByteBuffer.wrap(body);
        byteBuffer.order(ByteOrder.BIG_ENDIAN);
        this.moduleCnt = byteBuffer.get(0) & 0xFF;
        if (this.moduleCnt <= 0) {
            log.error("VdsResDataSpeed.process: VDS {}, Module Count Error {}.", this.vdsObj.getVdsCtlrNmbr(), this.moduleCnt);
            return false;
        }
        if (body.length < this.moduleCnt + 2) {
            log.error("VdsResDataSpeed.process: VDS {}, Module Count and Body Length Error {}, {}.", this.vdsObj.getVdsCtlrNmbr(), 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();

        if (this.vdsObj.getDebug().isDebug()) {
            log.info("{}", toString());
        }

//        this.vdsObj.getStts().updatePowerModuleStts(this.stts);

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

    public String toString() {
        StringBuilder data = new StringBuilder();
        data.append(String.format("RESPONSE-%s. VDS %d", eVdsOpCode.OP_VDS_DATA_VEHICLE, this.vdsObj.getVdsCtlrNmbr()));
        data.append(System.getProperty("line.separator"));
        data.append(String.format("Power Modules (%d), %s", this.moduleCnt, this.stts));
        return data.toString();
    }

}
