package com.prolog.eis.utils;

import org.apache.commons.collections.map.HashedMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author wangkang
 * @Description
 * @CreateTime 2021/7/28 13:19
 */
public class MathUtils {

   /* public static void main(String[] args) throws Exception {
        Map map = new HashedMap();
        List<String> list1 = new ArrayList<>();
        list1.add("a");
        list1.add("b");
        map.put(1, list1);

        List<String> list2 = new ArrayList<>();
        list2.add("c");
        list2.add("d");
        map.put(2, list2);

        List<String> list8 = new ArrayList<>();
        list8.add("c");
        list8.add("d");
        map.put(8, list8);

        List<String> list9 = new ArrayList<>();
        list9.add("c");
        list9.add("d");
        map.put(9, list9);


        List<String> list3 = new ArrayList<>();
        list3.add("a");
        list3.add("c");
        map.put(3, list3);


        List<String> list4 = new ArrayList<>();
        list4.add("b");
        list4.add("a");
        map.put(4, list4);


        List<String> list5 = new ArrayList<>();
        list5.add("e");
        list5.add("b");
        map.put(5, list5);


        List<String> list6 = new ArrayList<>();
        list6.add("d");
        list6.add("c");
        map.put(6, list6);


        List<String> list7 = new ArrayList<>();
        list7.add("c");
        list7.add("f");
        map.put(7, list7);

        List<String> list10 = new ArrayList<>();
        list10.add("a");
        list10.add("c");
        list10.add("e");
        map.put(10, list10);

        List<String> list11 = new ArrayList<>();
        list11.add("b");
        list11.add("g");
        map.put(11, list11);

        List<String> list12 = new ArrayList<>();
        list12.add("b");
        list12.add("f");
        map.put(12, list12);

        List<String> list13 = new ArrayList<>();
        list13.add("b");
        list13.add("f");
        map.put(13, list13);

        List<String> list14 = new ArrayList<>();
        list14.add("b");
        list14.add("f");
        map.put(14, list14);

        List<String> list15 = new ArrayList<>();
        list15.add("b");
        list15.add("f");
        map.put(15, list15);

        List list = computerOrder(map, null, 8);
        System.out.println(JsonUtils.toString(list));

    }*/

    public static List<Integer> computerOrder(Map<Integer, List<String>> upPriority,
                                              Map<Integer, List<String>> downPriority,
                                              Integer num) throws Exception {
        if (num < 2) {
            throw new Exception("所需数量小于2，无需技术重合度");
        }
        List<Integer> list = new ArrayList<>(num);
        if (downPriority == null) {
            Map copyMap = new HashedMap();
            copyMap.putAll(upPriority);
            Map<Integer, Integer> rate = new HashedMap();
            for (Integer i : upPriority.keySet()) {
                int weight = 0;
                for (Integer j : upPriority.keySet()) {
                    if (i.equals(j)) {
                        continue;
                    }
                    List<String> stringList1 = upPriority.get(i);
                    List<String> stringList2 = upPriority.get(j);
                    List<String> collect =
                            stringList1.stream().filter(s -> stringList2.contains(s)).collect(Collectors.toList());
                    weight = weight + collect.size();
                }
                rate.put(i, weight);
            }
            int key = getMaxValueKey(rate);
            System.out.println(key);
            list.add(key);
            while (list.size() < num) {
                Map newRate = new HashedMap();
                copyMap.forEach((k, v) -> {
                    int newWeight = 0;
                    boolean flag = true;
                    for (Integer integer : list) {
                        if (k.equals(integer)) {
                            flag = false;
                            continue;
                        }
                        for (String string : (List<String>) copyMap.get(integer)) {
                            if (v.toString().contains(string)) {
                                newWeight = newWeight + 1;
                            }
                        }
                    }
                    if (flag) {
                        newRate.put(k, newWeight);
                    }
                });
                int newKey = getMaxValueKey(newRate);
                list.add(newKey);
            }
        } else {
            if (null != upPriority) {
                list.addAll(upPriority.keySet());
            }
            while (list.size() < num) {
                Map newRate = new HashedMap();
                downPriority.forEach((k, v) -> {
                    int newWeight = 0;
                    boolean flag = true;
                    for (Integer integer : list) {
                        if (k.equals(integer)) {
                            flag = false;
                            continue;
                        }
                        if (downPriority.get(integer) == null) {
                            continue;
                        }
                        for (String string : (List<String>) downPriority.get(integer)) {
                            if (v.toString().contains(string)) {
                                newWeight = newWeight + 1;
                            }
                        }
                    }
                    if (flag) {
                        newRate.put(k, newWeight);
                    }
                });
                int newKey = getMaxValueKey(newRate);
                list.add(newKey);
            }

        }
        return list;
    }

    private static int getMaxValueKey(Map<Integer, Integer> rate) {
        int index = 0;
        Integer[] integers = new Integer[rate.keySet().size()];
        for (Integer integer : rate.keySet()) {
            integers[index] = integer;
            index++;
        }
        int key = 0;
        if (integers.length > 0) {
            key = integers[0];
            for (int i = 1; i < integers.length; i++) {
                if (rate.get(key) < rate.get(integers[i])) {
                    key = integers[i];
                }
            }
        }
        return key;
    }
}
