package com.stardust.easyassess.track.models.plan;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.HashMap;
import java.util.Map;

public class IQCPlanSpecimen {
    public final static String SPECIMEN_TYPE_SELECTION = "S";
    public final static String SPECIMEN_TYPE_TARGET_WITH_FIX_FLOAT = "T";
    public final static String SPECIMEN_TYPE_TARGET_WITH_PERCENTAGE_FLOAT = "P";

    private String type = SPECIMEN_TYPE_SELECTION;
    private String number;
    private String value;
    private double targetValue;
    private double floatValue;
    private Map<String, Integer> enumValues = new HashMap();

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public double getTargetValue() {
        return targetValue;
    }

    public void setTargetValue(double targetValue) {
        this.targetValue = targetValue;
    }

    public double getFloatValue() {
        return floatValue;
    }

    public void setFloatValue(double floatValue) {
        this.floatValue = floatValue;
    }

    public Map<String, Integer> getEnumValues() {
        return enumValues;
    }

    public void setEnumValues(Map<String, Integer> enumValues) {
        this.enumValues = enumValues;
    }

    @JsonIgnore
    public boolean isInControl() {
        boolean result = false;
        switch (type) {
            case SPECIMEN_TYPE_SELECTION:
                if (Math.abs(new Double(getTargetValue()).intValue() - enumValues.get(getValue())) <= new Double(getFloatValue()).intValue()) {
                    result = true;
                }
                break;
            case SPECIMEN_TYPE_TARGET_WITH_FIX_FLOAT:
                if (Math.abs(getTargetValue() - Double.parseDouble(getValue())) <= new Double(getFloatValue()).doubleValue()) {
                    result = true;
                }
                break;
            case SPECIMEN_TYPE_TARGET_WITH_PERCENTAGE_FLOAT:
                double diff = getTargetValue() - Double.parseDouble(getValue());
                double percentage = (diff/getTargetValue()) * 100;
                if (percentage < getFloatValue()) {
                    result = true;
                }
                break;
        }
        return result;
    }
}



@@ -2040,38 +2040,31 @@
 ) - 
-Double.parseDouble(get
+getSafelyNumber
 Value())
 ) %3C=
@@ -2051,33 +2051,32 @@
 lyNumberValue())
-)
  %3C= new Double(g
@@ -2298,38 +2298,30 @@
 ) - 
-Double.parseDouble(get
+getSafelyNumber
 Value()
-)
 ;%0A  
@@ -2515,32 +2515,32 @@
 reak;%0A        %7D%0A
-
         return r
@@ -2551,13 +2551,349 @@
 t;%0A    %7D
+%0A%0A    private double getSafelyNumberValue() %7B%0A        String value = getValue();%0A        if (value != null && !value.isEmpty()) %7B%0A            value = value.replaceAll(%22%5B%5E-+.%5C%5Cd%5D%22, %22%22);%0A        %7D%0A        try %7B%0A            return Double.parseDouble(value);%0A        %7D catch (NumberFormatException e) %7B%0A            return 0;%0A        %7D%0A    %7D
 %0A%7D%0A%0A%0A

Handle with invaild number format