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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.ifsc.TbIfscLinkRltnDto;
import com.its.api.its.model.entity.link.TbLink;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.persistence.*;
import java.io.Serializable;

/**
 * 정보제공구간 링크 관계 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("정보제공구간 링크 관계")
@Entity
@Table(name = "TB_IFSC_LINK_RLTN")
@IdClass(TbIfscLinkRltnKey.class)
public class TbIfscLinkRltn 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("링크ID")  // N NUMBER(10)
    @Id
    @Column(name = "LINK_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long linkId;

    @ApiModelProperty("순서")  // Y NUMBER(3)
    @Column(name = "ORD", columnDefinition = "NUMBER", length = 3)
    private Integer ord;

    @OneToOne//(fetch = FetchType.EAGER)
    @JoinColumn(insertable=false, updatable=false, name="LINK_ID", referencedColumnName = "LINK_ID")
    TbLink link = new TbLink();

    public TbIfscLinkRltnDto toDto() {

        TbIfscLinkRltnDto dto;
        if (this.link == null) {
            dto = TbIfscLinkRltnDto.builder()
                    .ifscId(this.ifscId)
                    .linkId(this.linkId)
                    .ord(this.ord)
                    .build();
        }
        else {
            dto = TbIfscLinkRltnDto.builder()
                    .ifscId(this.ifscId)
                    .linkId(this.linkId)
                    .ord(this.ord)
                    .fNodeId(this.link.getFNodeId())
                    .tNodeId(this.link.getTNodeId())
                    .lanes(this.link.getLanes())
                    .roadRank(this.link.getRoadRank())
                    .roadType(this.link.getRoadType())
                    .roadNo(this.link.getRoadNo())
                    .roadName(this.link.getRoadName())
                    .linkLeng(this.link.getLinkLeng())
                    .sectGradCd(this.link.getSectGradCd())
                    .fromNodeInfo(this.link.getFromNode() != null ? this.link.getFromNode().getNodeName() : "")
                    .toNodeInfo(this.link.getToNode() != null ? this.link.getFromNode().getNodeName() : "")
                    .build();

            dto.setRoadRankDesc(CodeManager.getCodeDesc(CodeManager.ROAD_RANK, dto.getRoadRank()));
            dto.setRoadTypeDesc(CodeManager.getCodeDesc(CodeManager.ROAD_TYPE, dto.getRoadType()));
            dto.setSectGradDesc(CodeManager.getCodeDesc(CodeManager.SECT_GRAD_CD, dto.getSectGradCd()));

        }

        return dto;
    }

    public TbIfscLinkRltn(Long ifscId, Long linkId) {
        this.ifscId = ifscId;
        this.linkId = linkId;
    }

    public void updateInfo(TbIfscLinkRltnDto.TbIfscLinkRltnUpdReq req) {
        this.ord = req.getOrd();
    }

}
