package com.its.op.entity.its.incident;

import com.its.op.dto.its.incident.TbIncdEstmSectDto;
import com.its.op.entity.its.link.TbLink;
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_INCD_ESTM_SECT")
@IdClass(TbIncdEstmSectKey.class)
public class TbIncdEstmSect implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("링크ID")  // N NUMBER(10)
    @Id
    @Column(name = "LINK_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long linkId;

    @ApiModelProperty("검지 일시")  // N VARCHAR(14)
    @Id
    @Column(name = "DETC_DT", nullable = false, length = 14)
    private String detcDt;

    @ApiModelProperty("수집 속도 평균")  // Y NUMBER(3)
    @Column(name = "CLCT_SPED_AVRG", columnDefinition = "NUMBER", length = 3)
    private Integer clctSpedAvrg;

    @ApiModelProperty("예상 속도 평균")  // Y NUMBER(3)
    @Column(name = "ESTM_SPED_AVRG", columnDefinition = "NUMBER", length = 3)
    private Integer estmSpedAvrg;

    @ApiModelProperty("사실 여부")  // Y CHAR(1)
    @Column(name = "REAL_YN", columnDefinition = "CHAR", length = 1)
    private String realYn;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="LINK_ID", referencedColumnName = "LINK_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    TbLink link;

    public TbIncdEstmSectDto toDto() {
        TbIncdEstmSectDto dto = TbIncdEstmSectDto.builder()
                .linkId(this.linkId)
                .detcDt(this.detcDt)
                .clctSpedAvrg(this.clctSpedAvrg)
                .estmSpedAvrg(this.estmSpedAvrg)
                .realYn(this.realYn)
                .build();

        if (this.link != null) {
            dto.setRoadName(this.link.getRoadName());
            dto.setFromNodeInfo(this.link.getFromNode() == null ? "" : this.link.getFromNode().getNodeName());
            dto.setToNodeInfo(this.link.getToNode() == null ? "" : this.link.getToNode().getNodeName());
            dto.setXCrdn(this.link.getFromNode() == null ? 0. : this.link.getFromNode().getXCrdn());
            dto.setYCrdn(this.link.getFromNode() == null ? 0. : this.link.getFromNode().getYCrdn());
        }

        return dto;
    }

    public TbIncdEstmSect(Long linkId, String detcDt) {
        this.linkId = linkId;
        this.detcDt = detcDt;
    }

    public void updateInfo(TbIncdEstmSectDto.TbIncdEstmSectUpdReq req) {
        this.clctSpedAvrg = req.getClctSpedAvrg();
        this.estmSpedAvrg = req.getEstmSpedAvrg();
        this.realYn = req.getRealYn();
    }

}
