package com.its.op.entity.its.vms;

import com.its.op.dto.its.vms.TbVmsDsplPrstDto;
import com.its.utils.ItsUtils;
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;
import java.util.ArrayList;

/**
 * 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("표출 일시")  // Y VARCHAR2(14)
    @Column(name = "DSPL_DT", length = 14)
    private String dsplDt;

    @ApiModelProperty("VMS 스케줄 유형(A:자동,F:고정,B:기본)")  // N VARCHAR2(5)
    @Column(name = "VMS_SCH_TYPE", nullable = false, length = 5)
    private String vmsSchType;

    @ApiModelProperty("VMS 폼 스케쥴 유형(0:교통,1:돌발,2:공사행사,3:홍보,4:우회,5:정체,6:안전,7:동영상,8:스트리밍영상)")  // Y NUMBER(3)
    @Column(name = "VMS_SCH_FORM_TYPE", columnDefinition = "NUMBER", length = 3)
    private Integer vmsSchFormType;

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

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

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

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

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

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

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

    @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;

    public TbVmsDsplPrstDto toDto() {
        TbVmsDsplPrstDto dto = TbVmsDsplPrstDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .dsplDt(this.dsplDt)
                .build();

        dto.setMsgs(new ArrayList<>());
        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("기본");
            }

            dto.setVmsCtlrId(this.vms.getVmsCtlrId());
            dto.setVmsNm(this.vms.getVmsNm());
            dto.setVmsTypeCd(this.vms.getVmsTypeCd());

            if (this.vms.getType() != null) {
                dto.setVmsTypeDesc(this.vms.getType().getVmsTypeNm());
                dto.setVmsWdth(this.vms.getType().getVmsWdth());
                dto.setVmsHght(this.vms.getType().getVmsHght());
                dto.setRows(this.vms.getType().getRowNum());
                dto.setCols(this.vms.getType().getColNum());
                dto.setModlRows(this.vms.getType().getModlRowNum());
                dto.setModlCols(this.vms.getType().getModlColNum());
                dto.setPowrRows(this.vms.getType().getPowrRowNum());
                dto.setPowrCols(this.vms.getType().getPowrColNum());
            }
        }
        return dto;
    }

}
