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

import com.its.api.its.model.dto.cctv.TbCctvMonitoringDto;
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;

/**
 * 웹카메라 모니터링 화면 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("웹카메라 모니터링 화면")
@Entity
@Table(name = "TB_CCTV_MONITORING")
@IdClass(TbCctvMonitoringKey.class)
public class TbCctvMonitoring implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("모니터링유형(1:예비,2,예비,3,영상,4:예비)")  // N NUMBER(3)
    @Id
    @Column(name = "MONITORING_TYPE", nullable = false, columnDefinition = "NUMBER", length = 3)
    private Integer monitoringType;

    @ApiModelProperty("모니터링명칭")  // N VARCHAR(40)
    @Id
    @Column(name = "MONITORING_NM", nullable = false, length = 40)
    private String monitoringNm;

    @ApiModelProperty("모니터링순서")  // N NUMBER(3)
    @Id
    @Column(name = "MONITORING_SEQ", nullable = false, columnDefinition = "NUMBER", length = 3)
    private Integer monitoringSeq;

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

    @ApiModelProperty("화면에 보여지는 방식(0:기본,1:화면맞춤))")  // Y NUMBER(1)
    @Column(name = "VIEW_MODE", columnDefinition = "NUMBER", length = 1)
    private Integer viewMode;

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

    public TbCctvMonitoringDto toDto() {
        TbCctvMonitoringDto dto = TbCctvMonitoringDto.builder()
                .monitoringType(this.monitoringType)
                .monitoringNm(this.monitoringNm)
                .monitoringSeq(this.monitoringSeq)
                .cctvCtlrNmbr(this.cctvCtlrNmbr)
                .viewMode(this.viewMode)
                .build();

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

        return dto;
    }

    public TbCctvMonitoring(Integer monitoringType, String monitoringNm, Integer monitoringSeq) {
        this.monitoringType = monitoringType;
        this.monitoringNm = monitoringNm;
        this.monitoringSeq = monitoringSeq;
    }

    public void updateInfo(TbCctvMonitoringDto.TbCctvMonitoringUpdReq req) {
        this.cctvCtlrNmbr = req.getCctvCtlrNmbr();
        this.viewMode = req.getViewMode();
    }

}
