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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.cctv.TbCctvCtlrDto;
import com.its.api.its.model.dto.cctv.TbCctvSttsDto;
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;

/**
 * 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_MNGM_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long cctvMngmNmbr;

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

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

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

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

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

    @ApiModelProperty("CCTV 광장비 아이피")  // Y VARCHAR(20)
    @Column(name = "CCTV_FIBR_IP", length = 20)
    private String cctvFibrIp;

    @ApiModelProperty("CCTV 인코더 아이피")  // Y VARCHAR(20)
    @Column(name = "CCTV_ENCD_IP", length = 20)
    private String cctvEncdIp;

    @ApiModelProperty("스트리밍 서버 아이피")  // Y VARCHAR(20)
    @Column(name = "STRM_SVR_IP", length = 20)
    private String strmSvrIp;

    @ApiModelProperty("스트리밍 서버 포트")  // Y VARCHAR(5)
    @Column(name = "STRM_SVR_PORT", length = 5)
    private String strmSvrPort;

    @ApiModelProperty("스트리밍 세션 명")  // Y VARCHAR(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("설치 위치 명")  // Y VARCHAR(40)
    @Column(name = "ISTL_LCTN_NM", length = 40)
    private String istlLctnNm;

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

    @ApiModelProperty("CCTV 채널")  // Y NUMBER(2)
    @Column(name = "CCTV_CHNL", columnDefinition = "NUMBER", length = 2)
    private Integer cctvChnl;

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

    @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("링크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 VARCHAR(10)
    @Column(name = "CCTV_ID", length = 10)
    private String cctvId;

    @ApiModelProperty("연계 포트")  // Y VARCHAR(5)
    @Column(name = "RELY_PORT", length = 5)
    private String relyPort;

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

    @ApiModelProperty("스트리밍 RTMP 주소")  // Y VARCHAR(200)
    @Column(name = "STRM_RTMP_ADDR", length = 200)
    private String strmRtmpAddr;

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

    @ApiModelProperty("스트리밍 저장 주소")  // Y VARCHAR(200)
    @Column(name = "STRM_STOR_ADDR", length = 200)
    private String strmStorAddr;

    @ApiModelProperty("지역 코드")  // Y VARCHAR(7)
    @Column(name = "AREA_CD", length = 7)
    private String areaCd;

    @ApiModelProperty("CCTV 유형(1:제노시스,2:프로브디지터,3:영국전자)")  // Y VARCHAR(10)
    @Column(name = "CCTV_TYPE", length = 10)
    private String cctvType;

    @ApiModelProperty("CCTV 대체 영상")  // Y BLOB
    @Lob
    @Column(name = "CCTV_SBST_IMGN")
    private byte[] cctvSbstImgn;

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

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

    @ApiModelProperty("내부 RTMP 주소")  // Y VARCHAR(200)
    @Column(name = "INTR_RTMP_ADDR", length = 200)
    private String intrRtmpAddr;

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

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

    public TbCctvCtlrDto toDto() {
        return TbCctvCtlrDto.builder()
                .cctvMngmNmbr(this.cctvMngmNmbr)
                .cctvCtlrId(this.cctvCtlrId)
                .cctvCtlrIp(this.cctvCtlrIp)
                .cctvCtlrPort(this.cctvCtlrPort)
                .cctvCaptIp(this.cctvCaptIp)
                .cctvCaptPort(this.cctvCaptPort)
                .cctvFibrIp(this.cctvFibrIp)
                .cctvEncdIp(this.cctvEncdIp)
                .strmSvrIp(this.strmSvrIp)
                .strmSvrPort(this.strmSvrPort)
                .strmSesnNm(this.strmSesnNm)
                .xCrdn(this.xCrdn)
                .yCrdn(this.yCrdn)
                .istlLctnNm(this.istlLctnNm)
                .istlLctnAddr(this.istlLctnAddr)
                .cctvChnl(this.cctvChnl)
                .delYn(this.delYn)
                .frstRegrNmbr(this.frstRegrNmbr)
                .frstRgstDt(this.frstRgstDt)
                .lastCrprNmbr(this.lastCrprNmbr)
                .lastCrctDt(this.lastCrctDt)
                .linkId(this.linkId)
                .nodeId(this.nodeId)
                .cctvId(this.cctvId)
                .relyPort(this.relyPort)
                .strmRtspAddr(this.strmRtspAddr)
                .strmRtmpAddr(this.strmRtmpAddr)
                .strmHttpAddr(this.strmHttpAddr)
                .strmStorAddr(this.strmStorAddr)
                .areaCd(this.areaCd)
                .cctvType(this.cctvType)
                .cctvSbstImgn(this.cctvSbstImgn)
                .cctvSbstDsplYn(this.cctvSbstDsplYn)
                .cctvAreaDvsn(this.cctvAreaDvsn)
                .intrRtmpAddr(this.intrRtmpAddr)
                .syopCntlYn(this.syopCntlYn)
                .build();
    }

    public TbCctvSttsDto toSttsDto() {
        TbCctvSttsDto dto = TbCctvSttsDto.builder()
                .cctvMngmNmbr(this.cctvMngmNmbr)
                .nmbr(this.cctvMngmNmbr)
                .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.setFanSttsCd(this.state.getFanSttsCd());
            dto.setHetrSttsCd(this.state.getHetrSttsCd());
            dto.setCboxTmpr(this.state.getCboxTmpr());
            dto.setCboxHmdt(this.state.getCboxHmdt());
        }
        dto.setMissSttsYn(ItsUtils.getMissSttsYn(dto.getUpdtDt()));
        if (("Y").equals(dto.getMissSttsYn())) {
            // 기준시각 보다 크기 때문에 상태정보를 통신두절로 설정
            dto.setCmncSttsCd(CodeManager.CMNC_STTS_ERROR);
        }
        dto.setCmncSttsDesc(CodeManager.getCodeDesc(CodeManager.CMNC_STTS_CD, dto.getCmncSttsCd()));

        return dto;
    }

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

    public void updateInfo(TbCctvCtlrDto.TbCctvCtlrUpdReq req) {
        this.cctvCtlrId = req.getCctvCtlrId();
        this.cctvCtlrIp = req.getCctvCtlrIp();
        this.cctvCtlrPort = req.getCctvCtlrPort();
        this.cctvCaptIp = req.getCctvCaptIp();
        this.cctvCaptPort = req.getCctvCaptPort();
        this.cctvFibrIp = req.getCctvFibrIp();
        this.cctvEncdIp = req.getCctvEncdIp();
        this.strmSvrIp = req.getStrmSvrIp();
        this.strmSvrPort = req.getStrmSvrPort();
        this.strmSesnNm = req.getStrmSesnNm();
        this.xCrdn = req.getXCrdn();
        this.yCrdn = req.getYCrdn();
        this.istlLctnNm = req.getIstlLctnNm();
        this.istlLctnAddr = req.getIstlLctnAddr();
        this.cctvChnl = req.getCctvChnl();
        this.delYn = req.getDelYn();
        this.frstRegrNmbr = req.getFrstRegrNmbr();
        this.frstRgstDt = req.getFrstRgstDt();
        this.lastCrprNmbr = req.getLastCrprNmbr();
        this.lastCrctDt = req.getLastCrctDt();
        this.linkId = req.getLinkId();
        this.nodeId = req.getNodeId();
        this.cctvId = req.getCctvId();
        this.relyPort = req.getRelyPort();
        this.strmRtspAddr = req.getStrmRtspAddr();
        this.strmRtmpAddr = req.getStrmRtmpAddr();
        this.strmHttpAddr = req.getStrmHttpAddr();
        this.strmStorAddr = req.getStrmStorAddr();
        this.areaCd = req.getAreaCd();
        this.cctvType = req.getCctvType();
        this.cctvSbstImgn = req.getCctvSbstImgn();
        this.cctvSbstDsplYn = req.getCctvSbstDsplYn();
        this.cctvAreaDvsn = req.getCctvAreaDvsn();
        this.intrRtmpAddr = req.getIntrRtmpAddr();
        this.syopCntlYn = req.getSyopCntlYn();
    }

}
