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

import com.its.api.its.model.dto.vms.TbVmsLumSetSchDto;
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_LUM_SET_SCH")
@IdClass(TbVmsLumSetSchKey.class)
public class TbVmsLumSetSch implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("VMS 제어기 번호")  // N NUMBER(10)
    @Id
    @Column(name = "VMS_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long vmsCtlrNmbr;

    @ApiModelProperty("설정시각(HH24MI)")  // N VARCHAR(4)
    @Id
    @Column(name = "SCH_TIME", nullable = false, length = 4)
    private String schTime;

    @ApiModelProperty("휘도값(0:자동,1~100)")  // N NUMBER(3)
    @Column(name = "LUMINANCE", nullable = false, columnDefinition = "NUMBER", length = 3)
    private Integer luminance;

    @ApiModelProperty("사용여부(Y:시용,N:사용안함)")  // N CHAR(1)
    @Column(name = "USE_YN", nullable = false, columnDefinition = "CHAR", length = 1)
    private String useYn;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_CTLR_NMBR", referencedColumnName = "VMS_CTLR_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsCtlr vms = new TbVmsCtlr();

    public TbVmsLumSetSchDto toDto() {
        TbVmsLumSetSchDto dto = TbVmsLumSetSchDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .schTime(this.schTime)
                .luminance(this.luminance)
                .useYn(this.useYn)
                .build();

        dto.setUseDesc(("Y").equals(dto.getUseYn()) ? "[Y] 사용" : "[N] 사용안함");

        if (this.vms != null) {
            dto.setVmsId(this.vms.getVmsId());
            dto.setVmsCtlrId(this.vms.getVmsCtlrId());
            dto.setVmsNm(this.vms.getVmsNm());
        }
        return dto;
    }

    public TbVmsLumSetSch(Long vmsCtlrNmbr, String schTime) {
        this.vmsCtlrNmbr = vmsCtlrNmbr;
        this.schTime = schTime;
    }

    public void updateInfo(TbVmsLumSetSchDto.TbVmsLumSetSchUpdReq req) {
        this.luminance = req.getLuminance();
        this.useYn = req.getUseYn();
    }

}
