package com.engine.htsc.doc.util;


import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class BeanUtil {

    /**
     * 将一个 Map 对象转化为一个 JavaBean
     * @param type 要转化的类型
     * @param map 包含属性值的 map
     * @return 转化出来的 JavaBean 对象
     * @throws IntrospectionException
     *             如果分析类属性失败
     * @throws IllegalAccessException
     *             如果实例化 JavaBean 失败
     * @throws InstantiationException
     *             如果实例化 JavaBean 失败
     * @throws InvocationTargetException
     *             如果调用属性的 setter 方法失败
     */
    public static Object convertMap(Class type, Map map)
            throws IntrospectionException, IllegalAccessException,
            InstantiationException, InvocationTargetException {
        BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
        Object obj = type.newInstance(); // 创建 JavaBean 对象

        // 给 JavaBean 对象的属性赋值
        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
        for (int i = 0; i< propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();

            if (map.containsKey(propertyName)) {
                // 下面一句可以 try 起来，这样当一个属性赋值失败的时候就不会影响其他属性赋值。
                Object value = map.get(propertyName);

                Object[] args = new Object[1];
                args[0] = value;

                descriptor.getWriteMethod().invoke(obj, args);
            }
        }
        return obj;
    }

    /**
     * 将一个 JavaBean 对象转化为一个  Map
     * @param bean 要转化的JavaBean 对象
     * @return 转化出来的  Map 对象
     * @throws IntrospectionException 如果分析类属性失败
     * @throws IllegalAccessException 如果实例化 JavaBean 失败
     * @throws InvocationTargetException 如果调用属性的 setter 方法失败
     */
    public static Map convertBean(Object bean)
            throws IntrospectionException, IllegalAccessException, InvocationTargetException {
        Class type = bean.getClass();
        Map returnMap = new HashMap();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);

        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
        for (int i = 0; i< propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if (!propertyName.equals("class")) {
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(bean, new Object[0]);
                if (result != null) {
                    returnMap.put(propertyName, result);
                } else {
                    returnMap.put(propertyName, "");
                }
            }
        }
        return returnMap;
    }

    /**
     * 将长字符串切分成短字符串数组
     * @param ids  原始字符串 逗号英文分割
     * @param pernum  每个字符数组大小
     * @return
     */
    public static List<String> splitByPerNum(String ids, int pernum) {
        List<String>  result = new ArrayList<>();
        if(StringUtils.isNotEmpty(ids)) {
            String[] idArray = ids.split(",");
            result = splitByPerNum(idArray,pernum);
        }
        return result;
    }

    /**
     * 将字符list按照大小切分成指定的短字符list 逗号分割
     * @param ids
     * @param pernum
     * @return
     */
    public static List<String> splitByPerNum (List<String> ids, int pernum) {
        List<String>  result = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(ids)) {
            String[] idArray = ids.toArray(new String[ids.size()]);
            result = splitByPerNum(idArray,pernum);
        }
        return result;
    }

    /**
     * 将长字符串切分成短字符串数组
     * @param ids
     * @param pernum
     * @return
     */
    public static List<String> splitByPerNum(String[] ids, int pernum) {
        List<String>  result = new ArrayList<>();
        if(null!=ids&&ids.length>0) {
            int countPer = 0;
            int countAll = 0;
            String tempIds = "";
            for(String id : ids) {
                tempIds = tempIds+id+",";
                countPer ++;
                if(countPer<pernum&&countAll+1<ids.length) {

                }else {
                    tempIds = tempIds.substring(0,tempIds.length()-1);
                    result.add(tempIds);
                    countPer = 0;
                    tempIds = "";
                }
                countAll++;
            }
        }
        return result;
    }

    /**
     * 去除集合中重复元素 顺序安全
     * @param list
     */
    public static void removeDuplicateWithOrder(List list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        list.clear();
        list.addAll(newList);
        System.out.println( " remove duplicate " + list);
    }

    /**
     * 将逗号分割的字符串转List
     * @param str
     * @return
     */
    public static List<String> convertStrToList(String str) {
        List<String> result = new ArrayList<>();
        if(StringUtils.isNotEmpty(str)) {
            String[] idArray = str.split(",");
            result = Arrays.asList(idArray);
        }
        return result;
    }

    /**
     * List拆分
     * @param list
     * @param len
     * @return
     */
    public static List<List<String>> splitList(List<String> list, int len) {
        if (list == null || list.size() == 0 || len < 1) {
            return null;
        }

        List<List<String>> result = new ArrayList<List<String>>();

        int size = list.size();
        int count = (size + len - 1) / len;

        for (int i = 0; i < count; i++) {
            List<String> subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
            result.add(subList);
        }
        return result;
    }

    /**
     * 判断一个id 是否在ids字符串里面
     * @param id
     * @param ids
     * @return
     */
    public static boolean isInIds(String id,String ids) {
        boolean flag = false;
        if(StringUtils.isNotEmpty(id)&&StringUtils.isNotEmpty(ids)) {
            List<String> idsList = Arrays.asList(ids.split(","));
            if(idsList.contains(id)) {
                flag = true;
            }
        }
        return flag;
    }


    /**
     * 泛微id比较 返回 A集合和B集合的交集
     * @param idsA
     * @param idsB
     * @return
     */
    public static String commonLeftAndRight(String idsA, String idsB) {
        String result = "";
        List<String> AList = StringUtils.isNotEmpty(idsA)? Arrays.asList(idsA.split(",")):new ArrayList<>();
        List<String> BList = StringUtils.isNotEmpty(idsB)? Arrays.asList(idsB.split(",")):new ArrayList<>();
        if(AList.size()==0||BList.size()==0) {
            return result;
        }
        for(String str : AList) {
            if(BList.contains(str)) {
                result = result+str+",";
            }else {
                continue;
            }
        }
        if(StringUtils.isNotEmpty(result)) {
            result = result.substring(0,result.length()-1);
        }
        return result;
    }

    public static void main(String args[]) {
        String sql = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
        List<String> result = splitByPerNum(sql,1);
        List<String> result2 = splitByPerNum(sql,2);
        List<String> result3 = splitByPerNum(sql,24);
        List<String> result4 = splitByPerNum(sql,26);
        List<String> result5 = splitByPerNum(sql,100);


        List<String> tempList = new ArrayList<>();
        for(int i =0;i<=2000;i++) {
            tempList.add(String.valueOf(i));
        }

        List<String> result6 = splitByPerNum(tempList,999);


        String id = "1,2";
        String ids1 = "12";
        String ids2 = "2,369,";
        String ids3 = "3,11,22";
        String ids4 = "1";
        String ids5 = "45,11,113,1";

        System.out.println(commonLeftAndRight(id,ids1));
        System.out.println(commonLeftAndRight(id,ids2));
        System.out.println(commonLeftAndRight(id,ids3));
        System.out.println(commonLeftAndRight(id,ids4));
        System.out.println(commonLeftAndRight(id,ids5));

        System.out.println("???");

    }


}
