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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.vds.TbVdsCtlrDto;
import com.its.api.its.model.dto.vds.TbVdsCtlrSttsDto;
import com.its.api.utils.ItsUtils;
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")
public class TbVdsCtlr 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("VDS 제어기 ID")  // Y VARCHAR(30)
    @Column(name = "VDS_CTLR_ID", length = 30)
    private String vdsCtlrId;

    @ApiModelProperty("제어기 유형 코드")  // Y VARCHAR(7)
    @Column(name = "CTLR_TYPE_CD", length = 7)
    private String ctlrTypeCd;

    @ApiModelProperty("유효 여부")  // Y CHAR(1)
    @Column(name = "VALD_YN", columnDefinition = "CHAR", length = 1)
    private String valdYn;

    @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;

    @ApiModelProperty("위치")  // Y VARCHAR(100)
    @Column(name = "LCTN", length = 100)
    private String lctn;

    @ApiModelProperty("단말기 아이피")  // Y VARCHAR(20)
    @Column(name = "TRMN_IP", length = 20)
    private String trmnIp;

    @ApiModelProperty("단말기 포트")  // Y VARCHAR(5)
    @Column(name = "TRMN_PORT", length = 5)
    private String trmnPort;

    @ApiModelProperty("제어기 수집 주기")  // Y NUMBER(3)
    @Column(name = "CTLR_CLCT_CYCL", columnDefinition = "NUMBER", length = 3)
    private Integer ctlrClctCycl;

    @ApiModelProperty("제어기 상태 주기")  // Y NUMBER(3)
    @Column(name = "CTLR_STTS_CYCL", columnDefinition = "NUMBER", length = 3)
    private Integer ctlrSttsCycl;

    @ApiModelProperty("루프 보드 개수")  // Y NUMBER(3)
    @Column(name = "LOOP_BORD_NUM", columnDefinition = "NUMBER", length = 3)
    private Integer loopBordNum;

    @ApiModelProperty("삭제자 번호")  // Y VARCHAR(20)
    @Column(name = "DLTR_NMBR", length = 20)
    private String dltrNmbr;

    @ApiModelProperty("삭제 일시")  // Y VARCHAR(14)
    @Column(name = "DEL_DT", length = 14)
    private String delDt;

    @ApiModelProperty("삭제 여부")  // Y CHAR(1)
    @Column(name = "DEL_YN", columnDefinition = "CHAR", length = 1)
    private String delYn;

    @ApiModelProperty("최초 등록자 번호")  // Y VARCHAR(20)
    @Column(name = "FRST_REGR_NMBR", length = 20)
    private String frstRegrNmbr;

    @ApiModelProperty("최초 등록 일시")  // Y VARCHAR(14)
    @Column(name = "FRST_RGST_DT", length = 14)
    private String frstRgstDt;

    @ApiModelProperty("최종 수정자 번호")  // Y VARCHAR(20)
    @Column(name = "LAST_CRPR_NMBR", length = 20)
    private String lastCrprNmbr;

    @ApiModelProperty("최종 수정  일시")  // Y VARCHAR(14)
    @Column(name = "LAST_CRCT__DT", length = 14)
    private String lastCrctDt;

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

    public TbVdsCtlrDto toDto() {
        return TbVdsCtlrDto.builder()
                .ctlrMngmNmbr(this.ctlrMngmNmbr)
                .vdsCtlrId(this.vdsCtlrId)
                .ctlrTypeCd(this.ctlrTypeCd)
                .valdYn(this.valdYn)
                .xCrdn(this.xCrdn)
                .yCrdn(this.yCrdn)
                .lctn(this.lctn)
                .trmnIp(this.trmnIp)
                .trmnPort(this.trmnPort)
                .ctlrClctCycl(this.ctlrClctCycl)
                .ctlrSttsCycl(this.ctlrSttsCycl)
                .loopBordNum(this.loopBordNum)
                .delYn(this.delYn)
                .build();
    }

    public TbVdsCtlrSttsDto toSttsDto() {
        TbVdsCtlrSttsDto dto = TbVdsCtlrSttsDto.builder()
                .ctlrMngmNmbr(this.ctlrMngmNmbr)
                .nmbr(this.ctlrMngmNmbr)
                .collCnt(0)
                .vdsCtlrId(this.vdsCtlrId)
                .vdsNm(this.lctn)
                .build();

        if (this.state != null) {
            dto.setUpdtDt(this.state.getUpdtDt());

            dto.setCmncSttsCd(this.state.getCmncSttsCd());
            dto.setPrntPwerSttsCd(this.state.getPrntPwerSttsCd());
            dto.setCboxDoorSttsCd(this.state.getCboxDoorSttsCd());
            dto.setFanSttsCd(this.state.getFanSttsCd());
            dto.setHetrSttsCd(this.state.getHetrSttsCd());
            dto.setSubCtlrVal(this.state.getSubCtlrVal());
            dto.setCboxTmpr(this.state.getCboxTmpr());
            dto.setInptVolt(this.state.getInptVolt());
            dto.setPrntVolt(this.state.getPrntVolt());
        }
        else {
            dto.setPrntPwerSttsDesc(CodeManager.STTS_UNKNOWN);      // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.STTS_UNKNOWN);      // 도어
            dto.setFanSttsDesc(CodeManager.STTS_UNKNOWN);           // 팬상태
            dto.setHetrSttsDesc(CodeManager.STTS_UNKNOWN);          // 히터상태
            dto.setCboxTmpr(255);
        }

        dto.setMissSttsYn(ItsUtils.getMissSttsYn(dto.getUpdtDt()));
        if (("Y").equals(dto.getMissSttsYn())) {
            // 기준시각 보다 크기 때문에 상태정보를 통신두절로 설정
            dto.setCmncSttsCd(CodeManager.CMNC_STTS_ERROR);
        }

        dto.setCmncSttsDesc(CodeManager.getCodeDescShort(CodeManager.CMNC_STTS_CD, dto.getCmncSttsCd()));           // 통신상태
        if (CodeManager.CMNC_STTS_NORMAL.equals(dto.getCmncSttsCd())) {
            dto.setPrntPwerSttsDesc(CodeManager.getCodeDescShort(CodeManager.PWER_STTS_CD, dto.getPrntPwerSttsCd()));   // 전원상태
            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.setPrntPwerSttsDesc(CodeManager.STTS_UNKNOWN);      // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.STTS_UNKNOWN);      // 도어
            dto.setFanSttsDesc(CodeManager.STTS_UNKNOWN);           // 팬상태
            dto.setHetrSttsDesc(CodeManager.STTS_UNKNOWN);          // 히터상태
            dto.setCboxTmpr(255);
        }

        return dto;
    }

    public TbVdsCtlr(String ctlrMngmNmbr) {
        this.ctlrMngmNmbr = ctlrMngmNmbr;
    }

    public void updateInfo(TbVdsCtlrDto.TbVdsCtlrUpdReq req) {
        this.vdsCtlrId = req.getVdsCtlrId();
        this.ctlrTypeCd = req.getCtlrTypeCd();
        this.valdYn = req.getValdYn();
        this.xCrdn = req.getXCrdn();
        this.yCrdn = req.getYCrdn();
        this.lctn = req.getLctn();
        this.trmnIp = req.getTrmnIp();
        this.trmnPort = req.getTrmnPort();
        this.ctlrClctCycl = req.getCtlrClctCycl();
        this.ctlrSttsCycl = req.getCtlrSttsCycl();
        this.loopBordNum = req.getLoopBordNum();
        this.delYn = req.getDelYn();
    }

}
