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

import com.its.api.its.model.dto.vms.TbVmsMsgDsplSchDto;
import com.its.api.its.model.dto.vms.TbVmsMsgDsplSchVmsListDto;
import com.its.api.utils.ItsUtils;
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_TRFC_INFR_DSPL_SCH")
@IdClass(TbVmsMsgDsplSchKey.class)
public class TbVmsMsgDsplSch implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("VMS 제어기 번호")  // N NUMBER(10)
    @Id
    @Column(name = "VMS_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long vmsCtlrNmbr;

    @ApiModelProperty("스케줄 유형 코드(0:교통정보,1:돌발,2:공사/행사문안,3:홍보문안,4:우회도로,5:고정스케줄, 6:기본스케줄)")  // N VARCHAR(7)
    @Id
    @Column(name = "TRFC_SITU_TYPE_CD", nullable = false, length = 7)
    private String trfcSituTypeCd;

    @ApiModelProperty("PHASE")  // N NUMBER(2)
    @Id
    @Column(name = "PHASE", nullable = false, columnDefinition = "NUMBER", length = 2)
    private Integer phase;

    @ApiModelProperty("VMS FORM ID")  // Y NUMBER(5)
    @Column(name = "VMS_FORM_ID", columnDefinition = "NUMBER", length = 5)
    private Integer vmsFormId;

    @ApiModelProperty("1단 VMS 정보제공구간 ID")  // Y NUMBER(10)
    @Column(name = "FRST_VMS_IFSC_ID", columnDefinition = "NUMBER", length = 10)
    private Long frstVmsIfscId;

    @ApiModelProperty("2단 VMS 정보제공구간 ID")  // Y NUMBER(10)
    @Column(name = "SECD_VMS_IFSC_ID", columnDefinition = "NUMBER", length = 10)
    private Long secdVmsIfscId;

    @ApiModelProperty("VMS FORM 우선순위 코드")  // Y VARCHAR(3)
    @Column(name = "VMS_FORM_PRRT_CD", length = 3)
    private String vmsFormPrrtCd;

    @ApiModelProperty("표출 시간")  // Y NUMBER(3)
    @Column(name = "DSPL_HH", columnDefinition = "NUMBER", length = 3)
    private Integer dsplHh;

    @ApiModelProperty("표출 시작 시간")  // Y VARCHAR(14)
    @Column(name = "DSPL_STRT_HH", length = 14)
    private String dsplStrtHh;

    @ApiModelProperty("표출 종료 시간")  // Y VARCHAR(14)
    @Column(name = "DSPL_END_HH", length = 14)
    private String dsplEndHh;

    @ApiModelProperty("3단 VMS 정보제공구간 ID")  // Y NUMBER(10)
    @Column(name = "THIR_VMS_IFSC_ID", columnDefinition = "NUMBER", length = 10)
    private Long thirVmsIfscId;

    @ApiModelProperty("4단 VMS 정보제공구간 ID")  // Y NUMBER(10)
    @Column(name = "FOUR_VMS_IFSC_ID", columnDefinition = "NUMBER", length = 10)
    private Long fourVmsIfscId;

    @ApiModelProperty("사용여부(Y:시용,N:사용안함)")  // Y CHAR(1)
    @Column(name = "USE_YN", columnDefinition = "CHAR", length = 1)
    private String useYn;

    @ApiModelProperty("1단 이미지 정보제공구간 ID")  // Y NUMBER(10)
    @Column(name = "FRST_IMG_IFSC_ID", columnDefinition = "NUMBER", length = 10)
    private Long frstImgIfscId;

    @ApiModelProperty("2단 이미지 정보제공구간 ID")  // Y NUMBER(10)
    @Column(name = "SECD_IMG_IFSC_ID", columnDefinition = "NUMBER", length = 10)
    private Long secdImgIfscId;

    @ApiModelProperty("3단 이미지 정보제공구간 ID")  // Y NUMBER(10)
    @Column(name = "THIR_IMG_IFSC_ID", columnDefinition = "NUMBER", length = 10)
    private Long thirImgIfscId;

    @ApiModelProperty("4단 이미지 정보제공구간 ID")  // Y NUMBER(10)
    @Column(name = "FOUR_IMG_IFSC_ID", columnDefinition = "NUMBER", length = 10)
    private Long fourImgIfscId;

    public void setPhase(int phase) {
        this.phase = phase;
    }

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_FORM_ID", referencedColumnName = "VMS_FORM_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsForm form = new TbVmsForm();

    public TbVmsMsgDsplSchDto toDto() {
        TbVmsMsgDsplSchDto dto = TbVmsMsgDsplSchDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .trfcSituTypeCd(this.trfcSituTypeCd)
                .phase(this.phase)
                .vmsFormId(this.vmsFormId)
                .frstVmsIfscId(this.frstVmsIfscId)
                .secdVmsIfscId(this.secdVmsIfscId)
                .vmsFormPrrtCd(this.vmsFormPrrtCd)
                .dsplHh(this.dsplHh)
                .dsplStrtHh(this.dsplStrtHh)
                .dsplEndHh(this.dsplEndHh)
                .thirVmsIfscId(this.thirVmsIfscId)
                .fourVmsIfscId(this.fourVmsIfscId)
                .useYn(this.useYn)
                .frstImgIfscId(this.frstImgIfscId)
                .secdImgIfscId(this.secdImgIfscId)
                .thirImgIfscId(this.thirImgIfscId)
                .fourImgIfscId(this.fourImgIfscId)
                .build();

        if (this.form != null) {
            dto.setVmsFormImag(ItsUtils.convertBmpToPng(this.form.getVmsFormImag()));
            dto.setImagFmt("png");
        }
        return dto;
    }
    public TbVmsMsgDsplSchVmsListDto toVmsListDto() {
        return TbVmsMsgDsplSchVmsListDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .trfcSituTypeCd(this.trfcSituTypeCd)
                .phase(this.phase)
                .vmsFormId(this.vmsFormId)
                .dsplHh(this.dsplHh)
                .dsplStrtHh(this.dsplStrtHh)
                .dsplEndHh(this.dsplEndHh)
                .useYn(this.useYn)
                .build();
    }

    public TbVmsMsgDsplSch(Long vmsCtlrNmbr, String trfcSituTypeCd, Integer phase) {
        this.vmsCtlrNmbr = vmsCtlrNmbr;
        this.trfcSituTypeCd = trfcSituTypeCd;
        this.phase = phase;
    }

    public void updateInfo(TbVmsMsgDsplSchDto.TbVmsTrfcInfrDsplSchUpdReq req) {
        this.vmsFormId = req.getVmsFormId();
        this.frstVmsIfscId = req.getFrstVmsIfscId();
        this.secdVmsIfscId = req.getSecdVmsIfscId();
        this.vmsFormPrrtCd = req.getVmsFormPrrtCd();
        this.dsplHh = req.getDsplHh();
        this.dsplStrtHh = req.getDsplStrtHh();
        this.dsplEndHh = req.getDsplEndHh();
        this.thirVmsIfscId = req.getThirVmsIfscId();
        this.fourVmsIfscId = req.getFourVmsIfscId();
        this.useYn = req.getUseYn();
        this.frstImgIfscId = req.getFrstImgIfscId();
        this.secdImgIfscId = req.getSecdImgIfscId();
        this.thirImgIfscId = req.getThirImgIfscId();
        this.fourImgIfscId = req.getFourImgIfscId();
    }

}
