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

import com.its.api.its.model.dto.cctv.TbCctvScnrDto;
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_SCNR")
@IdClass(TbCctvScnrKey.class)
public class TbCctvScnr implements Serializable {
    private static final long serialVersionUID = 1L;

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

    @ApiModelProperty("시작 시각")  // N VARCHAR(6)
    @Id
    @Column(name = "STRT_HMS", nullable = false, length = 6)
    private String strtHms;

    @ApiModelProperty("종료 시각")  // N VARCHAR(6)
    @Id
    @Column(name = "END_HMS", nullable = false, length = 6)
    private String endHms;

    @ApiModelProperty("프리셋 번호")  // Y VARCHAR(3)
    @Column(name = "PSET_NMBR", length = 3)
    private String psetNmbr;

    @ApiModelProperty("시나리오수행시각")  // Y VARCHAR(14)
    @Column(name = "CTRL_DT", length = 14)
    private String ctrlDt;

    @ApiModelProperty("시나리오수행결과(Y:성공, N:실패)")  // Y CHAR(1)
    @Column(name = "CTRL_RES_YN", columnDefinition = "CHAR", length = 1)
    private String ctrlResYn;

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

    @OneToOne
    @JoinColumns( {
        @JoinColumn(updatable=false, insertable=false, name="CCTV_MNGM_NMBR", referencedColumnName="CCTV_MNGM_NMBR"),
        @JoinColumn(updatable=false, insertable=false, name="PSET_NMBR", referencedColumnName="PSET_NMBR")
    })
    @NotFound(action = NotFoundAction.IGNORE)
    private TbCctvPset pset = new TbCctvPset();

    public void initEndHms() {
        if (this.endHms == null || this.endHms.length() != 6) {
            this.endHms = "000000";
        }
    }

    public TbCctvScnrDto toDto() {
        return TbCctvScnrDto.builder()
                .cctvMngmNmbr(this.cctvMngmNmbr)
                .strtHms(this.strtHms)
                .endHms(this.endHms)
                .psetNmbr(this.psetNmbr)
                .ctrlDt(this.ctrlDt)
                .ctrlResYn(this.ctrlResYn)
                .build();
    }

    public TbCctvScnr(Long cctvMngmNmbr, String strtHms, String endHms) {
        this.cctvMngmNmbr = cctvMngmNmbr;
        this.strtHms = strtHms;
        this.endHms = endHms;
    }

    public void updateInfo(TbCctvScnrDto.TbCctvScnrUpdReq req) {
        //this.strtHms = req.getStrtHms();
        //this.endHms = req.getEndHms();
        this.psetNmbr = req.getPsetNmbr();
    }

}
