package com.its.vms.entity;

import com.its.app.utils.SysUtils;
import lombok.Data;

import java.io.Serializable;

/**
 *  DTO Class
 */
@Data
public class TbVmsCtlrStts implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long vmsCtlrNmbr;
    private String updtDt;
    private String cmncSttsCd;      // 통신 상태 코드('CMS')
    private String pwerSttsCd;      // 전원 상태 코드('PWS')
    private String modlSttsCd;      // 모듈(LED) 상태 코드
    private String cboxDoorSttsCd;  // 함체 문 상태 코드('CDS')
    private String fanSttsCd;       // 팬 상태 코드('PAS')
    private String hetrSttsCd;      // 히터 상태 코드('HTS')
    private Integer cboxTmpr;       // 함체 온도
    private Integer brghVal;        // 휘도값
    private String commSttsCd;      // 통신 상태 코드('CMS')
    private String modlStts;        // 모듈상태(문자열, 0: 꺼짐, 1: 켜짐, 9: 알수없음)
    private String pwerStts;        // 전원상태(문자열, 0: 꺼짐, 1: 켜짐, 9: 알수없음)

    public TbVmsCtlrStts(Long vmsCtlrNmbr) {
        this.vmsCtlrNmbr = vmsCtlrNmbr;
        initUnknown();
    }

    public TbVmsCtlrStts clone() {
        TbVmsCtlrStts dto = new TbVmsCtlrStts(this.vmsCtlrNmbr);
        dto.setUpdtDt(this.updtDt);
        dto.setCmncSttsCd(this.cmncSttsCd);
        dto.setPwerSttsCd(this.pwerSttsCd);
        dto.setModlSttsCd(this.modlSttsCd);
        dto.setCboxDoorSttsCd(this.cboxDoorSttsCd);
        dto.setFanSttsCd(this.fanSttsCd);
        dto.setHetrSttsCd(this.hetrSttsCd);
        dto.setCboxTmpr(this.cboxTmpr);
        dto.setBrghVal(this.brghVal);
        dto.setCommSttsCd(this.commSttsCd);
        dto.setModlStts(this.modlStts);
        dto.setPwerStts(this.pwerStts);
        return dto;
    }
    public void initStts(boolean isConnected) {
        if (isConnected) {
            initUnknown();
        } else {
            initError();
        }
    }
    public void initUnknown() {
        this.updtDt = SysUtils.getSysTime();
        this.cmncSttsCd = "CMS2";
        this.pwerSttsCd = "PWS2";
        this.modlSttsCd = "MOS2";
        this.cboxDoorSttsCd = "CDS2";
        this.fanSttsCd = "PAS2";
        this.hetrSttsCd = "HTS2";
        this.cboxTmpr = 0;
        this.brghVal = 0;
        this.commSttsCd = "CMS2";
        this.modlStts = "11111111111111111111";
        this.pwerStts = "11111111111111111111";
    }
    public void initError() {
        initUnknown();
        this.cmncSttsCd = "CMS1";
        this.commSttsCd = "CMS1";
    }

    public void initNormal() {
        initUnknown();
        this.cmncSttsCd = "CMS0";
        this.commSttsCd = "CMS0";
    }

    public void updateStts(TbVmsCtlrStts dto) {
        this.updtDt = dto.getUpdtDt();
        this.cmncSttsCd = dto.getCmncSttsCd();
        this.pwerSttsCd = dto.getPwerSttsCd();
        this.modlSttsCd = dto.getModlSttsCd();
        this.cboxDoorSttsCd = dto.getCboxDoorSttsCd();
        this.fanSttsCd = dto.getFanSttsCd();
        this.hetrSttsCd = dto.getHetrSttsCd();
        this.cboxTmpr = dto.getCboxTmpr();
        this.brghVal = dto.getBrghVal();
        this.commSttsCd = dto.getCommSttsCd();
        this.modlStts = dto.getModlStts();
        this.pwerStts = dto.getPwerStts();
    }
