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

import com.its.op.dto.its.vms.TbVmsCtlrSttsHsDto;
import com.its.op.global.CodeManager;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
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_CTLR_STTS_HS")
@IdClass(TbVmsCtlrSttsHsKey.class)
public class TbVmsCtlrSttsHs implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("생성 일시")  // N VARCHAR2(14)
    @Id
    @Column(name = "CRTN_DT", nullable = false, length = 14)
    private String crtnDt;

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

    @ApiModelProperty("통신 상태 코드('CMS')")  // Y VARCHAR2(7)
    @Column(name = "CMNC_STTS_CD", length = 7)
    private String cmncSttsCd;

    @ApiModelProperty("전원 상태 코드('PWS')-사용안함")  // Y VARCHAR2(7)
    @Column(name = "PWER_STTS_CD", length = 7)
    private String pwerSttsCd;

    @ApiModelProperty("모듈(LED) 상태 코드('MOS')")  // Y VARCHAR2(7)
    @Column(name = "MODL_STTS_CD", length = 7)
    private String modlSttsCd;

    @ApiModelProperty("함체 문 상태 코드('CDS')-앞문/뒷문상태가 모두닫힌경우 닫힘")  // Y VARCHAR2(7)
    @Column(name = "CBOX_DOOR_STTS_CD", length = 7)
    private String cboxDoorSttsCd;

    @ApiModelProperty("팬 상태 코드('PAS')")  // Y VARCHAR2(7)
    @Column(name = "FAN_STTS_CD", length = 7)
    private String fanSttsCd;

    @ApiModelProperty("히터 상태 코드('HTS')")  // Y VARCHAR2(7)
    @Column(name = "HETR_STTS_CD", length = 7)
    private String hetrSttsCd;

    @ApiModelProperty("함체 온도")  // Y NUMBER(5)
    @Column(name = "CBOX_TMPR", columnDefinition = "NUMBER", length = 5)
    private Integer cboxTmpr;

    @ApiModelProperty("휘도값")  // Y NUMBER(3)
    @Column(name = "BRGH_VAL", columnDefinition = "NUMBER", length = 3)
    private Integer brghVal;

    @ApiModelProperty("통신 상태 코드('CMS')")  // Y VARCHAR2(7)
    @Column(name = "COMM_STTS_CD", length = 7)
    private String commSttsCd;

    @ApiModelProperty("모듈상태-사용안함")  // Y VARCHAR2(200)
    @Column(name = "MODL_STTS", length = 200)
    private String modlStts;

    @ApiModelProperty("모듈전원상태-사용안함")  // Y VARCHAR2(200)
    @Column(name = "PWER_STTS", length = 200)
    private String pwerStts;

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

    public TbVmsCtlrSttsHsDto toDto() {
        TbVmsCtlrSttsHsDto dto = TbVmsCtlrSttsHsDto.builder()
                .crtnDt(this.crtnDt)
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .cmncSttsCd(this.cmncSttsCd)
                .pwerSttsCd(this.pwerSttsCd)
                .modlSttsCd(this.modlSttsCd)
                .cboxDoorSttsCd(this.cboxDoorSttsCd)
                .fanSttsCd(this.fanSttsCd)
                .hetrSttsCd(this.hetrSttsCd)
                .cboxTmpr(this.cboxTmpr)
                .brghVal(this.brghVal)
                .commSttsCd(this.commSttsCd)
                .modlStts(this.modlStts)
                .pwerStts(this.pwerStts)
                .build();

        dto.setCmncSttsDesc(CodeManager.getCodeDescShort(CodeManager.CMNC_STTS_CD, dto.getCmncSttsCd()));       // 통신상태
        if (CodeManager.CMNC_STTS_NORMAL.equals(dto.getCmncSttsCd())) {
            dto.setModlSttsDesc(CodeManager.getCodeDescShort(CodeManager.MODL_STTS_CD, dto.getModlSttsCd()));           // 모듈상태
            dto.setPwerSttsDesc(CodeManager.getCodeDescShort(CodeManager.PWER_STTS_CD, dto.getPwerSttsCd()));           // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.getCodeDescShort(CodeManager.DOOR_STTS_CD, dto.getCboxDoorSttsCd()));   // 도어
            dto.setFanSttsDesc(CodeManager.getCodeDescShort(CodeManager.FAN_STTS_CD, dto.getFanSttsCd()));              // 팬상태
            dto.setHetrSttsDesc(CodeManager.getCodeDescShort(CodeManager.HETR_STTS_CD, dto.getHetrSttsCd()));            // 히터상태
        }
        else {
            dto.setModlSttsDesc(CodeManager.STTS_UNKNOWN);       // 모듈상태
            dto.setPwerSttsDesc(CodeManager.STTS_UNKNOWN);       // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.STTS_UNKNOWN);   // 도어
            dto.setFanSttsDesc(CodeManager.STTS_UNKNOWN);        // 팬상태
            dto.setHetrSttsDesc(CodeManager.STTS_UNKNOWN);       // 히터상태
            dto.setCboxTmpr(255);
        }

        if (this.vms != null) {
            dto.setVmsCtlrId(this.vms.getVmsCtlrId());
            dto.setVmsNm(this.vms.getVmsNm());
        }
        else {
            dto.setVmsCtlrId("-");
            dto.setVmsNm("?");
        }

        return dto;
    }

}
