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

import com.its.app.AppUtils;
import com.its.app.utils.SysUtils;
import com.its.vds.domain.NET;
import com.its.vds.dto.TbVdsCtlrDto;
import com.its.vds.entity.TbVdsCtlrStts;
import com.its.vds.process.DbmsData;
import com.its.vds.process.DbmsDataProcess;
import com.its.vds.process.DbmsDataType;
import com.its.vds.ui.MainUI;
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.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class VdsResStatusTemperature implements VdsResponse {

    private final TbVdsCtlrDto vdsObj;
    private final VdsFramePacket resFramePacket;
    private int cboxTemp;
    private int inputVoltage;
    private int outputVoltage;

    @Override
    public boolean process() {
        // 함체온도 및 입출력전압
        byte[] body = this.resFramePacket.getBody();
        log.info("VdsResStatusTemperature.process: VDS {}, body length: {}.", this.vdsObj.getVdsCtlrNmbr(), body != null ? body.length : 0);
        if (body == null || body.length < 3) {
            log.error("VdsResStatusTemperature.process: VDS {}, body length error: {}.", this.vdsObj.getVdsCtlrNmbr(), body != null ? body.length : 0);
            return false;
        }

        this.cboxTemp = body[0];
        this.inputVoltage = body[1] & 0xFF;
        this.outputVoltage = body[2] & 0xFF;

        this.vdsObj.getStts().setCboxTmpr(this.cboxTemp);
        this.vdsObj.getStts().setInptVolt(this.inputVoltage);
        this.vdsObj.getStts().setOuptVolt(this.outputVoltage);

        if (this.vdsObj.getNetState() != NET.LOGINED) {
            this.vdsObj.setNetState(NET.LOGINED);
            this.vdsObj.getStts().setUpdtDt(SysUtils.getSysTime());
            this.vdsObj.getStts().setCmncSttsCd("CMS0");

            // 제어기 상태정보 DB 업데이트
            List<TbVdsCtlrStts> ctlrSttsList = Collections.synchronizedList(new ArrayList<>());
            ctlrSttsList.add(this.vdsObj.getStts());
            DbmsDataProcess dbmsDataProcess = (DbmsDataProcess) AppUtils.getBean(DbmsDataProcess.class);
            dbmsDataProcess.add(new DbmsData(DbmsDataType.DBMS_DATA_CTLR_STTS, false, ctlrSttsList));
            MainUI mainUI = MainUI.getInstance();
            if (mainUI != null) {
                mainUI.updateCtlrStts(this.vdsObj);
            }
        }

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

    public String toString() {
        StringBuilder data = new StringBuilder();
        data.append(String.format("RESPONSE-%s. VDS %d", eVdsOpCode.OP_VDS_STATUS_TEMPERATURE, this.vdsObj.getVdsCtlrNmbr()));
        data.append(System.getProperty("line.separator"));
        data.append(String.format("온도 %d, 입력전압 %d, 출력전압 %d.", this.cboxTemp, this.inputVoltage, this.outputVoltage));
        return data.toString();
    }

}
