package com.inspect.analysis.domain;


import com.inspect.base.core.utils.StringUtils;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

@Setter
@Getter
public class AlgValue {
   private String max;
   private String min;

   public boolean isAlarm(String value) {
      try {
         double valueD = Double.parseDouble(value);
         double tmp;
         if(!StringUtils.isEmpty(this.max)) {
            tmp = Double.parseDouble(this.max);
            if(valueD > tmp) {
               return true;
            }
         }

         if(!StringUtils.isEmpty(this.min)) {
            tmp = Double.parseDouble(this.min);
            if(valueD < tmp) {
               return true;
            }
         }

         return false;
      } catch (NumberFormatException e) {
         return true;
      }
   }

   public String formatValue(String args) {
      String pattern = "(\\d+(\\.\\d+)?)";
      String valueOnlyPrecise = args.replaceAll(".*?(" + pattern + ").*", "$1");
      if(valueOnlyPrecise.startsWith(".")) {
         valueOnlyPrecise = "0" + valueOnlyPrecise;
      }

      if(valueOnlyPrecise.equals("0.0")) {
         valueOnlyPrecise = "0";
      }

      if(args.startsWith("-")) {
         valueOnlyPrecise = "-" + valueOnlyPrecise;
      }

      return valueOnlyPrecise;
   }

    public AlgValue(String max, String min) {
      this.max = max;
      this.min = min;
   }

   public AlgValue() {
   }

   @Override
   public boolean equals(Object object) {
      if (this == object) return true;
      if (object == null || getClass() != object.getClass()) return false;
      AlgValue algValue = (AlgValue) object;
      return Objects.equals(max, algValue.max) && Objects.equals(min, algValue.min);
   }

   @Override
   public int hashCode() {
      return Objects.hash(max, min);
   }

   @Override
   public String toString() {
      return "AlgValue{" +
              "max='" + max + '\'' +
              ", min='" + min + '\'' +
              '}';
   }
}
