package com.its.op.entity.its.node;

import com.its.op.global.CodeManager;
import com.its.op.dto.its.node.TbNodeDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * 노드정보 Entity Class
 */
@Getter
@NoArgsConstructor
@Builder
@AllArgsConstructor
@ApiModel("노드정보")
@Entity
@Table(name = "TB_NODE")
public class TbNode implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("노드ID")  // N NUMBER(10)
    @Id
    @Column(name = "NODE_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long nodeId;

    @ApiModelProperty("노드유형")  // Y VARCHAR(3)
    @Column(name = "NODE_TYPE", length = 3)
    private String nodeType;

    @ApiModelProperty("교차로명칭")  // Y VARCHAR(100)
    @Column(name = "NODE_NAME", length = 100)
    private String nodeName;

    @ApiModelProperty("회전제한유무")  // Y VARCHAR(1)
    @Column(name = "TURN_P", length = 1)
    private String turnP;

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

    @ApiModelProperty("X 좌표")  // Y NUMBER(11,8)
    @Column(name = "X_CRDN", columnDefinition = "NUMBER", length = 11, precision = 8)
    private Double xCrdn;

    @ApiModelProperty("Y 좌표")  // Y NUMBER(10,8)
    @Column(name = "Y_CRDN", columnDefinition = "NUMBER", length = 10, precision = 8)
    private Double yCrdn;

    public TbNodeDto toDto() {
        TbNodeDto dto = TbNodeDto.builder()
                .nodeId(this.nodeId)
                .nodeType(this.nodeType)
                .nodeName(this.nodeName)
                .turnP(this.turnP)
                .rmrk(this.rmrk)
                .xCrdn(this.xCrdn)
                .yCrdn(this.yCrdn)
                .build();

        dto.setNodeTypeDesc(CodeManager.getCodeDesc(CodeManager.NODE_TYPE, this.nodeType));
        if (this.turnP.equals("0"))
            dto.setTurnPDesc("[0] 무");
        else
        if (this.turnP.equals("1"))
            dto.setTurnPDesc("[1] 유");
        else
            dto.setTurnPDesc("[" + this.turnP + "] ?");

        return dto;
    }

    public TbNode(Long nodeId) {
        this.nodeId = nodeId;
    }

    public void updateInfo(TbNodeDto.TbNodeUpdReq req) {
        this.nodeType = req.getNodeType();
        this.nodeName = req.getNodeName();
        this.turnP = req.getTurnP();
        this.rmrk = req.getRmrk();
        this.xCrdn = req.getXCrdn();
        this.yCrdn = req.getYCrdn();
    }

    public void updateName(TbNodeDto.TbNodeNameUpdReq req) {
        this.nodeName = req.getNodeName();
    }

}
