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

import cc.smtweb.framework.core.common.SwEnum;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.db.vo.ModelCatalog;
import cc.smtweb.framework.core.db.vo.ModelProject;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.framework.core.util.JsonUtil;
import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.web.common.BpmEnum;
import cc.smtweb.system.bpm.web.design.db.ModelCatalogTreeHelper;
import cc.smtweb.system.bpm.web.design.db.ModelProjectCache;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageListHandler;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageLoadHandler;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageSaveHandler;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageTreeHandler;

import java.util.*;

/**
 * @Author: tanghp
 * @Date: 2022-08-01 17:47
 * @Desc:
 */
public class MenuHandler {
    private final static String Ds_menuCard = "menuCard";

    static class MenuLoadHandler extends DynPageLoadHandler {
        @Override
        public DynPageTreeHandler getTreeWorker(SwMap filter, PageDataset pageDataSet) {
            DynPageTreeHandler treeHandler = new MenuTreeHandler(form.getId(), filter, pageDataSet);
            if (pageDataSet.name.equals("mcTree")) {
                treeHandler = new MenuCatalogTreeHandler(form.getId(), filter, pageDataSet);
            }
            treeHandler.init(params, us);
            return treeHandler;
        }

        @Override
        public DynPageListHandler getListWorker(SwMap filter, PageDataset pageDataSet) {
            DynPageListHandler listHandler = new MenuListHandler(form.getId(), filter, pageDataSet);
            listHandler.init(params, us);
            return listHandler;
        }

        @Override
        protected void afterLoadBean(PageDataset dataset, SwMap bean) {
            super.afterLoadBean(dataset, bean);
            Menu menu = new Menu();
            menu.setData(bean);
            if (StringUtil.isNotEmpty(menu.getContent())) {
                Map<String, String> urlMap = JsonUtil.parseMap(menu.getContent());
                bean.put("sm_page_id", urlMap.get("pageId"));
                bean.put("sm_page_url", urlMap.get("path"));
            }
            if (StringUtil.isNotEmpty(menu.getRight())) {
                Map<String, List<String>> rightMap = JsonUtil.parseMap(menu.getRight());
                bean.put("sm_right_data", StringUtil.join(rightMap.get("data"), ","));
                bean.put("sm_right_func", StringUtil.join(rightMap.get("func"), ","));
                bean.put("sm_right_field", StringUtil.join(rightMap.get("field"), ","));
            }
        }
    }

    static class MenuSaveHandler extends DynPageSaveHandler {

        @Override
        protected DefaultEntity readBeanFromPage(PageDataset pageDataSet, SwMap data) {
            if (pageDataSet._isCurDataSet(Ds_menuCard)) {
                //处理请求内容
                SwMap sm_content = new SwMap();
                sm_content.put("pageId", data.readString("sm_page_id"));
                sm_content.put("path", data.readString("sm_page_url"));
                data.put("sm_content", JsonUtil.encodeString(sm_content));
                //处理权限点
                SwMap sm_right = new SwMap();
                sm_right.put("data", data.readStringSet("sm_right_data"));
                sm_right.put("func", data.readStringSet("sm_right_func"));
                sm_right.put("field", data.readStringSet("sm_right_field"));
                data.put("sm_right", JsonUtil.encodeString(sm_right));
                return super.readBeanFromPage(pageDataSet, data);
            }
            return super.readBeanFromPage(pageDataSet, data);
        }
    }

    static class MenuCatalogTreeHandler extends DynPageTreeHandler {
        private ModelCatalogTreeHelper mcTreeHelper = new ModelCatalogTreeHelper();

        public MenuCatalogTreeHandler(long pageId, SwMap filter, PageDataset pageDataSet) {
            super(pageId, filter, pageDataSet);
        }

        @Override
        protected List<SwMap> getChildren(long id) {
            List<SwMap> list = new ArrayList<>();
            buildTreeData(list, id);
            return list;
        }

