package com.its.op.entity.its.wcam;

import com.its.op.dto.its.wcam.TbWcamCtlrDto;
import com.its.op.dto.its.wcam.TbWcamCtlrSttsDto;
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;

/**
 * 웹 카메라 Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("웹 카메라")
@Entity
@Table(name = "TB_WCAM_CTLR")
public class TbWcamCtlr implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("웹카메라 제어기번호")  // N NUMBER(10)
    @Id
    @Column(name = "WCAM_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long wcamCtlrNmbr;

    @ApiModelProperty("웹카메라 제어기 ID")  // Y VARCHAR2(30)
    @Column(name = "WCAM_CTLR_ID", length = 30)
    private String wcamCtlrId;

    @ApiModelProperty("웹카메라 명칭")  // Y VARCHAR2(40)
    @Column(name = "WCAM_NM", length = 40)
    private String wcamNm;

    @ApiModelProperty("웹카메라 제어기 아이피")  // Y VARCHAR2(20)
    @Column(name = "WCAM_CTLR_IP", length = 20)
    private String wcamCtlrIp;

    @ApiModelProperty("웹카메라제어기 포트")  // Y VARCHAR2(5)
    @Column(name = "WCAM_CTLR_PORT", length = 5)
    private String wcamCtlrPort;

    @ApiModelProperty("웹카메라 제어기 지역번호")  // Y NUMBER(5)
    @Column(name = "WCAM_CTLR_LOCAL_NO", columnDefinition = "NUMBER", length = 5)
    private Integer wcamCtlrLocalNo;

    @ApiModelProperty("웹카메라 유형")  // Y VARCHAR2(7)
    @Column(name = "WCAM_TYPE_CD", length = 7)
    private String wcamTypeCd;

    @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 = "STRM_SVR_IP", length = 20)
    private String strmSvrIp;

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

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

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

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

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

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

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

    @ApiModelProperty("웹카메라 제어 ID")  // Y VARCHAR2(20)
    @Column(name = "CMRA_ID", length = 20)
    private String cmraId;

    @ApiModelProperty("웹카메라 제어 PSWD")  // Y VARCHAR2(64)
    @Column(name = "CMRA_PSWD", length = 64)
    private String cmraPswd;

    @ApiModelProperty("웹카메라제어 CHANNEL")  // Y NUMBER(5)
    @Column(name = "CMRA_CHNL", columnDefinition = "NUMBER", length = 5)
    private Integer cmraChnl;

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

    @ApiModelProperty("웹카메라 모델")  // Y VARCHAR2(20)
    @Column(name = "WCAM_MODL", length = 20)
    private String wcamModl;

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

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

    @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("RTC 서버 아이피")  // Y VARCHAR2(20)
    @Column(name = "RTC_SVR_IP", length = 20)
    private String rtcSvrIp;

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

    @ApiModelProperty("RTC ID")  // Y VARCHAR2(56)
    @Column(name = "RTC_ID", length = 56)
    private String rtcId;

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

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

    public TbWcamCtlrDto toDto() {
        return TbWcamCtlrDto.builder()
                .wcamCtlrNmbr(this.wcamCtlrNmbr)
                .wcamCtlrId(this.wcamCtlrId)
                .wcamNm(this.wcamNm)
                .wcamCtlrIp(this.wcamCtlrIp)
                .wcamCtlrPort(this.wcamCtlrPort)
                .wcamCtlrLocalNo(this.wcamCtlrLocalNo)
                .wcamTypeCd(this.wcamTypeCd)
                .istlLctnNm(this.istlLctnNm)
                .xCrdn(this.xCrdn)
                .yCrdn(this.yCrdn)
                .delYn(this.delYn)
                .strmSvrIp(this.strmSvrIp)
                .strmSvrPort(this.strmSvrPort)
                .strmSesnNm(this.strmSesnNm)
                .strmRtspAddr(this.strmRtspAddr)
                .strmRtmpAddr(this.strmRtmpAddr)
                .strmHttpAddr(this.strmHttpAddr)
                .strmStorAddr(this.strmStorAddr)
                .cmraPort(this.cmraPort)
                .cmraId(this.cmraId)
                .cmraPswd(this.cmraPswd)
                .cmraChnl(this.cmraChnl)
                .linkId(this.linkId)
                .wcamModl(this.wcamModl)
                .istlLctnAddr(this.istlLctnAddr)
                .fullStrmSesnNm(this.fullStrmSesnNm)
                .webRtcSvrIp(this.webRtcSvrIp)
                .webRtcSvrPort(this.webRtcSvrPort)
                .webRtcId(this.webRtcId)
                .rtcSvrIp(this.rtcSvrIp)
                .rtcSvrPort(this.rtcSvrPort)
                .rtcId(this.rtcId)
                .build();
    }

    public TbWcamCtlrSttsDto toSttsDto() {
        TbWcamCtlrSttsDto dto = TbWcamCtlrSttsDto.builder()
                .wcamCtlrNmbr(this.wcamCtlrNmbr)
                .wcamCtlrId(this.wcamCtlrId)
                .wcamNm(this.wcamNm)
                .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.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 TbWcamCtlr(Long wcamCtlrNmbr) {
        this.wcamCtlrNmbr = wcamCtlrNmbr;
    }

    public void updateInfo(TbWcamCtlrDto.TbWcamCtlrUpdReq req) {
        this.wcamCtlrId = req.getWcamCtlrId();
        this.wcamNm = req.getWcamNm();
        this.wcamCtlrIp = req.getWcamCtlrIp();
        this.wcamCtlrPort = req.getWcamCtlrPort();
        this.wcamCtlrLocalNo = req.getWcamCtlrLocalNo();
        this.wcamTypeCd = req.getWcamTypeCd();
        this.istlLctnNm = req.getIstlLctnNm();
        this.xCrdn = req.getXCrdn();
        this.yCrdn = req.getYCrdn();
        this.delYn = req.getDelYn();
        this.strmSvrIp = req.getStrmSvrIp();
        this.strmSvrPort = req.getStrmSvrPort();
        this.strmSesnNm = req.getStrmSesnNm();
        this.strmRtspAddr = req.getStrmRtspAddr();
        this.strmRtmpAddr = req.getStrmRtmpAddr();
        this.strmHttpAddr = req.getStrmHttpAddr();
        this.strmStorAddr = req.getStrmStorAddr();
        this.cmraPort = req.getCmraPort();
        this.cmraId = req.getCmraId();
        this.cmraPswd = req.getCmraPswd();
        this.cmraChnl = req.getCmraChnl();
        this.linkId = req.getLinkId();
        this.wcamModl = req.getWcamModl();
        this.istlLctnAddr = req.getIstlLctnAddr();
        this.fullStrmSesnNm = req.getFullStrmSesnNm();
        this.webRtcSvrIp = req.getWebRtcSvrIp();
        this.webRtcSvrPort = req.getWebRtcSvrPort();
        this.webRtcId = req.getWebRtcId();
        this.rtcSvrIp = req.getRtcSvrIp();
        this.rtcSvrPort = req.getRtcSvrPort();
        this.rtcId = req.getRtcId();
    }

    public String getCmncSttsCd() {
        String result = "CMS1";
        if (this.state != null) {
            result = this.state.getCmncSttsCd();
        }
        return result;
    }

    public TbWcamCtlrStts getState() {
        if (this.state == null) {
            TbWcamCtlrStts state = TbWcamCtlrStts.builder()
                    .wcamCtlrNmbr(this.wcamCtlrNmbr)
                    .updtDt("19700101000000")
                    .cmncSttsCd(CodeManager.CMNC_STTS_ERROR)
                    .build();
            this.state = state;
        }
        return this.state;
    }
}
