package com.its.api.its.model.entity.vms;

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.vms.TbVmsCtlrDto;
import com.its.api.its.model.dto.vms.TbVmsSttsDto;
import com.its.api.utils.ItsUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.io.Serializable;

/**
 * VMS 제어기 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("VMS 제어기")
@Entity
@Table(name = "TB_VMS_CTLR")
public class TbVmsCtlr implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("VMS 제어기 번호")  // N NUMBER(10)
    @Id
    @Column(name = "VMS_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long vmsCtlrNmbr;

    @ApiModelProperty("VMS ID")  // Y VARCHAR(4)
    @Column(name = "VMS_ID", length = 4)
    private String vmsId;

    @ApiModelProperty("VMS 제어기 ID")  // Y VARCHAR(30)
    @Column(name = "VMS_CTLR_ID", length = 30)
    private String vmsCtlrId;

    @ApiModelProperty("VMS 명")  // Y VARCHAR(40)
    @Column(name = "VMS_NM", length = 40)
    private String vmsNm;

    @ApiModelProperty("VMS 제어기 아이피")  // Y VARCHAR(20)
    @Column(name = "VMS_CTLR_IP", length = 20)
    private String vmsCtlrIp;

    @ApiModelProperty("VMS 용도 유형 코드")  // Y VARCHAR(7)
    @Column(name = "VMS_USAG_TYPE_CD", length = 7)
    private String vmsUsagTypeCd;

    @ApiModelProperty("VMS 형식 유형 코드")  // Y VARCHAR(7)
    @Column(name = "VMS_FRM_TYPE_CD", length = 7)
    private String vmsFrmTypeCd;

    @ApiModelProperty("VMS 유형 코드")  // Y VARCHAR(7)
    @Column(name = "VMS_TYPE_CD", length = 7)
    private String vmsTypeCd;

    @ApiModelProperty("VMS 모듈 유형 코드")  // Y VARCHAR(7)
    @Column(name = "VMS_MODL_TYPE_CD", length = 7)
    private String vmsModlTypeCd;

    @ApiModelProperty("VMS 통신 포트")  // Y VARCHAR(5)
    @Column(name = "VMS_CMNC_PORT", length = 5)
    private String vmsCmncPort;

    @ApiModelProperty("운영 모드")  // Y VARCHAR(7)
    @Column(name = "OPER_MODE", length = 7)
    private String operMode;

    @ApiModelProperty("VMS 통신 오류 기본 값")  // Y NUMBER(3)
    @Column(name = "VMS_CMNC_ERR_BASS_VAL", columnDefinition = "NUMBER", length = 3)
    private Integer vmsCmncErrBassVal;

    @ApiModelProperty("VMS 야간 휘도 단계")  // Y NUMBER(2)
    @Column(name = "VMS_NGHT_BRGH_STEP", columnDefinition = "NUMBER", length = 2)
    private Integer vmsNghtBrghStep;

    @ApiModelProperty("VMS 주간 휘도 단계")  // Y NUMBER(2)
    @Column(name = "VMS_WEEK_BRGH_STEP", columnDefinition = "NUMBER", length = 2)
    private Integer vmsWeekBrghStep;

    @ApiModelProperty("VMS 표출면 변경 주기")  // Y NUMBER(3)
    @Column(name = "VMS_PHSE_CHNG_CYCL", columnDefinition = "NUMBER", length = 3)
    private Integer vmsPhseChngCycl;

    @ApiModelProperty("VMS 모듈 오류 율")  // Y NUMBER(3)
    @Column(name = "VMS_MODL_ERR_RATE", columnDefinition = "NUMBER", length = 3)
    private Integer vmsModlErrRate;

    @ApiModelProperty("통신장애 SLOT 번호")  // Y NUMBER(4)
    @Column(name = "CMNCFAIL_SLOT_NMBR", columnDefinition = "NUMBER", length = 4)
    private Integer cmncfailSlotNmbr;

    @ApiModelProperty("전원 장애 SLOT 번호")  // Y NUMBER(4)
    @Column(name = "PWER_FAIL_SLOT_NMBR", columnDefinition = "NUMBER", length = 4)
    private Integer pwerFailSlotNmbr;

    @ApiModelProperty("삭제 여부")  // Y CHAR(1)
    @Column(name = "DEL_YN", columnDefinition = "CHAR", length = 1)
    private String delYn;

    @ApiModelProperty("소통정보 연계 여부")  // Y CHAR(1)
    @Column(name = "CMTRINFR_CNCT_YN", columnDefinition = "CHAR", length = 1)
    private String cmtrinfrCnctYn;

    @ApiModelProperty("기상정보 연계 여부")  // Y CHAR(1)
    @Column(name = "WTHRINFR_CNCT_YN", columnDefinition = "CHAR", length = 1)
    private String wthrinfrCnctYn;

    @ApiModelProperty("환경정보 연계 여부")  // Y CHAR(1)
    @Column(name = "ENVRINFR_CNCT_YN", columnDefinition = "CHAR", length = 1)
    private String envrinfrCnctYn;

    @ApiModelProperty("VMS 최대 표출면 개수")  // Y NUMBER(2)
    @Column(name = "VMS_MAX_PHSE_NUM", columnDefinition = "NUMBER", length = 2)
    private Integer vmsMaxPhseNum;

    @ApiModelProperty("최초 등록자 번호")  // Y VARCHAR(20)
    @Column(name = "FRST_REGR_NMBR", length = 20)
    private String frstRegrNmbr;

    @ApiModelProperty("최초 등록 일시")  // Y VARCHAR(14)
    @Column(name = "FRST_RGST_DT", length = 14)
    private String frstRgstDt;

    @ApiModelProperty("최종 수정자 번호")  // Y VARCHAR(20)
    @Column(name = "LAST_CRPR_NMBR", length = 20)
    private String lastCrprNmbr;

    @ApiModelProperty("최종 수정 일시")  // Y VARCHAR(14)
    @Column(name = "LAST_CRCT_DT", length = 14)
    private String lastCrctDt;

    @ApiModelProperty("전광판 ON TIME")  // Y VARCHAR(4)
    @Column(name = "PANL_ON_TIME", length = 4)
    private String panlOnTime;

    @ApiModelProperty("전광판 OFF TIME")  // Y VARCHAR(4)
    @Column(name = "PANL_OFF_TIME", length = 4)
    private String panlOffTime;

    @ApiModelProperty("전광판 전원 모드")  // Y VARCHAR(7)
    @Column(name = "PANL_PWER_MODE", length = 7)
    private String panlPwerMode;

    @ApiModelProperty("휘도 모드")  // Y VARCHAR(7)
    @Column(name = "BRGH_MODE", length = 7)
    private String brghMode;

    @ApiModelProperty("웹 카메라 아이피")  // Y VARCHAR(20)
    @Column(name = "WEB_CMRA_IP", length = 20)
    private String webCmraIp;

    @ApiModelProperty("X 좌표")  // Y NUMBER(11,8)
    @Column(name = "X_CRDN", columnDefinition = "NUMBER", length = 11, precision = 8)
    private Double xCrdn;

    @ApiModelProperty("Y 좌표")  // Y NUMBER(10,8)
    @Column(name = "Y_CRDN", columnDefinition = "NUMBER", length = 10, precision = 8)
    private Double yCrdn;

    @ApiModelProperty("설치 위치 명")  // Y VARCHAR(40)
    @Column(name = "ISTL_LCTN_NM", length = 40)
    private String istlLctnNm;

    @ApiModelProperty("교통 전략 사용 여부")  // Y CHAR(1)
    @Column(name = "TRFC_STRG_USE_YN", columnDefinition = "CHAR", length = 1)
    private String trfcStrgUseYn;

    @ApiModelProperty("VMS 위치 정보제공구간 ID")  // Y NUMBER(10)
    @Column(name = "VMS_LOC_IFSC_ID", columnDefinition = "NUMBER", length = 10)
    private Long vmsLocIfscId;

    @ApiModelProperty("팬 동작 온도")  // Y NUMBER(6,3)
    @Column(name = "FAN_MTNS_TMPR", columnDefinition = "NUMBER", length = 6, precision = 3)
    private Integer fanMtnsTmpr;

    @ApiModelProperty("히터 동작 온도")  // Y NUMBER(6,3)
    @Column(name = "HETR_MTNS_TMPR", columnDefinition = "NUMBER", length = 6, precision = 3)
    private Integer hetrMtnsTmpr;

    @ApiModelProperty("웹 카메라 포트")  // Y VARCHAR(5)
    @Column(name = "WEB_CMRA_PORT", length = 5)
    private String webCmraPort;

    @ApiModelProperty("웹 카메라 ID")  // Y VARCHAR(30)
    @Column(name = "WEB_CMRA_ID", length = 30)
    private String webCmraId;

    @ApiModelProperty("웹 카메라 암호")  // Y VARCHAR(64)
    @Column(name = "WEB_CMRA_PWD", length = 64)
    private String webCmraPwd;

    @ApiModelProperty("스트리밍 세션 명")  // Y VARCHAR(200)
    @Column(name = "STRM_SESN_NM", length = 200)
    private String strmSesnNm;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_TYPE_CD", referencedColumnName = "VMS_TYPE_CD")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsType type = new TbVmsType();

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_USAG_TYPE_CD", referencedColumnName = "VMS_USAG_TYPE_CD")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsUsagType usagType = new TbVmsUsagType();

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_CTLR_NMBR", referencedColumnName = "VMS_CTLR_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsStts state = new TbVmsStts();

    public TbVmsCtlrDto toDto() {
        TbVmsCtlrDto dto = TbVmsCtlrDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .vmsId(this.vmsId)
                .vmsCtlrId(this.vmsCtlrId)
                .vmsNm(this.vmsNm)
                .vmsCtlrIp(this.vmsCtlrIp)
                .vmsUsagTypeCd(this.vmsUsagTypeCd)
                .vmsFrmTypeCd(this.vmsFrmTypeCd)
                .vmsTypeCd(this.vmsTypeCd)
                .vmsModlTypeCd(this.vmsModlTypeCd)
                .vmsCmncPort(this.vmsCmncPort)
                .operMode(this.operMode)
                .vmsCmncErrBassVal(this.vmsCmncErrBassVal)
                .vmsNghtBrghStep(this.vmsNghtBrghStep)
                .vmsWeekBrghStep(this.vmsWeekBrghStep)
                .vmsPhseChngCycl(this.vmsPhseChngCycl)
                .vmsModlErrRate(this.vmsModlErrRate)
                .cmncfailSlotNmbr(this.cmncfailSlotNmbr)
                .pwerFailSlotNmbr(this.pwerFailSlotNmbr)
                .delYn(this.delYn)
                .cmtrinfrCnctYn(this.cmtrinfrCnctYn)
                .wthrinfrCnctYn(this.wthrinfrCnctYn)
                .envrinfrCnctYn(this.envrinfrCnctYn)
                .vmsMaxPhseNum(this.vmsMaxPhseNum)
                .frstRegrNmbr(this.frstRegrNmbr)
                .frstRgstDt(this.frstRgstDt)
                .lastCrprNmbr(this.lastCrprNmbr)
                .lastCrctDt(this.lastCrctDt)
                .panlOnTime(this.panlOnTime)
                .panlOffTime(this.panlOffTime)
                .panlPwerMode(this.panlPwerMode)
                .brghMode(this.brghMode)
                .webCmraIp(this.webCmraIp)
                .xCrdn(this.xCrdn)
                .yCrdn(this.yCrdn)
                .istlLctnNm(this.istlLctnNm)
                .trfcStrgUseYn(this.trfcStrgUseYn)
                .vmsLocIfscId(this.vmsLocIfscId)
                .fanMtnsTmpr(this.fanMtnsTmpr)
                .hetrMtnsTmpr(this.hetrMtnsTmpr)
                .webCmraPort(this.webCmraPort)
                .webCmraId(this.webCmraId)
                .webCmraPwd(this.webCmraPwd)
                .strmSesnNm(this.strmSesnNm)
                .build();

        //dto.setVmsUsagTypeDesc(CodeManager.getCodeDescShort(CodeManager.VMS_USAG_TYPE_CD, dto.getVmsUsagTypeCd()));
        //dto.setVmsTypeDesc(CodeManager.getCodeDescShort(CodeManager.VMS_TYPE_CD, dto.getVmsTypeCd()));
        if (this.usagType != null) {
            dto.setVmsUsagTypeDesc("[" + this.usagType.getVmsUsagTypeCd() + "] " + this.usagType.getVmsUsagTypeNm());
        }
        if (this.type != null) {
            dto.setVmsTypeDesc("[" + this.type.getVmsTypeCd() + "] " + this.type.getVmsTypeNm());
        }

        return dto;
    }

    public TbVmsSttsDto toSttsDto() {
        TbVmsSttsDto dto = TbVmsSttsDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .nmbr(this.vmsCtlrNmbr)
                .collCnt(100)
                .vmsCtlrId(this.vmsCtlrId)
                .vmsNm(this.vmsNm)
                .updtDt(CodeManager.CMNC_STTS_ERROR_DT)
                .cmncSttsCd(CodeManager.CMNC_STTS_ERROR)
                .build();

        if (this.state != null) {
            dto.setUpdtDt(this.state.getRgstDt());

            dto.setCmncSttsCd(this.state.getConnSttsCd());
            dto.setPwerStts(this.state.getPwerSttsCd());
            dto.setFanSttsCd(this.state.getFanSttsCd());
            dto.setHetrSttsCd(this.state.getHetrSttsCd());
            dto.setCboxDoorSttsCd(this.state.getCboxdoorOpenSttsCd());
            dto.setModlSttsCd(this.state.getCboxfanSttsCd());
            dto.setCboxTmpr(this.state.getCboxTmpr());
            dto.setModlStts(this.state.getModlStts());
            dto.setPwerStts(this.state.getPwerStts());
        }
        else {
            dto.setModlSttsDesc(CodeManager.STTS_UNKNOWN);       // 모듈상태
            dto.setPwerSttsDesc(CodeManager.STTS_UNKNOWN);       // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.STTS_UNKNOWN);   // 도어
            dto.setFanSttsDesc(CodeManager.STTS_UNKNOWN);        // 팬상태
            dto.setHetrSttsDesc(CodeManager.STTS_UNKNOWN);       // 히터상태
            dto.setCboxTmpr(255);
        }

        dto.setMissSttsYn(ItsUtils.getMissSttsYn(dto.getUpdtDt()));
        if (("Y").equals(dto.getMissSttsYn())) {
            // 기준시각 보다 크기 때문에 상태정보를 통신두절로 설정
            dto.setCmncSttsCd(CodeManager.CMNC_STTS_ERROR);
        }

        dto.setCmncSttsDesc(CodeManager.getCodeDescShort(CodeManager.CMNC_STTS_CD, dto.getCmncSttsCd()));       // 통신상태
        if (CodeManager.CMNC_STTS_NORMAL.equals(dto.getCmncSttsCd())) {
            dto.setModlSttsDesc(CodeManager.getCodeDescShort(CodeManager.MODL_STTS_CD, dto.getModlSttsCd()));           // 모듈상태
            dto.setPwerSttsDesc(CodeManager.getCodeDescShort(CodeManager.PWER_STTS_CD, dto.getPwerSttsCd()));           // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.getCodeDescShort(CodeManager.DOOR_STTS_CD, dto.getCboxDoorSttsCd()));   // 도어
            dto.setFanSttsDesc(CodeManager.getCodeDescShort(CodeManager.FAN_STTS_CD, dto.getFanSttsCd()));              // 팬상태
            dto.setHetrSttsDesc(CodeManager.getCodeDescShort(CodeManager.HETR_STTS_CD, dto.getFanSttsCd()));            // 히터상태
        }
        else {
            dto.setModlSttsDesc(CodeManager.STTS_UNKNOWN);       // 모듈상태
            dto.setPwerSttsDesc(CodeManager.STTS_UNKNOWN);       // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.STTS_UNKNOWN);   // 도어
            dto.setFanSttsDesc(CodeManager.STTS_UNKNOWN);        // 팬상태
            dto.setHetrSttsDesc(CodeManager.STTS_UNKNOWN);       // 히터상태
            dto.setCboxTmpr(255);
        }

        return dto;
    }

    public TbVmsCtlr(Long vmsCtlrNmbr) {
        this.vmsCtlrNmbr = vmsCtlrNmbr;
    }

    public void updateInfo(TbVmsCtlrDto.TbVmsCtlrUpdReq req) {
        this.vmsId = req.getVmsId();
        this.vmsCtlrId = req.getVmsCtlrId();
        this.vmsNm = req.getVmsNm();
        this.vmsCtlrIp = req.getVmsCtlrIp();
        this.vmsUsagTypeCd = req.getVmsUsagTypeCd();
        this.vmsFrmTypeCd = req.getVmsFrmTypeCd();
        this.vmsTypeCd = req.getVmsTypeCd();
        this.vmsModlTypeCd = req.getVmsModlTypeCd();
        this.vmsCmncPort = req.getVmsCmncPort();
        this.operMode = req.getOperMode();
        this.vmsCmncErrBassVal = req.getVmsCmncErrBassVal();
        this.vmsNghtBrghStep = req.getVmsNghtBrghStep();
        this.vmsWeekBrghStep = req.getVmsWeekBrghStep();
        this.vmsPhseChngCycl = req.getVmsPhseChngCycl();
        this.vmsModlErrRate = req.getVmsModlErrRate();
        this.cmncfailSlotNmbr = req.getCmncfailSlotNmbr();
        this.pwerFailSlotNmbr = req.getPwerFailSlotNmbr();
        this.delYn = req.getDelYn();
        this.cmtrinfrCnctYn = req.getCmtrinfrCnctYn();
        this.wthrinfrCnctYn = req.getWthrinfrCnctYn();
        this.envrinfrCnctYn = req.getEnvrinfrCnctYn();
        this.vmsMaxPhseNum = req.getVmsMaxPhseNum();
        this.frstRegrNmbr = req.getFrstRegrNmbr();
        this.frstRgstDt = req.getFrstRgstDt();
        this.lastCrprNmbr = req.getLastCrprNmbr();
        this.lastCrctDt = req.getLastCrctDt();
        this.panlOnTime = req.getPanlOnTime();
        this.panlOffTime = req.getPanlOffTime();
        this.panlPwerMode = req.getPanlPwerMode();
        this.brghMode = req.getBrghMode();
        this.webCmraIp = req.getWebCmraIp();
        this.xCrdn = req.getXCrdn();
        this.yCrdn = req.getYCrdn();
        this.istlLctnNm = req.getIstlLctnNm();
        this.trfcStrgUseYn = req.getTrfcStrgUseYn();
        this.vmsLocIfscId = req.getVmsLocIfscId();
        this.fanMtnsTmpr = req.getFanMtnsTmpr();
        this.hetrMtnsTmpr = req.getHetrMtnsTmpr();
        this.webCmraPort = req.getWebCmraPort();
        this.webCmraId = req.getWebCmraId();
        this.webCmraPwd = req.getWebCmraPwd();
        this.strmSesnNm = req.getStrmSesnNm();
    }

}
