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

import com.its.api.its.model.dto.vms.TbVmsDsplHsDto;
import com.its.api.its.model.dto.vms.TbVmsDsplHsImageDto;
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_HS")
@IdClass(TbVmsDsplHsKey.class)
public class TbVmsDsplHs 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("표출 일시")  // N VARCHAR(14)
    @Id
    @Column(name = "DSPL_DT", nullable = false, length = 14)
    private String dsplDt;

    @ApiModelProperty("표출 PHASE 개수")  // N NUMBER(2)
    @Id
    @Column(name = "DSPL_PHASE_NUM", nullable = false, columnDefinition = "NUMBER", length = 2)
    private Integer dsplPhaseNum;

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

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

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

    @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("심벌라이브러리 번호")  // Y NUMBER(4)
    @Column(name = "SYMBLIB_NMBR", columnDefinition = "NUMBER", length = 4)
    private Integer symblibNmbr;

    @ApiModelProperty("VMS FORM 유형 코드")  // Y NUMBER(3)
    @Column(name = "VMS_FORM_TYPE_CD", columnDefinition = "NUMBER", length = 3)
    private Integer vmsFormTypeCd;

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

    @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 TbVmsDsplHsDto toDto() {
        TbVmsDsplHsDto dto = TbVmsDsplHsDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .dsplDt(this.dsplDt)
                .dsplPhaseNum(this.dsplPhaseNum)
                .vmsDsplMsgData(this.vmsDsplMsgData)
                .vmsDsplMsgTxt(this.vmsDsplMsgTxt)
                .vmsMsgDsplDrctCd(this.vmsMsgDsplDrctCd)
                .vmsMsgDsplMthdCd(this.vmsMsgDsplMthdCd)
                .dsplHh(this.dsplHh)
                .symblibNmbr(this.symblibNmbr)
                .vmsFormTypeCd(this.vmsFormTypeCd)
                .trfcSituTypeCd(this.trfcSituTypeCd)
                .vmsFormId(this.vmsFormId)
                .vmsIfscId(this.vmsIfscId)
                .incdOcrrId(this.incdOcrrId)
                .detrId(this.detrId)
                .dnldYn(this.dnldYn)
                .build();

        if (this.vms != null) {
            dto.setVmsCtlrId(this.vms.getVmsCtlrId());
            dto.setVmsNm(this.vms.getVmsNm());
        }
        else {
            dto.setVmsCtlrId("-");
            dto.setVmsNm("?");
        }
        dto.setDnldDesc(("Y").equals(dto.getDnldYn()) ? "성공" : "실패");

        try {
            String sFormType;
            switch(Integer.parseInt(dto.getTrfcSituTypeCd())) {
                case 11: sFormType = "소통상황(1단)";       break;
                case 12: sFormType = "소통상황(2단)";       break;
                case 13: sFormType = "소통상황(3단)";       break;
                case 14: sFormType = "소통상황(4단)";       break;
                case 15: sFormType = "도형식배경소통정보";  break;
                case 20: sFormType = "돌발문안";            break;
                case 30: sFormType = "공사/행사문안";       break;
                case 40: sFormType = "홍보문안";            break;
                case 50: sFormType = "우회도로";            break;
                case 60: sFormType = "재난안전";            break;
                default:
                    sFormType = "[" + dto.getTrfcSituTypeCd() + "] -?-"; break;
            }
            dto.setTrfcSituTypeDesc(sFormType);
        }
        catch (Exception e) {
            dto.setTrfcSituTypeDesc("[" + dto.getTrfcSituTypeCd() + "] -?-");
        }
        return dto;
    }

    public TbVmsDsplHsImageDto toImageDto() {
        TbVmsDsplHsImageDto dto = TbVmsDsplHsImageDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .dsplDt(this.dsplDt)
                .dsplPhaseNum(this.dsplPhaseNum)
                .vmsDsplMsgData(this.vmsDsplMsgData)
                .dsplHh(this.dsplHh)
                .dnldYn(this.dnldYn)
                .build();

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

        return dto;
    }

}
