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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.common.TrafDto;
import com.its.api.its.model.dto.ifsc.TbIfscTrafDto;
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;

/**
 * 정보제공구간 소통상황 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("정보제공구간 소통상황")
@Entity
@Table(name = "TB_IFSC_TRAF")
public class TbIfscTraf implements Serializable {
    private static final long serialVersionUID = 1L;

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

    @ApiModelProperty("가공 일시")  // Y VARCHAR(14)
    @Column(name = "PRCN_DT", length = 14)
    private String prcnDt;

    @ApiModelProperty("교통량")  // Y NUMBER(6)
    @Column(name = "TFVL", columnDefinition = "NUMBER", length = 6)
    private Integer tfvl;

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

    @ApiModelProperty("패턴 속도")  // Y NUMBER(3)
    @Column(name = "PTRN_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer ptrnSped;

    @ApiModelProperty("점유 율")  // Y NUMBER(5,2)
    @Column(name = "OCPY_RATE", columnDefinition = "NUMBER", length = 5, precision = 2)
    private Double ocpyRate;

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

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

    @ApiModelProperty("데이터 개수")  // Y NUMBER(3)
    @Column(name = "DATA_NUM", columnDefinition = "NUMBER", length = 3)
    private Integer dataNum;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="IFSC_ID", referencedColumnName = "IFSC_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbIfsc ifsc = new TbIfsc();

    public TbIfscTrafDto toDto() {
        TbIfscTrafDto dto = TbIfscTrafDto.builder()
                .ifscId(this.ifscId)
                .prcnDt(this.prcnDt)
                .tfvl(this.tfvl)
                .sped(this.sped)
                .ptrnSped(this.ptrnSped)
                .ocpyRate(this.ocpyRate)
                .trvlHh(this.trvlHh)
                .cmtrGradCd(this.cmtrGradCd)
                .dataNum(this.dataNum)
                .build();

        if (this.ifsc != null) {
            dto.setIfscNm(this.ifsc.getIfscNm());
            dto.setStrtNm(this.ifsc.getStrtNm());
            dto.setEndNm(this.ifsc.getEndNm());
            dto.setDrctCd(this.ifsc.getDrctCd());
            dto.setMissYn(ItsUtils.getMissYn(this.prcnDt, this.cmtrGradCd));
        }
        dto.setDrctDesc(CodeManager.getCodeDesc(CodeManager.DRCT_CD, dto.getDrctCd()));
        dto.setCmtrGradDesc(CodeManager.getCodeDesc(CodeManager.CMTR_GRAD_CD, this.cmtrGradCd));

        return dto;
    }

    public TrafDto toTrafDto() {
        TrafDto dto = TrafDto.builder()
                .id(this.ifscId)
                .sped(this.sped)
                .cmtrGradCd(this.cmtrGradCd)
                .trvlHh(this.trvlHh)
                .build();

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

}
