package com.ruoyi.web.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ie.domain.AEnrollScore;
import com.ruoyi.ie.domain.AMarjorPlan;
import com.ruoyi.ie.domain.AMarjorSubmit;

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

public class VoluntaryRuleHelper {


    public boolean isMatchMajor(AEnrollScore r, String needMajor, String needDirect, String inclMajorDirection, String exclMajorDirection) {
        if (StringUtils.isNotBlank(r.getMajorNames())) {
            if (!r.getMajorNames().contains(needMajor)) {
                return false;
            }
            if (StringUtils.isNotBlank(r.getMajorDirections())) { // 有单独的方向时，分开判断
                if (StringUtils.isBlank(needDirect) || !r.getMajorDirections().contains(needDirect)) {
                    return false;
                }
            } else if (StringUtils.isNotBlank(needDirect)) { // 有direct要求时，这里需要有 "<专业>(<方向>)
                if (!r.getMajorNames().contains(inclMajorDirection)) {
                    return false;
                }
            } else if (r.getMajorNames().contains(exclMajorDirection)) { // 当无direct要求时，这时不能有 "<专业>("
                return false;
            }
        }
        return true;
    }
    public Map<Boolean, List<AEnrollScore>> buildMajorEnrollScoresMap(List<AEnrollScore> scoreList, String needGroup) {
        Map<Boolean, List<AEnrollScore>> majorEnrollScoresMap = scoreList.stream()
                .filter(t -> needGroup.equals(StringUtils.trimToEmpty(t.getMajorGroups())))
                .collect(Collectors.groupingBy(t -> StringUtils.isNotBlank(t.getMajorNames())));
        if(majorEnrollScoresMap.isEmpty() && StringUtils.isNotBlank(needGroup)) {
            majorEnrollScoresMap = scoreList.stream()
                    .filter(t -> StringUtils.isBlank(t.getMajorGroups()))
                    .collect(Collectors.groupingBy(t -> StringUtils.isNotBlank(t.getMajorNames())));

        }
        return majorEnrollScoresMap;
    }

    public List<AMarjorSubmit> filtValidSubmits(String needMajorDirect, List<AMarjorSubmit> submitList, Set<String> existSet) {
        List<AMarjorSubmit> validSubmitList = Lists.newArrayList();
        if (null != submitList) {
            for (AMarjorSubmit s : submitList) { // 历年录取通过专业分来判断差别
                String majorNameDirect = s.getMajorName() + StringUtils.trimToEmpty(s.getMajorDirection());
                if (null != s.getScore() && needMajorDirect.equals(majorNameDirect) && existSet.add(needMajorDirect + s.getYear() + s.getEnrollType())) {
                    validSubmitList.add(s);
                }
            }
        }
        return validSubmitList;
    }

    public List<AMarjorPlan> sortPlans(List<AMarjorPlan> historyPlanList) {
        if (null != historyPlanList) {
            Collections.sort(historyPlanList, new Comparator<AMarjorPlan>() {
                @Override
                public int compare(AMarjorPlan o1, AMarjorPlan o2) {
                    int iRet;
                    if (0 != (iRet = o1.getYear().compareTo(o2.getYear()))) {
                        return -iRet;
                    }
                    if (0 != (iRet = o1.getMajorName().compareTo(o2.getMajorName()))) {
                        return -iRet;
                    }
                    if (null == o1.getMajorDirection()) {
                        if (null != o2.getMajorDirection()) {
                            return 1;
                        }
                        return 0;
                    } else if (null == o2.getMajorDirection()) {
                        return -1;
                    }
                    return o1.getMajorDirection().compareTo(o2.getMajorDirection());
                }
            });
        } else {
            historyPlanList = Lists.newArrayList();
        }
        return historyPlanList;
    }
    /**
     *
     * @param submitList
     * @return
     */
    public List<AMarjorSubmit> sortSubmits(List<AMarjorSubmit> submitList) {
        if (null != submitList) {
            Collections.sort(submitList, new Comparator<AMarjorSubmit>() {
                @Override
                public int compare(AMarjorSubmit o1, AMarjorSubmit o2) {
                    int iRet;
                    if (0 != (iRet = o1.getYear().compareTo(o2.getYear()))) {
                        return -iRet;
                    }
                    if (0 != (iRet = o1.getMajorName().compareTo(o2.getMajorName()))) {
                        return -iRet;
                    }
                    if (null == o1.getMajorDirection()) {
                        if (null != o2.getMajorDirection()) {
                            return 1;
                        }
                        return 0;
                    } else if (null == o2.getMajorDirection()) {
                        return -1;
                    }
                    return 0;
                }
            });
        } else {
            submitList = Lists.newArrayList();
        }
        return submitList;
    }
}
