package com.its.op.entity.its.road;

import com.its.op.global.CodeManager;
import com.its.op.dto.its.common.TrafDto;
import com.its.op.dto.its.road.TbRoadTrafDto;
import com.its.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_ROAD_TRAF")
public class TbRoadTraf implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("도로 ID")  // N NUMBER(10)
    @Id
    @Column(name = "ROAD_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long roadId;

    @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="ROAD_ID", referencedColumnName = "ROAD_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbRoad road;

    public TbRoadTrafDto toDto() {
        TbRoadTrafDto dto = TbRoadTrafDto.builder()
                .roadId(this.roadId)
                .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.cmtrGradCd == null) {
            dto.setCmtrGradCd(CodeManager.MISS_SPED_GRAD);
        }
        dto.setMissYn(ItsUtils.getMissYn(this.prcnDt, this.cmtrGradCd));

        if (this.road != null) {
            dto.setRoadName(this.road.getRoadNm());
            dto.setStrtNm(this.road.getStrtNm());
            dto.setEndNm(this.road.getEndNm());
            dto.setDrctCd(this.road.getDrctCd());
            dto.setMissYn(ItsUtils.getMissYn(this.prcnDt, this.cmtrGradCd));
        }
        if (dto.getMissYn().equals("Y")) {
            dto.setCmtrGradCd(CodeManager.MISS_SPED_GRAD);
        }
        dto.setDrctDesc(CodeManager.getCodeDesc(CodeManager.DRCT_CD, dto.getDrctCd()));
        if (this.cmtrGradCd == null) {
            dto.setCmtrGradCd(CodeManager.MISS_SPED_GRAD);
        }
        dto.setCmtrGradDesc(CodeManager.getCodeDesc(CodeManager.CMTR_GRAD_CD, this.cmtrGradCd));

        return dto;
    }

    public TrafDto toTrafDto() {
        TrafDto dto = TrafDto.builder()
                .id(this.roadId)
                .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;
    }

}
