package com.its.op.entity.its.vms;

import com.its.op.dto.its.vms.TbVmsCtlrDto;
import com.its.op.dto.its.vms.TbVmsCtlrSttsDto;
import com.its.op.global.CodeManager;
import com.its.utils.ItsUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;
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 VARCHAR2(10)
    @Column(name = "VMS_CTLR_ID", length = 10)
    private String vmsCtlrId;

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

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

    @ApiModelProperty("VMS 제어기 포트")  // Y NUMBER(5)
    @Column(name = "VMS_CTLR_PORT", columnDefinition = "NUMBER", length = 5)
    private Integer vmsCtlrPort;

    @ApiModelProperty("VMS 제어기 지역번호")  // Y NUMBER(5)
    @Column(name = "VMS_CTLR_LOCAL_NO", columnDefinition = "NUMBER", length = 5)
    private Integer vmsCtlrLocalNo;

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

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

    @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 CHAR(1)
    @Column(name = "DEL_YN", columnDefinition = "CHAR", length = 1)
    private String delYn;

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

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

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

    @ApiModelProperty("웹카메라 비밀번호")  // Y VARCHAR2(20)
    @Column(name = "WEB_CMRA_PWD", length = 20)
    private String webCmraPwd;

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

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

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

    @ApiModelProperty("기본메시지주기")  // Y NUMBER(3)
    @Column(name = "DEF_PHSE_CHNG_CYCL", columnDefinition = "NUMBER", length = 3)
    private Integer defPhseChngCycl;

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

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

    @ApiModelProperty("전광판 전원 모드(0x00:꺼짐,0x01:켜짐,0x02:자동,0x09:알수없음)")  // Y NUMBER(3)
    @Column(name = "PANL_PWER_MODE", columnDefinition = "NUMBER", length = 3)
    private Integer panlPwerMode;

    @ApiModelProperty("FAN 동작모드(0x00:꺼짐,0x01:켜짐,0x02:자동,0x09:알수없음)")  // Y NUMBER(3)
    @Column(name = "FAN_MODE", columnDefinition = "NUMBER", length = 3)
    private Integer fanMode;

    @ApiModelProperty("팬 동작 온도")  // Y NUMBER(3)
    @Column(name = "FAN_RUN_TMPR", columnDefinition = "NUMBER", length = 3)
    private Integer fanRunTmpr;

    @ApiModelProperty("히터 동작모드(0x00:꺼짐,0x01:켜짐,0x02:자동,0x09:알수없음)")  // Y NUMBER(3)
    @Column(name = "HETR_MODE", columnDefinition = "NUMBER", length = 3)
    private Integer hetrMode;

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

    @ApiModelProperty("휘도 모드(0x00:주간,0x01:야간,0x02:자동,0x09:수동)")  // Y NUMBER(3)
    @Column(name = "BRGH_MODE", columnDefinition = "NUMBER", length = 3)
    private Integer brghMode;

    @ApiModelProperty("휘도 현재 단계(0~100)")  // Y NUMBER(3)
    @Column(name = "BRGH_CURR_STEP", columnDefinition = "NUMBER", length = 3)
    private Integer brghCurrStep;

    @ApiModelProperty("휘도 주간 단계(0~100)")  // Y NUMBER(3)
    @Column(name = "BRGH_WEEK_STEP", columnDefinition = "NUMBER", length = 3)
    private Integer brghWeekStep;

    @ApiModelProperty("휘도 야간 단계(0~100)")  // Y NUMBER(3)
    @Column(name = "BRGH_NGHT_STEP", columnDefinition = "NUMBER", length = 3)
    private Integer brghNghtStep;

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

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

    @ApiModelProperty("웹카메라제조사(삼성카메라'Samsung')")  // Y VARCHAR2(50)
    @Column(name = "WEB_CMRA_CMPY", length = 50)
    private String webCmraCmpy;

    @ApiModelProperty("웹카메라모델")  // Y VARCHAR2(126)
    @Column(name = "WEB_CMRA_MDL", length = 126)
    private String webCmraMdl;

    @ApiModelProperty("설치 위치 주소")  // Y VARCHAR2(200)
    @Column(name = "ISTL_LCTN_ADDR", length = 200)
    private String istlLctnAddr;

    @ApiModelProperty("스트리밍 HTTP 주소")  // Y VARCHAR2(200)
    @Column(name = "STRM_HTTP_ADDR", length = 200)
    private String strmHttpAddr;

    @ApiModelProperty("스트리밍 RTSP 주소")  // Y VARCHAR2(200)
    @Column(name = "STRM_RTSP_ADDR", length = 200)
    private String strmRtspAddr;

    @ApiModelProperty("WEB RTC 서버 아이피")  // Y VARCHAR2(20)
    @Column(name = "WEB_RTC_SVR_IP", length = 20)
    private String webRtcSvrIp;

    @ApiModelProperty("WEB RTC 서버 포트")  // Y NUMBER(5)
    @Column(name = "WEB_RTC_SVR_PORT", columnDefinition = "NUMBER", length = 5)
    private Integer webRtcSvrPort;

    @ApiModelProperty("WEB RTC ID")  // Y VARCHAR2(56)
    @Column(name = "WEB_RTC_ID", length = 56)
    private String webRtcId;

    @ApiModelProperty("CCTV 대체 영상 이미지 번호")  // Y NUMBER(5)
    @Column(name = "ALTT_IMAG_NMBR", columnDefinition = "NUMBER", length = 5)
    private Integer alttImagNmbr;

    @ApiModelProperty("CCTV 대체 표출 여부")  // Y CHAR(1)
    @Column(name = "ALTT_IMAG_DSPL_YN", columnDefinition = "CHAR", length = 1)
    private String alttImagDsplYn;

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

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

    public void setDelete() {
        this.delYn = "Y";
    }

    public TbVmsCtlrDto toDto() {
        TbVmsCtlrDto dto = TbVmsCtlrDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .vmsCtlrId(this.vmsCtlrId)
                .vmsNm(this.vmsNm)
                .vmsCtlrIp(this.vmsCtlrIp)
                .vmsCtlrPort(this.vmsCtlrPort)
                .vmsCtlrLocalNo(this.vmsCtlrLocalNo)
                .vmsTypeCd(this.vmsTypeCd)
                .istlLctnNm(this.istlLctnNm)
                .xCrdn(this.xCrdn)
                .yCrdn(this.yCrdn)
                .delYn(this.delYn)
                .webCmraIp(this.webCmraIp)
                .webCmraPort(this.webCmraPort)
                .webCmraId(this.webCmraId)
                .webCmraPwd(this.webCmraPwd)
                .strmSesnNm(this.strmSesnNm)
                .operMode(this.operMode)
                .vmsMaxPhseNum(this.vmsMaxPhseNum)
                .defPhseChngCycl(this.defPhseChngCycl)
                .panlOnTime(this.panlOnTime)
                .panlOffTime(this.panlOffTime)
                .panlPwerMode(this.panlPwerMode)
                .fanMode(this.fanMode)
                .fanRunTmpr(this.fanRunTmpr)
                .hetrMode(this.hetrMode)
                .hetrRunTmpr(this.hetrRunTmpr)
                .brghMode(this.brghMode)
                .brghCurrStep(this.brghCurrStep)
                .brghWeekStep(this.brghWeekStep)
                .brghNghtStep(this.brghNghtStep)
                .modlErrRate(this.modlErrRate)
                .cmncFailRate(this.cmncFailRate)
                .webCmraCmpy(this.webCmraCmpy)
                .webCmraMdl(this.webCmraMdl)
                .istlLctnAddr(this.istlLctnAddr)
                .strmHttpAddr(this.strmHttpAddr)
                .strmRtspAddr(this.strmRtspAddr)
                .webRtcSvrIp(this.webRtcSvrIp)
                .webRtcSvrPort(this.webRtcSvrPort)
                .webRtcId(this.webRtcId)
                .alttImagNmbr(this.alttImagNmbr)
                .alttImagDsplYn(this.alttImagDsplYn)
                .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 TbVmsCtlrSttsDto toSttsDto() {
        TbVmsCtlrSttsDto dto = TbVmsCtlrSttsDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .collCnt(100)
                .vmsCtlrId(this.vmsCtlrId)
                .vmsNm(this.vmsNm)
                .vmsCtlrId(this.vmsCtlrId)
                .vmsTypeCd(this.vmsTypeCd)
                //.vmsUsagTypeCd(this.vmsUsagTypeCd)
                //.vmsModlTypeCd(this.vmsModlTypeCd)
                .operMode(this.operMode)
//                .vmsCmncErrBassVal(this.vmsCmncErrBassVal)
//                .vmsNghtBrghStep(this.vmsNghtBrghStep)
//                .vmsWeekBrghStep(this.vmsWeekBrghStep)
//                .vmsPhseChngCycl(this.vmsPhseChngCycl)
//                .vmsModlErrRate(this.vmsModlErrRate)
                .vmsMaxPhseNum(this.vmsMaxPhseNum)
//                .fanMtnsTmpr(this.fanMtnsTmpr)
//                .hetrMtnsTmpr(this.hetrMtnsTmpr)
                .panlOnTime(this.panlOnTime)
                .panlOffTime(this.panlOffTime)
                .strmSesnNm(this.strmSesnNm)
                .strmHttpAddr(this.strmHttpAddr)
                .strmRtspAddr(this.strmRtspAddr)
                .updtDt(CodeManager.CMNC_STTS_ERROR_DT)
                .cmncSttsCd(CodeManager.CMNC_STTS_ERROR)
                .provideTime("")
                .provideResult("N")
                .provideCnt(0)
                .autoTotal(0)
                .autoUsed(0)
                .fixedTotal(0)
                .fixedUsed(0)
                .basicTotal(0)
                .basicUsed(0)
                .build();

        if (StringUtils.equals("A", this.operMode)) {
            dto.setOperModeDesc("자동");
        }
        else if (StringUtils.equals("F", this.operMode)) {
            dto.setOperModeDesc("고정");
        }
        else if (StringUtils.equals("B", this.operMode)) {
            dto.setOperModeDesc("기본");
        }
        else {
            dto.setOperModeDesc("[" + this.operMode + "] 알수없음");
        }

        if (this.type != null) {
            dto.setVmsTypeDesc(this.type.getVmsTypeNm());
            dto.setVmsWdth(this.type.getVmsWdth());
            dto.setVmsHght(this.type.getVmsHght());
            dto.setRows(this.type.getRowNum());
            dto.setCols(this.type.getColNum());
            dto.setModlRows(this.type.getModlRowNum());
            dto.setModlCols(this.type.getModlColNum());
            dto.setPowrRows(this.type.getPowrRowNum());
            dto.setPowrCols(this.type.getPowrColNum());
        }

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

            dto.setCmncSttsCd(this.state.getCmncSttsCd());
            dto.setPwerSttsCd(this.state.getPwerSttsCd());
            dto.setModlSttsCd(this.state.getModlSttsCd());
            dto.setCboxDoorSttsCd(this.state.getCboxDoorSttsCd());
            dto.setFanSttsCd(this.state.getFanSttsCd());
            dto.setHetrSttsCd(this.state.getHetrSttsCd());
            dto.setCboxTmpr(this.state.getCboxTmpr());
            dto.setBrghVal(this.state.getBrghVal());
            dto.setModlStts(this.state.getModlStts());
            dto.setPwerStts(this.state.getPwerStts());
            dto.setCommSttsCd(this.state.getCommSttsCd());
        }
        else {
            dto.setPwerSttsDesc(CodeManager.STTS_UNKNOWN);       // 전원상태
            dto.setModlSttsDesc(CodeManager.STTS_UNKNOWN);       // 모듈상태
            dto.setCboxDoorSttsDesc(CodeManager.STTS_UNKNOWN);   // 도어
            dto.setFanSttsDesc(CodeManager.STTS_UNKNOWN);        // 팬상태
            dto.setHetrSttsDesc(CodeManager.STTS_UNKNOWN);       // 히터상태
            dto.setCommSttsDesc(CodeManager.STTS_UNKNOWN);
            dto.setCboxTmpr(255);
            dto.setBrghVal(255);
        }
        dto.setCommSttsDesc(CodeManager.STTS_UNKNOWN);  // 사용안함

        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.setPwerSttsDesc(CodeManager.getCodeDescShort(CodeManager.PWER_STTS_CD, dto.getPwerSttsCd()));           // 전원상태
            dto.setModlSttsDesc(CodeManager.getCodeDescShort(CodeManager.MODL_STTS_CD, dto.getModlSttsCd()));           // 모듈상태
            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.getHetrSttsCd()));           // 히터상태
            dto.setCommSttsDesc(CodeManager.getCodeDescShort(CodeManager.CMNC_STTS_CD, dto.getCommSttsCd()));             // 통신상태
        }
        else {
            dto.setPwerSttsDesc(CodeManager.STTS_UNKNOWN);       // 전원상태
            dto.setModlSttsDesc(CodeManager.STTS_UNKNOWN);       // 모듈상태
            dto.setCboxDoorSttsDesc(CodeManager.STTS_UNKNOWN);   // 도어
            dto.setFanSttsDesc(CodeManager.STTS_UNKNOWN);        // 팬상태
            dto.setHetrSttsDesc(CodeManager.STTS_UNKNOWN);       // 히터상태
            dto.setCommSttsDesc(CodeManager.STTS_UNKNOWN);
            dto.setCboxTmpr(255);
            dto.setBrghVal(255);
        }

        return dto;
    }

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

    public void updateInfo(TbVmsCtlrDto.TbVmsCtlrUpdReq req) {
        this.vmsCtlrId = req.getVmsCtlrId();
        this.vmsNm = req.getVmsNm();
        this.vmsCtlrIp = req.getVmsCtlrIp();
        this.vmsCtlrPort = req.getVmsCtlrPort();
        this.vmsCtlrLocalNo = req.getVmsCtlrLocalNo();
        this.vmsTypeCd = req.getVmsTypeCd();
        this.istlLctnNm = req.getIstlLctnNm();
        this.xCrdn = req.getXCrdn();
        this.yCrdn = req.getYCrdn();
        this.delYn = req.getDelYn();
        this.webCmraIp = req.getWebCmraIp();
        this.webCmraPort = req.getWebCmraPort();
        this.webCmraId = req.getWebCmraId();
        this.webCmraPwd = req.getWebCmraPwd();
        this.strmSesnNm = req.getStrmSesnNm();
        this.operMode = req.getOperMode();
        this.vmsMaxPhseNum = req.getVmsMaxPhseNum();
        this.defPhseChngCycl = req.getDefPhseChngCycl();
        this.panlOnTime = req.getPanlOnTime();
        this.panlOffTime = req.getPanlOffTime();
        this.panlPwerMode = req.getPanlPwerMode();
        this.fanMode = req.getFanMode();
        this.fanRunTmpr = req.getFanRunTmpr();
        this.hetrMode = req.getHetrMode();
        this.hetrRunTmpr = req.getHetrRunTmpr();
        this.brghMode = req.getBrghMode();
        this.brghCurrStep = req.getBrghCurrStep();
        this.brghWeekStep = req.getBrghWeekStep();
        this.brghNghtStep = req.getBrghNghtStep();
        this.modlErrRate = req.getModlErrRate();
        this.cmncFailRate = req.getCmncFailRate();
        this.webCmraCmpy = req.getWebCmraCmpy();
        this.webCmraMdl = req.getWebCmraMdl();
        this.istlLctnAddr = req.getIstlLctnAddr();
        this.strmHttpAddr = req.getStrmHttpAddr();
        this.strmRtspAddr = req.getStrmRtspAddr();
        this.webRtcSvrIp = req.getWebRtcSvrIp();
        this.webRtcSvrPort = req.getWebRtcSvrPort();
        this.webRtcId = req.getWebRtcId();
        this.alttImagNmbr = req.getAlttImagNmbr();
        this.alttImagDsplYn = req.getAlttImagDsplYn();
    }

}
