package com.its.op.entity.its.cctv;

import com.its.op.dto.its.cctv.TbCctvCtlrDto;
import com.its.op.dto.its.cctv.TbCctvCtlrSttsDto;
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.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

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

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

    @ApiModelProperty("CCTV 관리 번호")  // N NUMBER(10)
    @Id
    @Column(name = "CCTV_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long cctvCtlrNmbr;

    @ApiModelProperty("CCTV 제어기 ID")  // Y VARCHAR2(10)
    @Column(name = "CCTV_CTLR_ID", length = 10)
    private String cctvCtlrId;

    @ApiModelProperty("CCTV 명칭")  // Y VARCHAR2(40)
    @Column(name = "CCTV_NM", length = 40)
    private String cctvNm;

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

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

    @ApiModelProperty("CCTV 제어기 아이피")  // Y VARCHAR2(20)
    @Column(name = "CCTV_CTLR_IP", length = 20)
    private String cctvCtlrIp;

    @ApiModelProperty("CCTV 제어기 포트")  // Y NUMBER(5)
    @Column(name = "CCTV_CTLR_PORT", columnDefinition = "NUMBER", length = 5)
    private Integer cctvCtlrPort;

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

    @ApiModelProperty("CCTV 유형 코드('CCTVT')")  // Y VARCHAR2(7)
    @Column(name = "CCTV_TYPE_CD", length = 7)
    private String cctvTypeCd;

    @ApiModelProperty("CCTV 자막기 아이피")  // Y VARCHAR2(20)
    @Column(name = "CCTV_CAPT_IP", length = 20)
    private String cctvCaptIp;

    @ApiModelProperty("CCTV 자막기 포트")  // Y NUMBER(5)
    @Column(name = "CCTV_CAPT_PORT", columnDefinition = "NUMBER", length = 5)
    private Integer cctvCaptPort;

    @ApiModelProperty("스트리밍 서버 아이피--->분배서버ip")  // Y VARCHAR2(20)
    @Column(name = "STRM_SVR_IP", length = 20)
    private String strmSvrIp;

    @ApiModelProperty("스트리밍 서버 포트--->분배서버port")  // Y NUMBER(5)
    @Column(name = "STRM_SVR_PORT", columnDefinition = "NUMBER", length = 5)
    private Integer strmSvrPort;

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

    @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("스트리밍 RTSP 주소")  // Y VARCHAR2(200)
    @Column(name = "STRM_RTSP_ADDR", length = 200)
    private String strmRtspAddr;

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

    @ApiModelProperty("인터넷 HLS 주소")  // Y VARCHAR2(200)
    @Column(name = "INTR_HTTP_ADDR", length = 200)
    private String intrHttpAddr;

    @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;

    @ApiModelProperty("운영자 제어 여부")  // Y CHAR(1)
    @Column(name = "SYOP_CNTL_YN", columnDefinition = "CHAR", length = 1)
    private String syopCntlYn;

    @ApiModelProperty("링크ID")  // Y NUMBER(10)
    @Column(name = "LINK_ID", columnDefinition = "NUMBER", length = 10)
    private Long linkId;

    @ApiModelProperty("노드ID")  // Y NUMBER(10)
    @Column(name = "NODE_ID", columnDefinition = "NUMBER", length = 10)
    private Long nodeId;

    @ApiModelProperty("공단 CCTV ID")  // Y VARCHAR2(10)
    @Column(name = "CCTV_ID", length = 10)
    private String cctvId;

    @ApiModelProperty("CCTV 지역 구분")  // Y VARCHAR2(7)
    @Column(name = "CCTV_AREA_DVSN", length = 7)
    private String cctvAreaDvsn;

    @ApiModelProperty("사용 여부")  // Y CHAR(1)
    @Column(name = "USE_YN", columnDefinition = "CHAR", length = 1)
    private String useYn;

    @ApiModelProperty("팬 ON 온도")  // Y NUMBER(3)
    @Column(name = "FAN_ON_TMPR", columnDefinition = "NUMBER", length = 3)
    private Integer fanOnTmpr;

    @ApiModelProperty("팬 OFF 온도")  // Y NUMBER(3)
    @Column(name = "FAN_OFF_TMPR", columnDefinition = "NUMBER", length = 3)
    private Integer fanOffTmpr;

    @ApiModelProperty("히터 ON 온도")  // Y NUMBER(3)
    @Column(name = "HETR_ON_TMPR", columnDefinition = "NUMBER", length = 3)
    private Integer hetrOnTmpr;

    @ApiModelProperty("히터 OFF 온도")  // Y NUMBER(3)
    @Column(name = "HETR_OFF_TMPR", columnDefinition = "NUMBER", length = 3)
    private Integer hetrOffTmpr;

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

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

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

    public TbCctvCtlrDto toDto() {
        TbCctvCtlrDto dto = TbCctvCtlrDto.builder()
                .cctvCtlrNmbr(this.cctvCtlrNmbr)
                .cctvCtlrId(this.cctvCtlrId)
                .cctvNm(this.cctvNm)
                .istlLctnNm(this.istlLctnNm)
                .istlLctnAddr(this.istlLctnAddr)
                .cctvCtlrIp(this.cctvCtlrIp)
                .cctvCtlrPort(this.cctvCtlrPort)
                .cctvCtlrLocalNo(this.cctvCtlrLocalNo)
                .cctvTypeCd(this.cctvTypeCd)
                .cctvCaptIp(this.cctvCaptIp)
                .cctvCaptPort(this.cctvCaptPort)
                .strmSvrIp(this.strmSvrIp)
                .strmSvrPort(this.strmSvrPort)
                .strmSesnNm(this.strmSesnNm)
                .xCrdn(this.xCrdn)
                .yCrdn(this.yCrdn)
                .strmRtspAddr(this.strmRtspAddr)
                .strmHttpAddr(this.strmHttpAddr)
                .intrHttpAddr(this.intrHttpAddr)
                .webRtcSvrIp(this.webRtcSvrIp)
                .webRtcSvrPort(this.webRtcSvrPort)
                .webRtcId(this.webRtcId)
                .alttImagNmbr(this.alttImagNmbr)
                .alttImagDsplYn(this.alttImagDsplYn)
                .syopCntlYn(this.syopCntlYn)
                .linkId(this.linkId)
                .nodeId(this.nodeId)
                .cctvId(this.cctvId)
                .cctvAreaDvsn(this.cctvAreaDvsn)
                .useYn(this.useYn)
                .fanOnTmpr(this.fanOnTmpr)
                .fanOffTmpr(this.fanOffTmpr)
                .hetrOnTmpr(this.hetrOnTmpr)
                .hetrOffTmpr(this.hetrOffTmpr)
                .delYn(this.delYn)
                .build();
        dto.setCctvTypeDesc(CodeManager.getCodeDesc(CodeManager.CCTV_TYPE_CD, dto.getCctvTypeCd()));
        return dto;
    }

    public TbCctvCtlrSttsDto toSttsDto() {
        TbCctvCtlrSttsDto dto = TbCctvCtlrSttsDto.builder()
                .cctvCtlrNmbr(this.cctvCtlrNmbr)
                .collCnt(100)
                .cctvCtlrId(this.cctvCtlrId)
                .istlLctnNm(this.istlLctnNm)
                .updtDt(CodeManager.CMNC_STTS_ERROR_DT)
                .cmncSttsCd(CodeManager.CMNC_STTS_ERROR)
                .build();

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

            dto.setCmncSttsCd(this.state.getCmncSttsCd());
            dto.setCboxDoorSttsCd(this.state.getCboxDoorSttsCd());
            dto.setFrontDoorSttsCd(this.state.getFrontDoorSttsCd());
            dto.setBackDoorSttsCd(this.state.getBackDoorSttsCd());
            dto.setFanSttsCd(this.state.getFanSttsCd());
            dto.setHetrSttsCd(this.state.getHetrSttsCd());
            dto.setVideoInput(this.state.getVideoInput());
            dto.setCboxTmpr(this.state.getCboxTmpr());
            dto.setCboxHmdt(this.state.getCboxHmdt());
            dto.setPan(this.state.getPan());
            dto.setTilt(this.state.getTilt());
            dto.setZoom(this.state.getZoom());
            dto.setFocus(this.state.getFocus());

            dto.setCboxDoorSttsDesc(CodeManager.getCodeDescShort(CodeManager.DOOR_STTS_CD, dto.getCboxDoorSttsCd()));
            dto.setFrontDoorSttsDesc(CodeManager.getCodeDescShort(CodeManager.DOOR_STTS_CD, dto.getFrontDoorSttsCd()));
            dto.setBackDoorSttsDesc(CodeManager.getCodeDescShort(CodeManager.DOOR_STTS_CD, dto.getBackDoorSttsCd()));
            dto.setFanSttsDesc(CodeManager.getCodeDescShort(CodeManager.FAN_STTS_CD, dto.getFanSttsCd()));
            dto.setHetrSttsDesc(CodeManager.getCodeDescShort(CodeManager.HETR_STTS_CD, dto.getHetrSttsCd()));
            dto.setVideoInputDesc(CodeManager.getCodeDescShort(CodeManager.VIDEO_INPUT_CD, dto.getVideoInput()));
        }
        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()));

        return dto;
    }

    public TbCctvCtlr(Long cctvCtlrNmbr) {
        this.cctvCtlrNmbr = cctvCtlrNmbr;
    }

    public void updateInfo(TbCctvCtlrDto.TbCctvCtlrUpdReq req) {
        this.cctvCtlrId = req.getCctvCtlrId();
        this.cctvNm = req.getCctvNm();
        this.istlLctnNm = req.getIstlLctnNm();
        this.istlLctnAddr = req.getIstlLctnAddr();
        this.cctvCtlrIp = req.getCctvCtlrIp();
        this.cctvCtlrPort = req.getCctvCtlrPort();
        this.cctvCtlrLocalNo = req.getCctvCtlrLocalNo();
        this.cctvTypeCd = req.getCctvTypeCd();
        this.cctvCaptIp = req.getCctvCaptIp();
        this.cctvCaptPort = req.getCctvCaptPort();
        this.strmSvrIp = req.getStrmSvrIp();
        this.strmSvrPort = req.getStrmSvrPort();
        this.strmSesnNm = req.getStrmSesnNm();
        this.xCrdn = req.getXCrdn();
        this.yCrdn = req.getYCrdn();
        this.strmRtspAddr = req.getStrmRtspAddr();
        this.strmHttpAddr = req.getStrmHttpAddr();
        this.intrHttpAddr = req.getIntrHttpAddr();
        this.webRtcSvrIp = req.getWebRtcSvrIp();
        this.webRtcSvrPort = req.getWebRtcSvrPort();
        this.webRtcId = req.getWebRtcId();
        this.alttImagNmbr = req.getAlttImagNmbr();
        this.alttImagDsplYn = req.getAlttImagDsplYn();
        this.syopCntlYn = req.getSyopCntlYn();
        this.linkId = req.getLinkId();
        this.nodeId = req.getNodeId();
        this.cctvId = req.getCctvId();
        this.cctvAreaDvsn = req.getCctvAreaDvsn();
        this.useYn = req.getUseYn();
        this.fanOnTmpr = req.getFanOnTmpr();
        this.fanOffTmpr = req.getFanOffTmpr();
        this.hetrOnTmpr = req.getHetrOnTmpr();
        this.hetrOffTmpr = req.getHetrOffTmpr();
        this.delYn = req.getDelYn();
    }

}
