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

import com.its.op.global.CodeManager;
import com.its.op.dto.its.cctv.TbCctvCtlrSttsHsDto;
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_STTS_HS")
@IdClass(TbCctvCtlrSttsHsKey.class)
public class TbCctvCtlrSttsHs implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("생성 시각")  // N VARCHAR2(14)
    @Id
    @Column(name = "CRTN_DT", nullable = false, length = 14)
    private String crtnDt;

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

    @ApiModelProperty("통신 상태 코드")  // Y VARCHAR2(7)
    @Column(name = "CMNC_STTS_CD", length = 7)
    private String cmncSttsCd;

    @ApiModelProperty("함체 문 상태 코드('CDS')-앞문/뒷문상태가 모두닫힌경우 닫힘")  // Y VARCHAR2(7)
    @Column(name = "CBOX_DOOR_STTS_CD", length = 7)
    private String cboxDoorSttsCd;

    @ApiModelProperty("앞문 상태 코드('CDS')")  // Y VARCHAR2(7)
    @Column(name = "FRONT_DOOR_STTS_CD", length = 7)
    private String frontDoorSttsCd;

    @ApiModelProperty("뒷문 상태 코드('CDS')")  // Y VARCHAR2(7)
    @Column(name = "BACK_DOOR_STTS_CD", length = 7)
    private String backDoorSttsCd;

    @ApiModelProperty("팬 상태 코드('PAS')")  // Y VARCHAR2(7)
    @Column(name = "FAN_STTS_CD", length = 7)
    private String fanSttsCd;

    @ApiModelProperty("히터 상태 코드('HTS')")  // Y VARCHAR2(7)
    @Column(name = "HETR_STTS_CD", length = 7)
    private String hetrSttsCd;

    @ApiModelProperty("Video Input 여부('VDI')")  // Y VARCHAR2(7)
    @Column(name = "VIDEO_INPUT", length = 7)
    private String videoInput;

    @ApiModelProperty("함체 온도")  // Y NUMBER(3)
    @Column(name = "CBOX_TMPR", columnDefinition = "NUMBER", length = 3)
    private Integer cboxTmpr;

    @ApiModelProperty("함체 습도")  // Y NUMBER(3)
    @Column(name = "CBOX_HMDT", columnDefinition = "NUMBER", length = 3)
    private Integer cboxHmdt;

    @ApiModelProperty("PAN")  // Y NUMBER(6)
    @Column(name = "PAN", columnDefinition = "NUMBER", length = 6)
    private Integer pan;

    @ApiModelProperty("TILT")  // Y NUMBER(6)
    @Column(name = "TILT", columnDefinition = "NUMBER", length = 6)
    private Integer tilt;

    @ApiModelProperty("확대/축소범위")  // Y NUMBER(6)
    @Column(name = "ZOOM", columnDefinition = "NUMBER", length = 6)
    private Integer zoom;

    @ApiModelProperty("FOCUS")  // Y NUMBER(6)
    @Column(name = "FOCUS", columnDefinition = "NUMBER", length = 6)
    private Integer focus;

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

    public TbCctvCtlrSttsHsDto toDto() {
        TbCctvCtlrSttsHsDto dto = TbCctvCtlrSttsHsDto.builder()
                .crtnDt(this.crtnDt)
                .cctvCtlrNmbr(this.cctvCtlrNmbr)
                .cmncSttsCd(this.cmncSttsCd)
                .cboxDoorSttsCd(this.cboxDoorSttsCd)
                .frontDoorSttsCd(this.frontDoorSttsCd)
                .backDoorSttsCd(this.backDoorSttsCd)
                .fanSttsCd(this.fanSttsCd)
                .hetrSttsCd(this.hetrSttsCd)
                .videoInput(this.videoInput)
                .cboxTmpr(this.cboxTmpr)
                .cboxHmdt(this.cboxHmdt)
                .pan(this.pan)
                .tilt(this.tilt)
                .zoom(this.zoom)
                .focus(this.focus)
                .build();

        if (this.cctv != null) {
            dto.setCctvCtlrId(this.cctv.getCctvCtlrId());
            dto.setIstlLctnNm(this.cctv.getIstlLctnNm());
        }
        else {
            dto.setCctvCtlrId("-");
            dto.setIstlLctnNm("?");
        }

        dto.setCmncSttsDesc(CodeManager.getCodeDesc(CodeManager.CMNC_STTS_CD, this.cmncSttsCd));

        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()));

        return dto;
    }

}