        private void buildTreeData(List<SwMap> list, long id) {
            if (id <= 0) {
                Collection<ModelProject> mpList = ModelProjectCache.getInstance().getAll();
                for (ModelProject mp : mpList) {
                    SwMap row = new SwMap();
                    row.put("mc_id", mp.getId());
                    row.put("mc_name", mp.getName());
                    list.add(row);
                    /*List<SwMap> children = new ArrayList<>();
                    buildTreeData(children, mp.getId());
                    if (PubUtil.isEmpty(children)) continue;
                    row.put("children", children);*/
                }
                return;
            }
            ModelProject mp = ModelProjectCache.getInstance().get(id);
            if (mp != null) {
                List<ModelCatalog> mcList = mcTreeHelper.getChildren(0, mp.getId(), true, (o1, o2) -> StringUtil.chineseCompare(o1.getName(), o2.getName()));
                if (CommUtil.isEmpty(mcList)) return;
                for (ModelCatalog mc : mcList) {
                    SwMap row = new SwMap();
                    row.put("mc_id", mc.getId());
                    row.put("mc_name", mc.getName());
                    list.add(row);
                    /*List<SwMap> children = new ArrayList<>();
                    buildTreeData(children, mc.getId());
                    if (PubUtil.isEmpty(children)) continue;
                    row.put("children", children);*/
                }
                return;
            }
            List<ModelCatalog> mcList = mcTreeHelper.getChildren(id, (o1, o2) -> StringUtil.chineseCompare(o1.getName(), o2.getName()));
            if (CommUtil.isEmpty(mcList)) return;
            for (ModelCatalog mc : mcList) {
                SwMap row = new SwMap();
                row.put("mc_id", mc.getId());
                row.put("mc_name", mc.getName());
                list.add(row);
                /*List<SwMap> children = new ArrayList<>();
                buildTreeData(children, mc.getId());
                if (PubUtil.isEmpty(children)) continue;
                row.put("children", children);*/
            }
        }

        @Override
        protected long getId(SwMap bean) {
            return bean.readLong("mc_id");
        }

        @Override
        protected String getText(SwMap bean) {
            return bean.readString("mc_name");
        }
    }

    static class MenuTreeHandler extends DynPageTreeHandler {
        private ModelCatalogTreeHelper mcTreeHelper = new ModelCatalogTreeHelper();

        public MenuTreeHandler(long pageId, SwMap filter, PageDataset pageDataSet) {
            super(pageId, filter, pageDataSet);
        }

        @Override
        protected List<SwMap> getChildren(long id) {
            List<SwMap> list = new ArrayList<>();
            long prj_id = params.readFilter().readLong("project_id");
            if (prj_id <= 0) return list;
            Menu menu = MenuCache.getInstance().get(id);
            if (menu != null) return list;
            return buildTreeData(prj_id, id);
        }

        private List<SwMap> buildTreeData(long prj_id, long id) {
            List<SwMap> list = new ArrayList<>();
            List<ModelCatalog> mcList = mcTreeHelper.getChildren(id, prj_id, false, (o1, o2) -> StringUtil.chineseCompare(o1.getName(), o2.getName()));
            if (!CommUtil.isEmpty(mcList)) {
                for (ModelCatalog mc : mcList) {
                    List<SwMap> children = buildTreeData(prj_id, mc.getId());
                    if (!CommUtil.isEmpty(children)) {
                        MenuNode menuNode = new MenuNode();
                        menuNode.setMenu(mc.getId());
                        menuNode.setLabel(mc.getName());
                        menuNode.setChildren(children);
                        list.add(menuNode.getData());
                    }
                    Set<Menu> menus = MenuCache.getInstance().getByM(String.valueOf(mc.getId()));
                    if (!CommUtil.isEmpty(menus)) {
                        for (Menu m : menus) {
                            MenuNode menu = new MenuNode();
                            menu.setMenu(m.getId());
                            menu.setLabel(m.getName());
                            menu.setIcon(m.getIcon());
                            menu.setSeq(m.getSeq());
                            list.add(menu.getData());
                        }
                    }
                }
                return list;
            }
            return list;
        }

        @Override
        protected long getId(SwMap bean) {
            return bean.readLong("menu");
        }

        @Override
        protected String getText(SwMap bean) {
            return bean.readString("label");
        }
    }

    static class MenuListHandler extends DynPageListHandler {

        public MenuListHandler(long pageId, SwMap filter, PageDataset pageDataSet) {
            super(pageId, filter, pageDataSet);
        }

        @Override
        protected void afterQuery(List<SwMap> listData) {
            super.afterQuery(listData);
            listData.forEach(row -> {
                row.put("sm_type_text", BpmEnum.MenuType.instance.getName(row.readInt("sm_type")));
            });
        }
    }
}
