package com.tofly.common.core.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class JSONUtil {
	
	public static HashMap<String, Object> reflect(JSONObject json){
        HashMap<String, Object> map = new HashMap<String, Object>();
        Set keys = json.keySet();
        for(Object key : keys){
            Object o = json.get(key);
            if(o instanceof JSONArray){
                map.put((String) key, reflect((JSONArray) o));
            }
            else if(o instanceof JSONObject){
                map.put((String) key, reflect((JSONObject) o));
            }
            else{
                map.put((String) key, o);
            }

        }
        return map;
    }

    public static Map<String, Object> reflectlinkedHashMap(JSONObject json){
        HashMap<String, Object> map = new LinkedHashMap<>();
        Set keys = json.keySet();
        for(Object key : keys){
            Object o = json.get(key);
            if(o instanceof JSONArray){
                map.put((String) key, reflect((JSONArray) o));
            }
            else if(o instanceof JSONObject){
                map.put((String) key, reflect((JSONObject) o));
            }
            else{
                map.put((String) key, o);
            }

        }
        return map;
    }

    /**
     * 将JSONArray对象转换成List集合
     * @param json
     * @return
     */
    public static List<Object> reflect(JSONArray json){
    	
        List<Object> list = new ArrayList<Object>();
        for(Object o : json){
            if(o instanceof JSONArray){
                list.add(reflect((JSONArray) o));
            }
            else if(o instanceof JSONObject){
                list.add(reflect((JSONObject) o));
            }
            else{
                list.add(o);
            }

        }
        return list; 
    }

    public static Map getJSONObjectLinkHashMap(JSONObject json,String key) {
        Object value = json.getInnerMap().get(key);
        if (value instanceof JSONObject) {
            return new JSONObject(true);
        } else {
            return value instanceof String ? JSON.parseObject((String)value) : (JSONObject)JSON.toJSON(value);
        }
    }

    /**
     * 将数据解析为树状机构，实体类必须有3个参数，id，数据唯一id值，parentid父节点Id，child子集
     * @param list  所有数据
     * @param id   唯一id
     * @param parent 父节点id
     * @param children 子集
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T,R> List<T> parTree(List<? extends T> list, Function<? super T, ? extends R> id,
                                        Function<? super T, ? extends R> parent,
                                        Function<? super T, ? extends R> children){
        if(list==null || list.size()==0){
            return new ArrayList();
        }
        List<T> list1=new ArrayList();
//        Map<Object, T> collect = list.stream().collect(Collectors.toMap(id, t -> t));
        LinkedHashMap<? extends R, ? extends List<? extends T>> collect1 = list.stream().collect(Collectors.groupingBy(id, LinkedHashMap::new, Collectors.toList()));
        Iterator<? extends R> iterator = collect1.keySet().iterator();
        while (iterator.hasNext()) {
            T t = collect1.get(iterator.next()).get(0);
            Object parentObject = parent.apply(t);
            if(parentObject==null){
                list1.add(t);
            }else{
                List list2=collect1.get(parentObject);
                if(list2!=null && list2.size()>0){
                    T parentT=(T)list2.get(0);
                    List<T> childrenList = (List<T>)children.apply(parentT);
                    childrenList.add(t);
                }else{
                    list1.add(t);
                }
            }
        }
//        Iterator<Object> iter = collect.keySet().iterator();
//        while (iter.hasNext()) {
//            T t = collect.get(iter.next());
//            Object parentObject = parent.apply(t);
//            if(parentObject==null){
//                list1.add(t);
//            }else{
//                T parentT=collect.get(parentObject);
//                if(parentT!=null){
//                    List<T> childrenList = (List<T>)children.apply(parentT);
//                    childrenList.add(t);
//                }
//            }
//        }
        return list1;
    }


}
