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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.ifsc.TbIfscDto;
import com.its.api.its.model.entity.node.TbNode;
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
@Builder
@AllArgsConstructor
@ApiModel("정보제공구간")
@Entity
@Table(name = "TB_IFSC")
public class TbIfsc 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(100)
    @Column(name = "IFSC_NM", length = 100)
    private String ifscNm;

    @ApiModelProperty("방향 코드")  // Y VARCHAR(7)
    @Column(name = "DRCT_CD", length = 7)
    private String drctCd;

    @ApiModelProperty("시점 명")  // Y VARCHAR(100)
    @Column(name = "STRT_NM", length = 100)
    private String strtNm;

    @ApiModelProperty("종점 명")  // Y VARCHAR(100)
    @Column(name = "END_NM", length = 100)
    private String endNm;

    @ApiModelProperty("구간 길이")  // Y NUMBER(5)
    @Column(name = "SECT_LNGT", columnDefinition = "NUMBER", length = 5)
    private Integer sectLngt;

    @ApiModelProperty("외부 연계 구간 여부")  // Y CHAR(1)
    @Column(name = "EXTR_CNCT_SECT_YN", columnDefinition = "CHAR", length = 1)
    private String extrCnctSectYn;

    @ApiModelProperty("삭제 여부")  // Y CHAR(1)
    @Column(name = "DEL_YN", columnDefinition = "CHAR", length = 1)
    private String delYn;

    @ApiModelProperty("비고")  // Y VARCHAR(600)
    @Column(name = "RMRK", length = 600)
    private String rmrk;

    @ApiModelProperty("시작 노드 ID")  // Y NUMBER(10)
    @Column(name = "F_NODE_ID", columnDefinition = "NUMBER", length = 10)
    private Long fNodeId;

    @ApiModelProperty("종료 노드 ID")  // Y NUMBER(10)
    @Column(name = "T_NODE_ID", columnDefinition = "NUMBER", length = 10)
    private Long tNodeId;

    @ApiModelProperty("구간 등급 코드")  // Y VARCHAR(7)
    @Column(name = "SECT_GRAD_CD", length = 7)
    private String sectGradCd;

    @ApiModelProperty("지역 코드")  // Y VARCHAR(7)
    @Column(name = "AREA_CD", length = 7)
    private String areaCd;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="F_NODE_ID", referencedColumnName = "NODE_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbNode fromNode = new TbNode();

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="T_NODE_ID", referencedColumnName = "NODE_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbNode toNode = new TbNode();

    public TbIfscDto toDto() {
        TbIfscDto dto = TbIfscDto.builder()
                .ifscId(this.ifscId)
                .ifscNm(this.ifscNm)
                .drctCd(this.drctCd)
                .strtNm(this.strtNm)
                .endNm(this.endNm)
                .sectLngt(this.sectLngt)
                .extrCnctSectYn(this.extrCnctSectYn)
                .delYn(this.delYn)
                .rmrk(this.rmrk)
                .fNodeId(this.fNodeId)
                .tNodeId(this.tNodeId)
                .sectGradCd(this.sectGradCd)
                .areaCd(this.areaCd)
                .build();

        dto.setFromNodeInfo(this.getFromNode().getNodeName() == null ? "" : this.getFromNode().getNodeName());
        dto.setToNodeInfo(this.getToNode().getNodeName() == null ? "" : this.getToNode().getNodeName());

        dto.setDrctDesc(CodeManager.getCodeDesc(CodeManager.DRCT_CD, this.drctCd));
        dto.setSectGradDesc(CodeManager.getCodeDesc(CodeManager.SECT_GRAD_CD, this.sectGradCd));

        return dto;
    }

    public TbIfsc(Long ifscId) {
        this.ifscId = ifscId;
    }

    public void updateInfo(TbIfscDto.TbIfscUpdReq req) {
        this.ifscNm = req.getIfscNm();
        this.drctCd = req.getDrctCd();
        this.strtNm = req.getStrtNm();
        this.endNm = req.getEndNm();
        this.sectLngt = req.getSectLngt();
        this.extrCnctSectYn = req.getExtrCnctSectYn();
        this.delYn = req.getDelYn();
        this.rmrk = req.getRmrk();
        this.fNodeId = req.getFNodeId();
        this.tNodeId = req.getTNodeId();
        this.sectGradCd = req.getSectGradCd();
        this.areaCd = req.getAreaCd();
    }

    public void updateName(TbIfscDto.TbIfscNameUpdReq req) {
        this.ifscNm = req.getIfscNm();
        this.strtNm = req.getStrtNm();
        this.endNm = req.getEndNm();
    }
}
