package cn.superdesk.uniorder.common.numerical;

import lombok.NoArgsConstructor;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.*;

/**
 * 集合常用方法工具类 计划弃用
 *
 * @author yubing
 * @author Caratacus
 * @author peiyu
 * @author linjinyu 整理
 * @since 2016-07-11
 * @since 2016-09-19
 */
@Deprecated
@NoArgsConstructor
public class AnCollectionUtils {

	/**
	 * 判断一个集合是否为空
	 * null或者空集合都会返回true
	 *
	 * @param collection 需要判断的集合
	 * @return 是否有值,null或者空集合都是返回true
	 */
	public static boolean isEmpty(Collection<?> collection) {
		return null == collection || collection.isEmpty();
	}

	/**
	 * 判断一个集合是否不为空
	 *
	 * @param collection 需要判断的集合
	 * @return 是否不为空
	 */
	public static boolean isNotEmpty(Collection<?> collection) {
		return null != collection && !collection.isEmpty();
	}

	/**
	 * 判断Map是否为空
	 *
	 * @param map 入参
	 * @return boolean
	 * @author Caratacus
	 * @since 2016-09-19
	 */
	public static boolean isEmpty(Map<?, ?> map) {
		return (map == null || map.isEmpty());
	}

	/**
	 * 判断Map是否不为空
	 *
	 * @param map 入参
	 * @return boolean
	 * @author Caratacus
	 * @since 2016-09-19
	 */
	public static boolean isNotEmpty(Map<?, ?> map) {
		return !isEmpty(map);
	}

	/**
	 * 创建一个空集合
	 *
	 * @param <T> 泛型
	 * @return 集合对象
	 */
	public static <T> ArrayList<T> newArrayList() {
		return new ArrayList<T>();
	}

	/**
	 * 创建一个指定大小的集合
	 *
	 * @param initialCapacity 集合大小
	 * @param <T>             泛型
	 * @return 集合对象
	 */
	public static <T> ArrayList<T> newArrayList(int initialCapacity) {
		return new ArrayList<T>(initialCapacity);
	}

	/**
	 * 创建一个有默认内容的集合
	 *
	 * @param <T> 泛型
	 * @param ele 内容
	 * @return 集合对象
	 */
	public static <T> ArrayList newArrayList(T... ele) {
		ArrayList list = null;
		if (null != ele && 0 != ele.length) {
			list = newArrayList(ele.length);
			Collections.addAll(list, ele);
		}
		return list;
	}

	/**
	 * CollectionUtils.sortList2
	 *
	 * @param list
	 * @param sortField
	 * @param sortMode
	 * @param <T>
	 * @author yubign
	 * @since 2016-07-11
	 */
	public static <T> void sortList2(List<T> list, final String sortField, final String sortMode) {
		if (list == null || list.size() < 2) {
			return;
		}
		Collections.sort(list, new Comparator<T>() {
			@Override
			public int compare(T o1, T o2) {
				try {
					Class clazz = o1.getClass();
					Field field = clazz.getDeclaredField(sortField); //获取成员变量
					field.setAccessible(true); //设置成可访问状态
					String typeName = field.getType().getName().toLowerCase(); //转换成小写

					Object v1 = field.get(o1); //获取field的值
					Object v2 = field.get(o2); //获取field的值

					boolean ASC_order = (sortMode == null || "ASC".equalsIgnoreCase(sortMode));

					//判断字段数据类型,并比较大小
					if (typeName.endsWith("string")) {
						String value1 = v1.toString();
						String value2 = v2.toString();
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else if (typeName.endsWith("short")) {
						Short value1 = Short.parseShort(v1.toString());
						Short value2 = Short.parseShort(v2.toString());
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else if (typeName.endsWith("byte")) {
						Byte value1 = Byte.parseByte(v1.toString());
						Byte value2 = Byte.parseByte(v2.toString());
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else if (typeName.endsWith("char")) {
						Integer value1 = (int) (v1.toString().charAt(0));
						Integer value2 = (int) (v2.toString().charAt(0));
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else if (typeName.endsWith("int") || typeName.endsWith("integer")) {
						Integer value1 = Integer.parseInt(v1.toString());
						Integer value2 = Integer.parseInt(v2.toString());
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else if (typeName.endsWith("long")) {
						Long value1 = Long.parseLong(v1.toString());
						Long value2 = Long.parseLong(v2.toString());
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else if (typeName.endsWith("float")) {
						Float value1 = Float.parseFloat(v1.toString());
						Float value2 = Float.parseFloat(v2.toString());
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else if (typeName.endsWith("double")) {
						Double value1 = Double.parseDouble(v1.toString());
						Double value2 = Double.parseDouble(v2.toString());
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else if (typeName.endsWith("boolean")) {
						Boolean value1 = Boolean.parseBoolean(v1.toString());
						Boolean value2 = Boolean.parseBoolean(v2.toString());
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else if (typeName.endsWith("date")) {
						Date value1 = (Date) (v1);
						Date value2 = (Date) (v2);
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else if (typeName.endsWith("timestamp")) {
						Timestamp value1 = (Timestamp) (v1);
						Timestamp value2 = (Timestamp) (v2);
						return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
					} else {
						//调用对象的compareTo()方法比较大小
						Method method = field.getType().getDeclaredMethod("compareTo", new Class[]{field.getType()});
						method.setAccessible(true); //设置可访问权限
						int result = (Integer) method.invoke(v1, new Object[]{v2});
						return ASC_order ? result : result * (-1);
					}
				} catch (Exception e) {
					String err = e.getLocalizedMessage();
					System.out.println(err);
					e.printStackTrace();
				}

				return 0; //未知类型,无法比较大小
			}
		});
	}

	/**
	 * CollectionSortMap.sortList
	 *
	 * @param list
	 * @param sortField
	 * @param sortMode
	 * @param <T>
	 * @author yubign
	 * @since 2016-07-11
	 */
	public static <T> void sortList(List<T> list, final String sortField, final String sortMode) {
		if (list == null || list.size() < 2) {
			return;
		}
		Collections.sort(list, new Comparator<T>() {
			@Override
			public int compare(T o1, T o2) {
				try {
					Map m1 = (Map) o1, m2 = (Map) o2;

					Integer value1 = (Integer) m1.get(sortField);
					Integer value2 = (Integer) m2.get(sortField);

					boolean ASC_order = (sortMode == null || "ASC".equalsIgnoreCase(sortMode));
					return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);

				} catch (Exception e) {
					String err = e.getLocalizedMessage();
					System.out.println(err);
					e.printStackTrace();
				}

				return 0; //未知类型,无法比较大小
			}
		});
	}

}
