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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.common.TrafDto;
import com.its.api.its.model.dto.vms.TbVmsCmtrInfrDto;
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_CMTRINFR")
public class TbVmsCmtrInfr implements Serializable {
    private static final long serialVersionUID = 1L;

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

    @ApiModelProperty("등록 일시")  // Y VARCHAR(14)
    @Column(name = "RGST_DT", length = 14)
    private String rgstDt;

    @ApiModelProperty("속도")  // Y NUMBER(3)
    @Column(name = "SPED", columnDefinition = "NUMBER", length = 3)
    private Integer sped;

    @ApiModelProperty("소통 등급 코드")  // Y VARCHAR(7)
    @Column(name = "CMTR_GRAD_CD", length = 7)
    private String cmtrGradCd;

    @ApiModelProperty("통행 시간")  // Y NUMBER(6)
    @Column(name = "TRVL_HH", columnDefinition = "NUMBER", length = 6)
    private Integer trvlHh;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_IFSC_ID", referencedColumnName = "VMS_IFSC_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsIfsc vmsIfsc = new TbVmsIfsc();

    public TbVmsCmtrInfrDto toDto() {
        TbVmsCmtrInfrDto dto = TbVmsCmtrInfrDto.builder()
                .vmsIfscId(this.vmsIfscId)
                .rgstDt(this.rgstDt)
                .sped(this.sped)
                .cmtrGradCd(this.cmtrGradCd)
                .trvlHh(this.trvlHh)
                .build();

        if (this.vmsIfsc != null) {
            dto.setVmsIfscNm(this.vmsIfsc.getVmsIfscNm());
            dto.setDsplStrtNodeNm(this.vmsIfsc.getDsplStrtNodeNm());
            dto.setDsplEndNodeNm(this.vmsIfsc.getDsplEndNodeNm());
        }

        if (("1").equals(dto.getCmtrGradCd())) {
            dto.setCmtrGradDesc("원활");
        }
        else if (("2").equals(dto.getCmtrGradCd())) {
            dto.setCmtrGradDesc("지체");
        }
        else if (("3").equals(dto.getCmtrGradCd())) {
            dto.setCmtrGradDesc("정체");
        }
        else {
            dto.setCmtrGradDesc("정보없음");
        }
        return dto;
    }

    public TrafDto toTrafDto() {
        TrafDto dto = TrafDto.builder()
                .id(this.vmsIfscId)
                .sped(this.sped)
                .cmtrGradCd("LTC"+this.cmtrGradCd)
                .trvlHh(this.trvlHh)
                .prcnDt(this.rgstDt)
                .build();

        dto.setMissYn(ItsUtils.getMissYn(this.rgstDt, dto.getCmtrGradCd()));
        if (dto.getMissYn().equals("Y")) {
            dto.setSped(0);
            dto.setTrvlHh(0);
            dto.setCmtrGradCd(CodeManager.MISS_SPED_GRAD);
        }
        return dto;
    }

}
