package cc.smtweb.system.bpm.web.sys.user.menuPlan;

import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.web.design.form.ModelForm;
import cc.smtweb.system.bpm.web.design.form.ModelFormCache;

import java.util.*;

/**
 * @Author: tanghp
 * @Date: 2022-09-26 10:19
 * @Desc: 菜单方案辅助类
 */
public class MenuPlanHelper {
    /**
     * 获取命中菜单方案明细ID
     * @param menuId 菜单方案明细ID
     * @param pageId 页面ID
     * @param fullPath 带参数的路径
     * @return
     */
    public static long findMenuId(long menuId,long pageId,String fullPath){
        MenuPlanItemProvider.MenuPlanItem2 mItem = MenuPlanItemProvider.getInstance().getMenuPlanItem(menuId);
        if(mItem==null)return 0L;
        // 1、如果是本页面的菜单 直接返回菜单ID
        if(mItem.getPageId() == pageId){
            return menuId;
        }
        // 获取本菜单对应的模块包含的所有页面
        Set<ModelForm>  children = ModelFormCache.getInstance().getIncPage(mItem.getPageId());
        if(!CommUtil.isEmpty(children)){
            for(ModelForm form : children){
                //2、本页属于本菜单模块的子页面 也返回该菜单
                if(form.getId() == pageId){
                    return menuId;
                }
            }
        }
        // 获取包含本页面的所有的菜单
        List<MenuPlanItemProvider.MenuPlanItem2> pItems = findItemIncPage(mItem.getMpId(),pageId);
        // 解析fullPath
        UrlPath urlPath = new UrlPath(fullPath);
        Map<Long, Map<String,Integer>> countMap = new HashMap<>();
        for(MenuPlanItemProvider.MenuPlanItem2 item: pItems){
            Map<String,Integer> map = new HashMap<>();
            map.put("total",item.getParameterNames().size());
            map.put("count",0);
            item.getParameterNames().forEach(name->{
                if(urlPath.getParamNames().contains(name)){
                    map.put("count",map.get("count")+1);
                }
            });
            countMap.put(item.getId(),map);
            // 3.参数总个数和命中个数 和配置一致直接返回
            if(map.get("total").intValue() == map.get("count").intValue()&& map.get("count")==urlPath.paramNames.size()){
                return item.getId();
            }
        }
        // 记录最大命中数的菜单
        int count = 0;
        long rtMenuId=0L;
        for(Map.Entry<Long,Map<String,Integer>> item:  countMap.entrySet()){
            // 4、参数命中个数 和 url的参数个数一致 也返回
            if(item.getValue().get("count")==urlPath.paramNames.size()){
                return item.getKey();
            }
            // 找最大命中数的菜单
            if(count<item.getValue().get("count")){
                count = item.getValue().get("count");
                rtMenuId = item.getKey();
            }
        }
        // 5.返回最大命中数的菜单
        return rtMenuId;
    }

    /**
     * 获取包含指定页面的所有明细
     * @param mpId 菜单方案ID
     * @param pageId 页面ID
     * @return 明细集合
     */
    public static List<MenuPlanItemProvider.MenuPlanItem2> findItemIncPage(long mpId, long pageId){
        MenuPlan menuPlan = MenuPlanCache.getInstance().get(mpId);
        if(menuPlan==null){
            return new ArrayList<>();
        }
        List<MenuPlanItemProvider.MenuPlanItem2> rtList = new ArrayList<>();
        List<MenuPlanItem> tempList = new ArrayList<>();
        MenuPlanContent menuPlanContent = new MenuPlanContent(menuPlan.getContent());
        menuPlanContent.findPage(pageId,tempList);
        tempList.forEach(item->{
            MenuPlanItemProvider.MenuPlanItem2 bean = new MenuPlanItemProvider.MenuPlanItem2();
            bean.getData().putAll(item.getData());
            bean.setMpId(mpId);
        });
        return rtList;
    }
    static class UrlPath{
        private String path;
        private Set<String> paramNames = new HashSet<>();

        public UrlPath(String urlPath) {
            String[] array = urlPath.split("\\?");
            if(array.length>0){
                path = array[0];
            }
            if(array.length>1){
                String[] pArray = array[1].split("&");
                for(String p1 : pArray){
                    String[] param = p1.split("=");
                    if(param.length>0){
                        String p = param[0];
                        if(StringUtil.isNotEmpty(p)){
                            paramNames.add(p);
                        }
                    }
                }
            }
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public Set<String> getParamNames() {
            return paramNames;
        }

        public void setParamNames(Set<String> paramNames) {
            this.paramNames = paramNames;
        }
    }
}
