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

import com.its.api.its.model.dto.vms.TbVmsDsplPrstDto;
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_DSPL_PRST")
@IdClass(TbVmsDsplPrstKey.class)
public class TbVmsDsplPrst 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("PHASE")  // N NUMBER(2)
    @Id
    @Column(name = "PHASE", nullable = false, columnDefinition = "NUMBER", length = 2)
    private Integer phase;

    @ApiModelProperty("VMS 표출 메시지 데이터")  // Y VARCHAR(1200)
    @Column(name = "VMS_DSPL_MSG_DATA", length = 1200)
    private String vmsDsplMsgData;

    @ApiModelProperty("VMS 표출 메시지 이미지")  // Y BLOB
    @Lob
    @Column(name = "VMS_DSPL_MSG_IMAG")
    private byte[] vmsDsplMsgImag;

    @ApiModelProperty("VMS 표출 메시지 텍스트")  // Y VARCHAR(400)
    @Column(name = "VMS_DSPL_MSG_TXT", length = 400)
    private String vmsDsplMsgTxt;

    @ApiModelProperty("교통 상황 유형 코드")  // Y VARCHAR(7)
    @Column(name = "TRFC_SITU_TYPE_CD", length = 7)
    private String trfcSituTypeCd;

    @ApiModelProperty("VMS 메시지 표출 방향 코드")  // N NUMBER(3)
    @Column(name = "VMS_MSG_DSPL_DRCT_CD", nullable = false, columnDefinition = "NUMBER", length = 3)
    private Integer vmsMsgDsplDrctCd;

    @ApiModelProperty("표출 일시")  // Y VARCHAR(14)
    @Column(name = "DSPL_DT", length = 14)
    private String dsplDt;

    @ApiModelProperty("심벌라이브러리 번호")  // Y NUMBER(4)
    @Column(name = "SYMBLIB_NMBR", columnDefinition = "NUMBER", length = 4)
    private Integer symblibNmbr;

    @ApiModelProperty("VMS 메시지 표출 방법 코드")  // Y NUMBER(3)
    @Column(name = "VMS_MSG_DSPL_MTHD_CD", columnDefinition = "NUMBER", length = 3)
    private Integer vmsMsgDsplMthdCd;

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

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

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

    @ApiModelProperty("돌발상황 발생 ID")  // Y VARCHAR(12)
    @Column(name = "INCD_OCRR_ID", length = 12)
    private String incdOcrrId;

    @ApiModelProperty("우회도로 ID")  // Y NUMBER(10)
    @Column(name = "DETR_ID", columnDefinition = "NUMBER", length = 10)
    private Long detrId;

    @ApiModelProperty("제어기다운로드 여부(Y:성공,N:실패)")  // Y CHAR(1)
    @Column(name = "DNLD_YN", columnDefinition = "CHAR", length = 1)
    private String dnldYn;

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

    public TbVmsDsplPrstDto toDto() {
        TbVmsDsplPrstDto dto = TbVmsDsplPrstDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .phase(this.phase)
                .vmsDsplMsgData(this.vmsDsplMsgData)
                .vmsDsplMsgImag(this.vmsDsplMsgImag)
                .vmsDsplMsgTxt(this.vmsDsplMsgTxt)
                .trfcSituTypeCd(this.trfcSituTypeCd)
                .vmsMsgDsplDrctCd(this.vmsMsgDsplDrctCd)
                .dsplDt(this.dsplDt)
                .symblibNmbr(this.symblibNmbr)
                .vmsMsgDsplMthdCd(this.vmsMsgDsplMthdCd)
                .dsplHh(this.dsplHh)
                .vmsFormId(this.vmsFormId)
                .vmsIfscId(this.vmsIfscId)
                .incdOcrrId(this.incdOcrrId)
                .detrId(this.detrId)
                .dnldYn(this.dnldYn)
                .build();

        dto.setVmsDsplMsgImag(ItsUtils.convertBmpToPng(dto.getVmsDsplMsgImag()));
        dto.setImagFmt("png");
        dto.setSvcYn(ItsUtils.getServiceYn(dto.getDsplDt()));

        if (this.vms != null) {
            dto.setOperMode(this.vms.getOperMode());
            if (("A").equals(dto.getOperMode())) {
                dto.setOperModeDesc("자동");
            }
            else if (("F").equals(dto.getOperMode())) {
                dto.setOperModeDesc("고정");
            }
            else { //if (("D").equals(dto.getOperMode())) {
                dto.setOperModeDesc("기본");
            }
        }
        return dto;
    }

}
