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

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

/**
 * VMS 모니터링 화면 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("VMS 모니터링 화면")
@Entity
@Table(name = "TB_VMS_MONITORING")
@IdClass(TbVmsMonitoringKey.class)
public class TbVmsMonitoring implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("모니터링유형(1:현시,2,시나리오,3,영상)")  // 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("VMS 제어기 번호")  // N NUMBER(10)
    @Column(name = "VMS_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long vmsCtlrNmbr;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(insertable=false, updatable=false, name="VMS_CTLR_NMBR", referencedColumnName="VMS_CTLR_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsCtlr vms = new TbVmsCtlr();

    public TbVmsMonitoringDto toDto() {
        TbVmsMonitoringDto dto = TbVmsMonitoringDto.builder()
                .monitoringType(this.monitoringType)
                .monitoringNm(this.monitoringNm)
                .monitoringSeq(this.monitoringSeq)
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .build();

        if (this.vms != null) {
            dto.setVmsCtlrId(this.vms.getVmsCtlrId());
            dto.setVmsNm(this.vms.getVmsNm());
            dto.setStrmSesnNm(this.vms.getStrmSesnNm());
        }
        else {
            dto.setVmsCtlrId("-");
            dto.setVmsNm("?");
            dto.setStrmSesnNm("");
        }

        return dto;
    }

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

    public void updateInfo(TbVmsMonitoringDto.TbVmsMonitoringUpdReq req) {
        this.vmsCtlrNmbr = req.getVmsCtlrNmbr();
    }

}
