package com.its.op.entity.its.vds;

import com.its.op.dto.its.vds.TbVdsCtlrDto;
import com.its.op.dto.its.vds.TbVdsCtlrSttsDto;
import com.its.op.global.CodeManager;
import com.its.utils.ItsUtils;
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_VDS_CTLR")
public class TbVdsCtlr implements Serializable {
    private static final long serialVersionUID = 1L;

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

    @ApiModelProperty("VDS 제어기 ID")  // Y VARCHAR2(10)
    @Column(name = "VDS_CTLR_ID", length = 10)
    private String vdsCtlrId;

    @ApiModelProperty("VDS 명칭")  // Y VARCHAR2(40)
    @Column(name = "VDS_NM", length = 40)
    private String vdsNm;

    @ApiModelProperty("VDS 제어기 아이피")  // Y VARCHAR2(20)
    @Column(name = "VDS_CTLR_IP", length = 20)
    private String vdsCtlrIp;

    @ApiModelProperty("VDS 제어기 포트")  // Y NUMBER(5)
    @Column(name = "VDS_CTLR_PORT", columnDefinition = "NUMBER", length = 5)
    private Integer vdsCtlrPort;

    @ApiModelProperty("VDS 제어기 지역번호")  // Y NUMBER(5)
    @Column(name = "VDS_CTLR_LOCAL_NO", columnDefinition = "NUMBER", length = 5)
    private Integer vdsCtlrLocalNo;

    @ApiModelProperty("VDS 유형 코드('VDST')")  // Y VARCHAR2(7)
    @Column(name = "VDS_TYPE_CD", length = 7)
    private String vdsTypeCd;

    @ApiModelProperty("설치 위치 명")  // Y VARCHAR2(40)
    @Column(name = "ISTL_LCTN_NM", length = 40)
    private String istlLctnNm;

    @ApiModelProperty("설치 위치 주소")  // Y VARCHAR2(200)
    @Column(name = "ISTL_LCTN_ADDR", length = 200)
    private String istlLctnAddr;

    @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 NUMBER(5)
    @Column(name = "GROUP_NO", columnDefinition = "NUMBER", length = 5)
    private Integer groupNo;

    @ApiModelProperty("FAN 동작모드(0x00:꺼짐,0x01:켜짐,0x02:자동,0x09:알수없음)")  // Y NUMBER(3)
    @Column(name = "FAN_MODE", columnDefinition = "NUMBER", length = 3)
    private Integer fanMode;

    @ApiModelProperty("팬 동작 온도")  // Y NUMBER(3)
    @Column(name = "FAN_RUN_TMPR", columnDefinition = "NUMBER", length = 3)
    private Integer fanRunTmpr;

    @ApiModelProperty("히터 동작모드(0x00:꺼짐,0x01:켜짐,0x02:자동,0x09:알수없음)")  // Y NUMBER(3)
    @Column(name = "HETR_MODE", columnDefinition = "NUMBER", length = 3)
    private Integer hetrMode;

    @ApiModelProperty("히터 동작 온도")  // Y NUMBER(3)
    @Column(name = "HETR_RUN_TMPR", columnDefinition = "NUMBER", length = 3)
    private Integer hetrRunTmpr;

    @ApiModelProperty("검지 전체 차로수")  // Y NUMBER(2)
    @Column(name = "DETECT_LANES", columnDefinition = "NUMBER", length = 2)
    private Integer detectLanes;

    @ApiModelProperty("교통정보수집주기")  // Y NUMBER(3)
    @Column(name = "TRAF_CLCT_CYCL", columnDefinition = "NUMBER", length = 3)
    private Integer trafClctCycl;

    @ApiModelProperty("상태정보수집주기")  // Y NUMBER(3)
    @Column(name = "STTS_CLCT_CYCL", columnDefinition = "NUMBER", length = 3)
    private Integer sttsClctCycl;

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

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

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

    public void setDelete() {
        this.delYn = "Y";
    }


    public TbVdsCtlrDto toDto() {
        return TbVdsCtlrDto.builder()
                .vdsCtlrNmbr(this.vdsCtlrNmbr)
                .vdsCtlrId(this.vdsCtlrId)
                .vdsNm(this.vdsNm)
                .vdsCtlrIp(this.vdsCtlrIp)
                .vdsCtlrPort(this.vdsCtlrPort)
                .vdsCtlrLocalNo(this.vdsCtlrLocalNo)
                .vdsTypeCd(this.vdsTypeCd)
                .istlLctnNm(this.istlLctnNm)
                .istlLctnAddr(this.istlLctnAddr)
                .xCrdn(this.xCrdn)
                .yCrdn(this.yCrdn)
                .groupNo(this.groupNo)
                .fanMode(this.fanMode)
                .fanRunTmpr(this.fanRunTmpr)
                .hetrMode(this.hetrMode)
                .hetrRunTmpr(this.hetrRunTmpr)
                .detectLanes(this.detectLanes)
                .trafClctCycl(this.trafClctCycl)
                .sttsClctCycl(this.sttsClctCycl)
                .valdYn(this.valdYn)
                .delYn(this.delYn)
                .build();
    }

    public TbVdsCtlr(Long vdsCtlrNmbr) {
        this.vdsCtlrNmbr = vdsCtlrNmbr;
    }

    public TbVdsCtlrSttsDto toSttsDto() {
        TbVdsCtlrSttsDto dto = TbVdsCtlrSttsDto.builder()
                .vdsCtlrNmbr(this.vdsCtlrNmbr)
                .collCnt(0)
                .vdsCtlrId(this.vdsCtlrId)
                .vdsNm(this.vdsNm)
                .valdYn(this.valdYn)
                .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.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()));           // 히터상태

            dto.setCollSttsDesc("이상");
        }
        else {
            dto.setPrntPwerSttsDesc(CodeManager.STTS_UNKNOWN);      // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.STTS_UNKNOWN);      // 도어
            dto.setFanSttsDesc(CodeManager.STTS_UNKNOWN);           // 팬상태
            dto.setHetrSttsDesc(CodeManager.STTS_UNKNOWN);          // 히터상태
            dto.setCollSttsDesc(CodeManager.STTS_UNKNOWN);          // 수집상태
            dto.setCboxTmpr(255);
        }

        return dto;
    }

    public void updateInfo(TbVdsCtlrDto.TbVdsCtlrUpdReq req) {
        this.vdsCtlrId = req.getVdsCtlrId();
        this.vdsNm = req.getVdsNm();
        this.vdsCtlrIp = req.getVdsCtlrIp();
        this.vdsCtlrPort = req.getVdsCtlrPort();
        this.vdsCtlrLocalNo = req.getVdsCtlrLocalNo();
        this.vdsTypeCd = req.getVdsTypeCd();
        this.istlLctnNm = req.getIstlLctnNm();
        this.istlLctnAddr = req.getIstlLctnAddr();
        this.xCrdn = req.getXCrdn();
        this.yCrdn = req.getYCrdn();
        this.groupNo = req.getGroupNo();
        this.fanMode = req.getFanMode();
        this.fanRunTmpr = req.getFanRunTmpr();
        this.hetrMode = req.getHetrMode();
        this.hetrRunTmpr = req.getHetrRunTmpr();
        this.detectLanes = req.getDetectLanes();
        this.trafClctCycl = req.getTrafClctCycl();
        this.sttsClctCycl = req.getSttsClctCycl();
        this.valdYn = req.getValdYn();
        this.delYn = req.getDelYn();
    }

}