/*
    private int door;       // Door Open/Close 상태, 0x00: Open, 0x01: Close, 0x09: Unknown
    private int power;      // 전원의 ON/OFF 상태, 0x00: On, 0x01: Off, 0x09: Unknown (Sign Board)
    private int fan;        // VMS Fan 동작 상태, 0x00: On, 0x01: Off, 0x09: Unknown
    private int heater;     // VMS Heater 동작 상태, 0x00: On, 0x01: Off, 0x09: Unknown
    private int formNo;     // 표출 폼 번호, 현재 표출중인 폼 번호 : 0～9999
    private int reboot;     // 표출 폼 번호, 0x00: 정상, 0x01: 재실행
    private int cboxTemp;   // 함체온도, 127 ～ -127, -128 : Unknown
    private int brightMode; // 화면의 밝기 - 휘도 모드, 0x00:주간, 0x01:야간, 0x02:Auto, 0x03:수동
    private int brightCurr; // 화면의 밝기 - 현재 휘도값, 0～100
    private int brightWeek; // 화면의 밝기 - 주간 휘도값, 0～100
    private int brightNght; // 화면의 밝기 - 야간 휘도값, 0～100
    private int dpTemp;     // 외부온도, -127 ～ 127, -128: Unknown
    private int dpHum;      // 외부습도,    0 ～ 100,  101: Unknown
    private int powerModuleStts = 0x02; // 0x00: 정상, 0x01: 에러, 0x02: Unknown
    private int moduleStts = 0x02;      // 0x00: 정상, 0x01: 불량, 0x02: Unknown
    private int cboxFan = 0x09;         // 0x00: On, 0x01: Off, 0x09: Unknown
    private int cboxHeather = 0x09;     // 0x00: On, 0x01: Off, 0x09: Unknown

 */
    public boolean updateStatus(int door, int power, int fan, int heater, int formNo, int reboot, int cboxTemp,
                                int brightMode, int brightCurr, int brightWeek, int brightNght, int dpTemp, int dpHum,
                                int powerModuleStts, int moduleStts, int cboxFan, int cboxHeather) {
        boolean isDifferent = false;
        this.updtDt = SysUtils.getSysTime();

        int doorSttsCd;
        if (door == 0) doorSttsCd = 1;
        else if (door == 1) doorSttsCd = 0;
        else doorSttsCd = 2;

        if (fan > 1) {
            fan = 2;
        }
        if (heater > 1) {
            heater = 2;
        }

        String pwerSttsCd = "PWS" + powerModuleStts;
        String modlSttsCd = "MOS" + moduleStts;
        String cboxDoorSttsCd = "CDS" + doorSttsCd;
        String fanSttsCd = "PAS" + fan;
        String hetrSttsCd = "HTS" + heater;

        if (!pwerSttsCd.equals(this.pwerSttsCd)) {
            isDifferent = true;
            this.pwerSttsCd = pwerSttsCd;
        }
        if (!modlSttsCd.equals(this.modlSttsCd)) {
            isDifferent = true;
            this.modlSttsCd = modlSttsCd;
        }
        if (!cboxDoorSttsCd.equals(this.cboxDoorSttsCd)) {
            isDifferent = true;
            this.cboxDoorSttsCd = cboxDoorSttsCd;
        }
        if (!fanSttsCd.equals(this.fanSttsCd)) {
            isDifferent = true;
            this.fanSttsCd = fanSttsCd;
        }
        if (!hetrSttsCd.equals(this.hetrSttsCd)) {
            isDifferent = true;
            this.hetrSttsCd = hetrSttsCd;
        }

        if (cboxTemp != this.cboxTmpr) {
            isDifferent = true;
            this.cboxTmpr = cboxTemp;
        }
        if (brightCurr != this.brghVal) {
            isDifferent = true;
            this.brghVal = brightCurr;
        }

        return isDifferent;
    }

    public boolean updatePowerModuleStts(String stts) {
        if (stts.equals(this.pwerStts)) {
            return false;
        }
        this.pwerStts = stts;
        return true;
    }

    public boolean updateModuleStts(String stts) {
        if (stts.equals(this.modlStts)) {
            return false;
        }
        this.modlStts = stts;
        return true;
    }

}
