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 VdsResCheckVersion implements VdsResponse {

    private final TbVdsCtlrDto vdsObj;
    private final VdsFramePacket resFramePacket;
    private int x;
    private int y;
    private String stts;

    @Override
    public boolean process() {

        this.x = 0;
        this.y = 0;
        this.stts = "";

        byte[] body = this.resFramePacket.getBody();
        log.info("VdsResCheckVersion.process: VDS {}, body length: {}.", this.vdsObj.getVdsCtlrNmbr(), body != null ? body.length : 0);
        if (body == null || body.length < 2) {
            log.error("VdsResCheckVersion.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.x = byteBuffer.get(0) & 0xFF;
        this.y = byteBuffer.get(1) & 0xFF;
        if (this.x <= 0 || this.y <= 0) {
            log.error("VdsResCheckVersion.process: VDS {}, Module Count Error {}/{}.", this.vdsObj.getVdsCtlrNmbr(), this.x, this.y);
            return false;
        }
        if (body.length < ((this.x * this.y) + 2)) {
            log.error("VdsResCheckVersion.process: VDS {}, Module Count and Body Length Error {}/{}, {}.", this.vdsObj.getVdsCtlrNmbr(), this.x, this.y, body.length);
            return false;
        }

        int idx = 2;
        StringBuilder data = new StringBuilder();
        for (int ii = 0; ii < this.y; ii++) {
            for (int jj = 0; jj < this.x; jj++) {
                byte status = (byte) (byteBuffer.get(idx++) & 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().updateModuleStts(this.stts);

        // 제어기 파라미터 정보 요청
        return this.vdsObj.addRequestData(new TcpServerSendData(eVdsOpCode.OP_VDS_DATA_IMAGE, null));
    }

    public String toString() {
        StringBuilder data = new StringBuilder();
        data.append(String.format("RESPONSE-%s. VDS %d", eVdsOpCode.OP_VDS_STATUS_VALIDATION, this.vdsObj.getVdsCtlrNmbr()));
        data.append(System.getProperty("line.separator"));
        data.append(String.format("Display Modules (X: %d, Y: %d)", this.x, this.y));
        for (int ii = 0; ii < this.y; ii++) {
            data.append(System.getProperty("line.separator")).append(String.format("Y(%02d): %s", ii, this.stts.substring(ii * this.x, (ii * this.x) + this.x)));
        }
        return data.toString();
    }

}
