package com.its.op.entity.its.link;

import com.its.op.dto.its.link.TbLinkParaStupDto;
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;
import java.util.ArrayList;

/**
 * 링크 파라미터 설정 Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("링크 파라미터 설정")
@Entity
@Table(name = "TB_LINK_PARA_STUP")
public class TbLinkParaStup implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("파라미터 ID")  // N NUMBER(10)
    @Id
    @Column(name = "PARA_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long paraId;

    @ApiModelProperty("필터링 방법 코드")  // Y VARCHAR2(7)
    @Column(name = "FLTR_MTHD_CD", length = 7)
    private String fltrMthdCd;

    @ApiModelProperty("퓨전 방법 코드")  // Y VARCHAR2(7)
    @Column(name = "FUSN_MTHD_CD", length = 7)
    private String fusnMthdCd;

    @ApiModelProperty("보정 방법 코드")  // Y VARCHAR2(7)
    @Column(name = "ADJS_MTHD_CD", length = 7)
    private String adjsMthdCd;

    @ApiModelProperty("평활화 방법 코드")  // Y VARCHAR2(7)
    @Column(name = "SMTH_MTHD_CD", length = 7)
    private String smthMthdCd;

    @ApiModelProperty("파라미터명")  // Y VARCHAR2(40)
    @Column(name = "PARA_NM", length = 40)
    private String paraNm;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="PARA_ID", referencedColumnName = "PARA_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbLinkParaDetl detl;


    public TbLinkParaStupDto toDto() {
        TbLinkParaStupDto dto = TbLinkParaStupDto.builder()
                .paraId(this.paraId)
                .fltrMthdCd(this.fltrMthdCd)
                .fusnMthdCd(this.fusnMthdCd)
                .adjsMthdCd(this.adjsMthdCd)
                .smthMthdCd(this.smthMthdCd)
                .paraNm(this.paraNm)
                .build();

        // 필터링 파라미터
        if ("FMT0".equals(dto.getFltrMthdCd())) dto.setFltrMthdDesc("사용안함");
        else
        if ("FMT1".equals(dto.getFltrMthdCd())) dto.setFltrMthdDesc("범위값 사용");
        else dto.setFltrMthdDesc("- " + dto.getFltrMthdCd());

        // 퓨전 파라미터
        if ("FST1".equals(dto.getFusnMthdCd())) dto.setFusnMthdDesc("우선순위");
        else
        if ("FST2".equals(dto.getFusnMthdCd())) dto.setFusnMthdDesc("가중치");
        else
        if ("FST3".equals(dto.getFusnMthdCd())) dto.setFusnMthdDesc("최대값");
        else dto.setFusnMthdDesc("- " + dto.getFusnMthdCd());

        // 결측보정 파라미터
        if ("MCA0".equals(dto.getAdjsMthdCd())) dto.setAdjsMthdDesc("사용안함");
        else
        if ("MCA1".equals(dto.getAdjsMthdCd())) dto.setAdjsMthdDesc("단기(15분이동평균)/장기(5분패턴)");
        else
        if ("MCA2".equals(dto.getAdjsMthdCd())) dto.setAdjsMthdDesc("패턴적용");
        else dto.setAdjsMthdDesc("- " + dto.getAdjsMthdCd());

        // 평활화 파라미터
        if ("SMT0".equals(dto.getSmthMthdCd())) dto.setSmthMthdDesc("사용안함");
        else
        if ("SMT1".equals(dto.getSmthMthdCd())) dto.setSmthMthdDesc("편차제한");
        else
        if ("SMT2".equals(dto.getSmthMthdCd())) dto.setSmthMthdDesc("15분 이동평균");
        else dto.setSmthMthdDesc("- " + dto.getSmthMthdCd());

        dto.setClcts(new ArrayList<>());
        return dto;
    }

    public TbLinkParaStup(Long paraId) {
        this.paraId = paraId;
    }

    public void updateInfo(TbLinkParaStupDto.TbLinkParaStupUpdReq req) {
        this.fltrMthdCd = req.getFltrMthdCd();
        this.fusnMthdCd = req.getFusnMthdCd();
        this.adjsMthdCd = req.getAdjsMthdCd();
        this.smthMthdCd = req.getSmthMthdCd();
        this.paraNm = req.getParaNm();
    }

}
