/*
 * Decompiled with CFR 0.152.
 */
package com.hst.framework.util;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Collection;

public class HstComputeHjUtil {
    public static final Class STRING = String.class;
    public static final Class BIGDECIMAL = BigDecimal.class;
    public static final Class INTEGER = Integer.class;
    public static final Class LONG = Long.class;
    public static final Class DOUBLE = Double.class;
    public static final Class FLOAT = Float.class;
    public static final Class Int = Integer.TYPE;
    public static final Class Long = java.lang.Long.TYPE;
    public static final Class Double = java.lang.Double.TYPE;
    public static final Class Float = java.lang.Float.TYPE;

    public static Object compute(Object[] objs, Class clazz, String[] properties, Class propertyType) throws Exception {
        Object result = clazz.getConstructor(new Class[0]).newInstance(new Object[0]);
        int i = 0;
        while (i < properties.length) {
            BigDecimal total = HstComputeHjUtil.getOnePropertyTotal(objs, properties[i]);
            HstComputeHjUtil.setOnePropertyTotal(result, properties[i], total, propertyType);
            ++i;
        }
        return result;
    }

    public static Object compute(Object[] objs, Class clazz, String[] properties, Class[] propertyTypeS) throws Exception {
        Object result = clazz.getConstructor(new Class[0]).newInstance(new Object[0]);
        int i = 0;
        while (i < properties.length) {
            BigDecimal total = HstComputeHjUtil.getOnePropertyTotal(objs, properties[i]);
            HstComputeHjUtil.setOnePropertyTotal(result, properties[i], total, propertyTypeS[i]);
            ++i;
        }
        return result;
    }

    public static Object compute(Collection collection, Class clazz, String[] properties, Class propertyType) throws Exception {
        Object[] objs = new Object[collection.size()];
        collection.toArray(objs);
        return HstComputeHjUtil.compute(objs, clazz, properties, propertyType);
    }

    private static void setOnePropertyTotal(Object result, String property, BigDecimal total, Class propertyType) throws Exception {
        String setMethodName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
        Class<?> newClass = result.getClass();
        Method method = newClass.getMethod(setMethodName, propertyType);
        if (propertyType == STRING) {
            method.invoke(result, total.setScale(2, 4).toString());
        } else if (propertyType == BIGDECIMAL) {
            method.invoke(result, total.setScale(2, 4));
        } else if (propertyType == INTEGER || propertyType == Int) {
            method.invoke(result, new Integer(total.intValue()));
        } else if (propertyType == LONG || propertyType == Long) {
            method.invoke(result, new Long(total.longValue()));
        } else if (propertyType == DOUBLE || propertyType == Double) {
            method.invoke(result, new Double(total.doubleValue()));
        } else if (propertyType == FLOAT || propertyType == Float) {
            method.invoke(result, new Float(total.floatValue()));
        }
    }

    private static BigDecimal getOnePropertyTotal(Object[] objs, String property) throws Exception {
        BigDecimal result = new BigDecimal(0.0);
        int i = 0;
        while (i < objs.length) {
            String getMethodName;
            Class<?> c = objs[i].getClass();
            Method method = c.getMethod(getMethodName = "get" + property.substring(0, 1).toUpperCase() + property.substring(1), new Class[0]);
            Object retobj = method.invoke(objs[i], new Object[0]);
            if (retobj instanceof String) {
                result = result.add(new BigDecimal((String)retobj));
            } else if (retobj instanceof Integer) {
                result = result.add(new BigDecimal(((Integer)retobj).doubleValue()));
            } else if (retobj instanceof Long) {
                result = result.add(new BigDecimal(((Long)retobj).doubleValue()));
            } else if (retobj instanceof Double) {
                result = result.add(new BigDecimal((Double)retobj));
            } else if (retobj instanceof Float) {
                result = result.add(new BigDecimal(((Float)retobj).doubleValue()));
            } else if (retobj instanceof BigDecimal) {
                result = result.add((BigDecimal)retobj);
            } else {
                throw new Exception("\u5bf9\u5e94\u7684\u5c5e\u6027[" + property + "]\u4e0d\u662f\u6570\u503c\u7c7b\u578b\uff0c\u8bf7\u68c0\u67e5\uff01");
            }
            ++i;
        }
        return result;
    }
}
