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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.vds.TbVdsCtlrSttsHsDto;
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;

/**
 * VDS 제어기 상태 원시 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("VDS 제어기 상태 원시")
@Entity
@Table(name = "TB_VDS_CTLR_STTS_RAW")
@IdClass(TbVdsCtlrSttsHsKey.class)
public class TbVdsCtlrSttsHs implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("제어기 관리 번호")  // N VARCHAR(20)
    @Id
    @Column(name = "CTLR_MNGM_NMBR", nullable = false, length = 20)
    private String ctlrMngmNmbr;

    @ApiModelProperty("발생 일시")  // N VARCHAR(14)
    @Id
    @Column(name = "OCRR_DT", nullable = false, length = 14)
    private String ocrrDt;

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

    @ApiModelProperty("출력 전원 상태 코드")  // Y VARCHAR(7)
    @Column(name = "PRNT_PWER_STTS_CD", length = 7)
    private String prntPwerSttsCd;

    @ApiModelProperty("함체 문 상태 코드")  // Y VARCHAR(7)
    @Column(name = "CBOX_DOOR_STTS_CD", length = 7)
    private String cboxDoorSttsCd;

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

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

    @ApiModelProperty("출력 전원 값")  // Y NUMBER(3)
    @Column(name = "PRNT_PWER_VAL", columnDefinition = "NUMBER", length = 3)
    private Integer prntPwerVal;

    @ApiModelProperty("부 제어기 값")  // Y NUMBER(3)
    @Column(name = "SUB_CTLR_VAL", columnDefinition = "NUMBER", length = 3)
    private Integer subCtlrVal;

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

    @ApiModelProperty("입력 전압")  // Y NUMBER(3)
    @Column(name = "INPT_VOLT", columnDefinition = "NUMBER", length = 3)
    private Integer inptVolt;

    @ApiModelProperty("출력 전압")  // Y NUMBER(3)
    @Column(name = "PRNT_VOLT", columnDefinition = "NUMBER", length = 3)
    private Integer prntVolt;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="CTLR_MNGM_NMBR", referencedColumnName = "CTLR_MNGM_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVdsCtlr vds = new TbVdsCtlr();

    public TbVdsCtlrSttsHsDto toDto() {
        TbVdsCtlrSttsHsDto dto = TbVdsCtlrSttsHsDto.builder()
                .ctlrMngmNmbr(this.ctlrMngmNmbr)
                .ocrrDt(this.ocrrDt)
                .cmncSttsCd(this.cmncSttsCd)
                .prntPwerSttsCd(this.prntPwerSttsCd)
                .cboxDoorSttsCd(this.cboxDoorSttsCd)
                .fanSttsCd(this.fanSttsCd)
                .hetrSttsCd(this.hetrSttsCd)
                .prntPwerVal(this.prntPwerVal)
                .subCtlrVal(this.subCtlrVal)
                .cboxTmpr(this.cboxTmpr)
                .inptVolt(this.inptVolt)
                .prntVolt(this.prntVolt)
                .build();

        dto.setCmncSttsDesc(CodeManager.getCodeDescShort(CodeManager.CMNC_STTS_CD, this.cmncSttsCd));           // 통신상태
        if (CodeManager.CMNC_STTS_NORMAL.equals(dto.getCmncSttsCd())) {
            dto.setPrntPwerSttsDesc(CodeManager.getCodeDescShort(CodeManager.PWER_STTS_CD, this.prntPwerSttsCd));   // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.getCodeDescShort(CodeManager.DOOR_STTS_CD, this.cboxDoorSttsCd));   // 도어
            dto.setFanSttsDesc(CodeManager.getCodeDescShort(CodeManager.FAN_STTS_CD, this.fanSttsCd));              // 팬상태
            dto.setHetrSttsDesc(CodeManager.getCodeDescShort(CodeManager.HETR_STTS_CD, this.hetrSttsCd));           // 히터상태
        }
        else {
            dto.setPrntPwerSttsDesc(CodeManager.STTS_UNKNOWN);      // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.STTS_UNKNOWN);      // 도어
            dto.setFanSttsDesc(CodeManager.STTS_UNKNOWN);           // 팬상태
            dto.setHetrSttsDesc(CodeManager.STTS_UNKNOWN);          // 히터상태
            dto.setCboxTmpr(255);
        }

        if (this.vds != null) {
            dto.setVdsCtlrId(this.vds.getVdsCtlrId());
            dto.setVdsNm(this.vds.getLctn());
        }
        else {
            dto.setVdsCtlrId("-");
            dto.setVdsNm("-");
        }

        return dto;
    }

}
