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

import com.its.op.dto.its.wcam.TbWcamMonitoringDto;
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_MONITORING")
@IdClass(TbWcamMonitoringKey.class)
public class TbWcamMonitoring 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 VARCHAR2(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 = "WCAM_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long wcamCtlrNmbr;

    @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="WCAM_CTLR_NMBR", referencedColumnName="WCAM_CTLR_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbWcamCtlr wcam;

    public void setType(Integer monitoringType) {
        this.monitoringType = monitoringType;
    }

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

        if (this.wcam != null) {
            dto.setWcamCtlrId(this.wcam.getWcamCtlrId());
            dto.setWcamNm(this.wcam.getWcamNm());
            dto.setWebRtcSvrIp(this.wcam.getWebRtcSvrIp());
            dto.setWebRtcSvrPort(this.wcam.getWebRtcSvrPort());
            dto.setWebRtcId(this.wcam.getWebRtcId());
            dto.setRtcSvrIp(this.wcam.getRtcSvrIp());
            dto.setRtcSvrPort(this.wcam.getRtcSvrPort());
            dto.setRtcId(this.wcam.getRtcId());
        }
        else {
            dto.setWcamCtlrId("-");
            dto.setWcamNm("?");
            dto.setWebRtcSvrIp("");
            dto.setWebRtcSvrPort(0);
            dto.setWebRtcId("");
            dto.setRtcSvrIp("");
            dto.setRtcSvrPort(0);
            dto.setRtcId("");
        }
        return dto;
    }

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

}
