package cn.palmte.work.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by wang.lin@esstx.cn on 2018/4/20.
 */
public class ObjectKit {

    public static boolean isEmpty(Object object) {
        if (object == null || "".equals(object) || "null".equals(object)) {
            return true;
        }
        return false;
    }

    public static boolean isNotEmpty(Object object) {
        if (object == null || "".equals(object) || "null".equals(object)) {
            return false;
        }
        return true;
    }
    public static boolean listNotNull(List list){
        return list!=null && list.size()>0;
    }
    /**
     * 获取JavaBean风格的属性值
     */
    public static Object get(Object o , String propertyName){
        try {
            propertyName = StrKit.toUpperCaseFirstOne(propertyName);
            Class<?> clazz = o.getClass();
            Method getMethod = clazz.getMethod("get" + propertyName);
            return getMethod.invoke(o);
        } catch (Exception e) {
        }
        return null;
    }

    public static Object getRecordValue(Object o , String propertyName){
        try {
            Class<?> clazz = o.getClass();
            Method getMethod = clazz.getMethod("get",String.class);
            return getMethod.invoke(o,propertyName);
        } catch (Exception e) {
        }
        return null;
    }

    public static String  toString(Object object) {
        return object!=null?object.toString():null;
    }

    public static Float toFloat(Object object){
        return object!=null?Float.parseFloat(object.toString()):null;
    }

    /*public static Double toDouble(Object object){
        java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#.00");
        return object!=null?Double.valueOf(object.toString()):null;
    }*/

    public static Integer toInt(Object object){
        return object!=null?Integer.parseInt(object.toString()):null;
    }

    public static Date toDate(Object object) throws ParseException {
        return DateKit.getDate(ObjectKit.toString(object));
    }

    public static <T> void setValue(T instance, Field field , Object v) {
        if(null == v){
            return;
        }
        try {
            field.setAccessible(true);
            field.set(instance,v);
        } catch (Exception e) {
        }
    }

    public static <T> void setValueCompatibleDate(T instance, Field field , Object v) {
        if(null == v){
            return;
        }
        try {
            if(field.getType() == Date.class) {
                if (v instanceof String) {
                    try {
                        v = new SimpleDateFormat("yyyy-MM-dd").parse(v.toString());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else if (v instanceof Number) {
                    v = new Date(((Number) v).longValue());
                }
            }
            field.setAccessible(true);
            field.set(instance,v);
        } catch (Exception e) {
        }
    }

    /**
     * 将一个值转换为兼容类型的值
     * @param value src
     * @param toClazz 转换的类型
     */
    public static Object compatibleValue(Object value , Class<?> toClazz){
        if(toClazz == Byte.class || toClazz == byte.class){
            if(value instanceof Number){
                return ((Number)value).byteValue();
            }else if(value instanceof String){
                return Byte.valueOf((String) value);
            }
        }
        if(toClazz == Short.class || toClazz == short.class){
            if(value instanceof Number){
                return ((Number)value).shortValue();
            }else if(value instanceof String){
                return Short.valueOf((String) value);
            }
        }
        if(toClazz == Integer.class || toClazz == int.class){
            if(value instanceof Number){
                return ((Number)value).intValue();
            }else if(value instanceof String){
                return Integer.valueOf((String) value);
            }
        }
        if(toClazz == Long.class || toClazz == long.class){
            if(value instanceof Number){
                return ((Number)value).longValue();
            }else if(value instanceof String){
                return Long.valueOf((String) value);
            }
        }
        if(toClazz == Double.class || toClazz == double.class){
            if(value instanceof Number){
                return ((Number)value).doubleValue();
            }else if(value instanceof String){
                return Double.valueOf((String) value);
            }
        }
        if(toClazz == Float.class || toClazz == float.class){
            if(value instanceof Number){
                return ((Number)value).floatValue();
            }else if(value instanceof String){
                return Float.valueOf((String) value);
            }
        }
        if(toClazz == BigDecimal.class){
            return new BigDecimal(value.toString());
        }
        if(toClazz == BigInteger.class){
            return new BigInteger(value.toString());
        }
        if(toClazz == Boolean.class || toClazz == boolean.class){
            return Boolean.valueOf(value.toString());
        }
        if(toClazz == Character.class || toClazz == char.class){
            return value;
        }

        if(toClazz == String.class){
            return value.toString();
        }

        /*if(toClazz == java.util.Date.class){
            if(value instanceof String){
                try {
                    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }else if(value instanceof Number){
                return new Date(((Number) value).longValue());
            }
        }*/

        return value;
    }
}
