package com.its.vms.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 *  Entity Class
 */
@Data
@Builder
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class TbVmsAtmp implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long atmpSttnNmbr;
    private String atmpSttnNm;
    private String vmsDispNm;
    private String delYn;

    private String msrmDt;
    private String msrmSystNm;
    private String coVal;
    private String so2Val;
    private String no2Val;
    private String o3Val;
    private String pm10Val;
    private String pm1024hhVal;
    private String pm25Val;
    private String pm2524hhVal;
    private String intgAtmpVal;
    private String intgAtmpGrad;
    private String so2Grad;
    private String coGrad;
    private String o3Grad;
    private String no2Grad;
    private String pm1024hhGrad;
    private String pm2524hhGrad;
    private String pm101hhGrad;
    private String pm251hhGrad;
    private String updtDt;

    private boolean isSuccess;

    public void correct(TbVmsAtmp obj) {
        obj.setMsrmDt(this.msrmDt);
        obj.setMsrmSystNm(this.msrmSystNm);
        obj.setCoVal(this.coVal);
        obj.setSo2Val(this.so2Val);
        obj.setNo2Val(this.no2Val);
        obj.setO3Val(this.o3Val);
        obj.setPm10Val(this.pm10Val);
        obj.setPm1024hhVal(this.pm1024hhVal);
        obj.setPm25Val(this.pm25Val);
        obj.setPm2524hhVal(this.pm2524hhVal);
        obj.setIntgAtmpVal(this.intgAtmpVal);
        obj.setIntgAtmpGrad(this.intgAtmpGrad);
        obj.setSo2Grad(this.so2Grad);
        obj.setCoGrad(this.coGrad);
        obj.setO3Grad(this.o3Grad);
        obj.setNo2Grad(this.no2Grad);
        obj.setPm1024hhGrad(this.pm1024hhGrad);
        obj.setPm2524hhGrad(this.pm2524hhGrad);
        obj.setPm101hhGrad(this.pm101hhGrad);
        obj.setPm251hhGrad(this.pm251hhGrad);
        obj.setUpdtDt(this.updtDt);
        obj.setSuccess(this.isSuccess);
    }

    public String getPM10_VAL(boolean AUnit) {
        // 미세먼지
        if (this.isSuccess) {
            if (AUnit)
                return this.pm10Val + " ㎍/㎥";
            else
                return this.pm10Val;
        }
        return (AUnit) ? "- ㎍/㎥" : "-";
    }
    
    public String getPM25_VAL(boolean AUnit) {
        //초미세먼지
        if (this.isSuccess) {
            if (AUnit)
                return this.pm25Val + " ㎍/㎥";
            else
                return this.pm25Val;
        }
        return (AUnit) ? "- ㎍/㎥" : "-";
    }
    
    public String getATMP_VAL() {
        //통합대기환경수치
        if (this.isSuccess) {
            return this.intgAtmpVal;
        }
        return "-";
    }
    
    public String GetO3_VAL(boolean AUnit) {
        //오존농도
        if (this.isSuccess) {
            if (AUnit)
                return this.o3Val + " ppm";
            else
                return this.o3Val;
        }
        return (AUnit) ? "- ppm" : "-";
    }
    
    public String getGradeDesc(int AGrad) {
        if (!this.isSuccess) return "-";

        switch(AGrad) {
            case 1: return "좋음";
            case 2: return "보통";
            case 3: return "나쁨";
            case 4: return "매우나쁨";
        }
        return "-";
    }
    
    public String getPM10_GRAD() {
        return getGradeDesc(parseDefInt(this.pm101hhGrad, 0));
    }
    
    public String getPM25_GRAD() {
        return getGradeDesc(parseDefInt(this.pm251hhGrad, 0));
    }
    
    public String getATMP_GRAD() {
        return getGradeDesc(parseDefInt(this.intgAtmpGrad, 0));
    }
    
    public String getO3_GRAD() {
        return getGradeDesc(parseDefInt(this.o3Grad, 0));
    }
    
    public int getGradeColor(int AGrad) {
        if (!this.isSuccess) return 3;//Color.GREEN.getRGB();

        switch(AGrad)
        {
            case 1: return 2;//밝은녹색(Lime)
            case 2: return 5;//하늘색(Aqua)
            case 3: return 4;//주황색(Orange)
            case 4: return 1;//적색(Red)
        }
        return 3;
    }
    
    public int getPM10_CLR() {
        return getGradeColor(parseDefInt(this.pm101hhGrad, 0));
    }
    
    public int getPM25_CLR() {
        return getGradeColor(parseDefInt(this.pm251hhGrad, 0));
    }
    
    public int getATMP_CLR() {
        return getGradeColor(parseDefInt(this.intgAtmpGrad, 0));
    }
    
    public int getO3_CLR() {
        return getGradeColor(parseDefInt(this.o3Grad, 0));
    }

    private int parseDefInt(String value, int defValue) {
        if (value == null || value.length() == 0)
            return defValue;

        String strValue = value.trim();
        for (int i = 0; i < strValue.length(); i++) {
            if (!Character.isDigit(strValue.charAt(i)))
                return defValue;
        }
        return Integer.parseInt(strValue);
    }
}
