package com.bonus.canteen.core.nutrition.utils;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.nutrition.constant.NutritionConstants;
import com.bonus.common.core.exception.ServiceException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Iterator;
import java.util.List;

public class NuConvertUtil {
   public static <T> T getDailyAverageData(T obj, long day) {
      T newObj = null;

      try {
         Class<?> clazz = obj.getClass();
         newObj = (T) clazz.getDeclaredConstructor().newInstance();
         Field[] fields = clazz.getDeclaredFields();
         Field[] var6 = fields;
         int var7 = fields.length;

         for(int var8 = 0; var8 < var7; ++var8) {
            Field field = var6[var8];
            field.setAccessible(true);
            if (field.getType() == Double.class) {
               Double currentValue = (Double)field.get(obj);
               if (currentValue != null) {
                  Double newValue = BigDecimal.valueOf(currentValue).divide(new BigDecimal(day), NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP).doubleValue();
                  field.set(newObj, newValue);
               }
            } else if (field.getType() == BigDecimal.class) {
               BigDecimal currentValue = (BigDecimal)field.get(obj);
               if (currentValue != null) {
                  BigDecimal newValue = currentValue.divide(new BigDecimal(day), NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP);
                  field.set(newObj, newValue);
               } else {
                  field.set(newObj, BigDecimal.ZERO);
               }
            } else {
               field.set(newObj, field.get(obj));
            }
         }
      } catch (Exception var12) {
         throw new ServiceException("创建或修改对象出现异常：" + var12);
      }

      obj = null;
      return newObj;
   }

   public static <T> T getActualContent(T obj, BigDecimal actualWeight, BigDecimal standardWeight) {
      if (ObjectUtil.isNull(standardWeight) || NumberUtil.equals(standardWeight, BigDecimal.ZERO)) {
         standardWeight = BigDecimal.valueOf(NutritionConstants.BASICS_WEIGHT);
      }

      T newObj = null;

      try {
         Class<?> clazz = obj.getClass();
         newObj = (T) clazz.getDeclaredConstructor().newInstance();
         Field[] fields = clazz.getDeclaredFields();
         Field[] var6 = fields;
         int var7 = fields.length;

         for(int var8 = 0; var8 < var7; ++var8) {
            Field field = var6[var8];
            field.setAccessible(true);
            if (field.getType() == Double.class) {
               Double currentValue = (Double)field.get(obj);
               if (currentValue != null) {
                  Double newValue = actualWeight.divide(standardWeight).multiply(BigDecimal.valueOf(currentValue)).setScale(NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP).doubleValue();
                  field.set(newObj, newValue);
               } else {
                  field.set(newObj, 0.0);
               }
            } else if (field.getType() == BigDecimal.class) {
               BigDecimal currentValue = (BigDecimal)field.get(obj);
               if (currentValue != null) {
                  BigDecimal newValue = actualWeight.divide(standardWeight).multiply(currentValue).setScale(NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP);
                  field.set(newObj, newValue);
               } else {
                  field.set(newObj, BigDecimal.ZERO);
               }
            } else {
               field.set(newObj, field.get(obj));
            }
         }
      } catch (Exception var12) {
         throw new ServiceException("创建或修改对象出现异常：" + var12);
      }

      obj = null;
      return newObj;
   }

   public static <T> T sumDoubleValues(List<T> list) {
      T obj = null;

      try {
         T first = list.get(0);
         Class<?> clazz = first.getClass();
         obj = (T) clazz.getDeclaredConstructor().newInstance();
         Field[] fields = clazz.getDeclaredFields();
         Field[] var5 = fields;
         int var6 = fields.length;

         for(int var7 = 0; var7 < var6; ++var7) {
            Field field = var5[var7];
            field.setAccessible(true);
            if (field.getType() == Double.class) {
               Double sum = 0.0;
               Iterator var10 = list.iterator();

               while(var10.hasNext()) {
                  T element = (T) var10.next();
                  Double value = (Double)field.get(element);
                  if (value != null) {
                     sum = NumberUtil.add(sum, value);
                  }
               }

               field.set(obj, sum);
            }
         }
      } catch (Exception var13) {
         var13.printStackTrace();
      }

      return obj;
   }

   public static <T> T sumBigDecimalValues(List<T> list) {
      T obj = null;

      try {
         T first = list.get(0);
         Class<?> clazz = first.getClass();
         obj = (T) clazz.getDeclaredConstructor().newInstance();
         Field[] fields = clazz.getDeclaredFields();
         Field[] var5 = fields;
         int var6 = fields.length;

         for(int var7 = 0; var7 < var6; ++var7) {
            Field field = var5[var7];
            field.setAccessible(true);
            if (field.getType() == BigDecimal.class) {
               BigDecimal sum = BigDecimal.ZERO;
               Iterator var10 = list.iterator();

               while(var10.hasNext()) {
                  T element = (T) var10.next();
                  Number value = (Number)field.get(element);
                  if (value != null) {
                     sum = sum.add(new BigDecimal(value.toString()));
                  }
               }

               field.set(obj, sum);
            }
         }
      } catch (Exception var13) {
         var13.printStackTrace();
      }

      return obj;
   }

   public static BigDecimal toTwo(Number val) {
      return ObjectUtil.isNull(val) ? BigDecimal.ZERO.setScale(NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP) : BigDecimal.valueOf(val.doubleValue()).setScale(NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP);
   }

   public static BigDecimal per(Number val, Number total) {
      if (ObjectUtil.isNull(val)) {
         return BigDecimal.ZERO.setScale(NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP);
      } else {
         return !ObjectUtil.isNull(total) && !NumberUtil.equals((BigDecimal)total, BigDecimal.ZERO) ? NumberUtil.div(val, total).setScale(NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP) : BigDecimal.ZERO.setScale(NutritionConstants.NEW_SCALE, RoundingMode.HALF_UP);
      }
   }

   public static BigDecimal getBMI(BigDecimal weight, BigDecimal height) {
      if (!ObjectUtil.isNull(weight) && !ObjectUtil.isNull(height)) {
         if (NumberUtil.equals(height, BigDecimal.ZERO)) {
            return new BigDecimal(-1);
         } else {
            height = NumberUtil.div(height, BigDecimal.valueOf(100L), 2, RoundingMode.HALF_UP);
            return NumberUtil.div(weight, NumberUtil.mul(height, height), 2, RoundingMode.HALF_UP);
         }
      } else {
         return new BigDecimal(-1);
      }
   }
}
