package com.its.op.entity.its.vms;

import com.its.op.dto.its.vms.TbVmsSymbIfscDto;
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;

/**
 * VMS 심벌 라이브러리 셀 구간정보 Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("VMS 심벌 라이브러리 셀 구간정보")
@Entity
@Table(name = "TB_VMS_SYMB_IFSC")
@IdClass(TbVmsSymbIfscKey.class)
public class TbVmsSymbIfsc implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("심벌라이브러리 번호")  // N NUMBER(4)
    @Id
    @Column(name = "SYMB_LIB_NMBR", nullable = false, columnDefinition = "NUMBER", length = 4)
    private Integer symbLibNmbr;

    @ApiModelProperty("셀 ID")  // N NUMBER(4)
    @Id
    @Column(name = "CELL_ID", nullable = false, columnDefinition = "NUMBER", length = 4)
    private Integer cellId;

    @ApiModelProperty("이미지내 셀좌표 X")  // Y NUMBER(4)
    @Column(name = "POSX", columnDefinition = "NUMBER", length = 4)
    private Integer posx;

    @ApiModelProperty("이미지내 셀좌표 Y")  // Y NUMBER(4)
    @Column(name = "POSY", columnDefinition = "NUMBER", length = 4)
    private Integer posy;

    @ApiModelProperty("VMS 정보제공구간 ID")  // Y NUMBER(10)
    @Column(name = "VMS_IFSC_ID", columnDefinition = "NUMBER", length = 10)
    private Long vmsIfscId;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_IFSC_ID", referencedColumnName = "VMS_IFSC_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsIfsc ifsc;

    public TbVmsSymbIfscDto toDto() {
        TbVmsSymbIfscDto dto = TbVmsSymbIfscDto.builder()
                .symbLibNmbr(this.symbLibNmbr)
                .cellId(this.cellId)
                .posx(this.posx)
                .posy(this.posy)
                .vmsIfscId(this.vmsIfscId)
                .build();

        if (this.ifsc != null) {
            dto.setDsplStrtNodeNm(this.ifsc.getDsplStrtNodeNm());
            dto.setDsplEndNodeNm(this.ifsc.getDsplEndNodeNm());
        }
        return dto;
    }

    public TbVmsSymbIfsc(Integer symbLibNmbr, Integer cellId) {
        this.symbLibNmbr = symbLibNmbr;
        this.cellId = cellId;
    }

    public void updateInfo(TbVmsSymbIfscDto.TbVmsSymbIfscUpdReq req) {
        this.posx = req.getPosx();
        this.posy = req.getPosy();
        this.vmsIfscId = req.getVmsIfscId();
    }

}
