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

import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.jdbc.IDbWorker;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.mvc.service.AbstractHandler;
import cc.smtweb.framework.core.mvc.service.SwListData;
import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.framework.core.util.JsonUtil;
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.DynPageProvider;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageTreeHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: tanghp
 * @Date: 2022-08-01 18:10
 * @Desc:
 */
public class MenuPlanHandler {
    // 菜单列表数据集
    private static String DS_MENU_LIST = "menuList";
    private static String DS_MENU_CARD = "menuCard";
    private static String DS_MENU_TREE = "menuTree";

    static class MenuCustHandler extends AbstractHandler {
        // 保存方案明细
        public R savePlanItem() {
            // 方案ID
            long plan_id = params.readLong("plan_id");
            if (plan_id <= 0) {
                throw new BizException("所属方案不能为空！");
            }
            MenuPlan menuPlan = MenuPlanCache.getInstance().get(plan_id);
            if (menuPlan == null) {
                throw new BizException("所属方案不能为空！");
            }
            long parent_id = params.readLong("parent_id", -1L);
            MenuPlanItem menuPlanItem = JsonUtil.parse(params, MenuPlanItem.class);
            // 处理content大字段
            MenuPlanContent menuPlanContent = new MenuPlanContent(menuPlan.getContent());
            menuPlanContent.saveItem(menuPlanItem, parent_id);
            menuPlan.setContent(menuPlanContent.toContentStr());

            DbEngine.getInstance().doTrans(new IDbWorker() {
                @Override
                public void work() {
                    DbEngine.getInstance().updateEntity(menuPlan, "smp_content");
                }

                @Override
                public void doAfterDbCommit() {
                    MenuPlanCache.getInstance().put(menuPlan);
                }
            });
            return R.success(menuPlanItem);
        }

        // 删除方案明细
        public R delPlanItem() {
            // 方案ID
            long plan_id = params.readLong("plan_id");
            long id = params.readLong("id");
            if (plan_id <= 0 || id <= 0) {
                throw new BizException("参数错误！");
            }
            MenuPlan menuPlan = MenuPlanCache.getInstance().get(plan_id);
            if (menuPlan == null) {
                throw new BizException("参数错误！");
            }
            // 处理content大字段
            MenuPlanContent menuPlanContent = new MenuPlanContent(menuPlan.getContent());
            menuPlanContent.delItem(id);
            menuPlan.setContent(menuPlanContent.toContentStr());

            DbEngine.getInstance().doTrans(new IDbWorker() {
                @Override
                public void work() {
                    DbEngine.getInstance().updateEntity(menuPlan, "smp_content");
                }

                @Override
                public void doAfterDbCommit() {
                    MenuPlanCache.getInstance().put(menuPlan);
                }
            });
            return R.success();
        }
    }

    static class MenuLoadHandler extends DynPageLoadHandler {
        @Override
        public DynPageProvider getProvider() {
            return new MenuProvider();
        }

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

        @Override
        public DynPageTreeHandler getTreeWorker(SwMap filter, PageDataset pageDataSet) {
            MenuTreeHandler treeHandler = new MenuPlanHandler.MenuTreeHandler(form.getId(), filter, pageDataSet);
            treeHandler.init(params, us);
            return treeHandler;
        }

        @Override
        protected void afterAddBean(PageDataset dataset, SwMap bean) {
            super.afterAddBean(dataset, bean);
            if (DS_MENU_CARD.equals(dataset.name)) {
                bean.put("type", 0);
            }
        }
    }

    static class MenuListHandler extends DynPageListHandler {
        public MenuListHandler(long pageId, SwMap filter, PageDataset pageDataSet) {
            super(pageId, filter, pageDataSet);
        }

        @Override
        public SwListData buildListData() {
            long plan_id = filter.readLong("plan_id", -1L);
            List<MenuPlanItem> children = getChildren();
            List<SwMap> list = new ArrayList<>();
            int total = 0;
            if (!CommUtil.isEmpty(children)) {
                total = children.size();
                for (MenuPlanItem bean : children) {
                    SwMap map = bean.getData();
                    map.put("plan_id", plan_id);
                    map.put("type", bean.getMenu() > 0 ? 1: 0);
                    map.put("type_text", bean.getMenu() > 0 ? "菜单": "目录");
                    list.add(map);
                }
            }
            return SwListData.create(list, total);
        }

        @Override
        public R getTotal() {
            SwMap r = new SwMap();
            List<MenuPlanItem> children = getChildren();
            r.put("total", children.size());
            return R.success(r);
        }

        private List<MenuPlanItem> getChildren() {
            long plan_id = filter.readLong("plan_id", -1L);
            long parent_id = filter.readLong("parent_id", -1L);
            if (plan_id <= 0) {
                return new ArrayList<>();
            }
            MenuPlan menuPlan = MenuPlanCache.getInstance().get(plan_id);
            if (menuPlan == null) {
                return new ArrayList<>();
            }
            MenuPlanContent menuPlanContent = new MenuPlanContent(menuPlan.getContent());
            return menuPlanContent.getChildren(parent_id);
        }
    }

    static class MenuProvider extends DynPageProvider {
        @Override
        public SwMap loadData(SwMap filter, PageDataset pageDataSet) {
            if (DS_MENU_CARD.equals(pageDataSet.name)) {
                long id = filter.readLong("id", 1L);
                long plan_id = filter.readLong("plan_id", 1L);
                MenuPlan menuPlan = MenuPlanCache.getInstance().get(plan_id);
                if (menuPlan == null) {
                    return new SwMap();
                }
                MenuPlanContent menuPlanContent = new MenuPlanContent(menuPlan.getContent());
                MenuPlanItem menuPlanItem = menuPlanContent.findById(id);
                MenuPlanItem parent;
                if (menuPlanItem == null) {
                    return new SwMap();
                } else {
                    parent = menuPlanContent.findParentById(menuPlanItem.getId());
                }
                SwMap r = new SwMap();
                r.putAll(menuPlanItem.getData());
                r.put("plan_id", plan_id);
                r.put("type", menuPlanItem.getMenu() > 0 ? 1: 0);
                r.put("parent_id", parent == null ? -1: parent.getId());
                return r;
            }
            return super.loadData(filter, pageDataSet);
        }
    }

    static class MenuTreeHandler extends DynPageTreeHandler {
        private MenuPlanContent menuPlanContent;

        public MenuTreeHandler(long pageId, SwMap filter, PageDataset pageDataSet) {
            super(pageId, filter, pageDataSet);
            long plan_id = filter.readLong("plan_id", -1L);
            if (plan_id <= 0) {
                menuPlanContent = new MenuPlanContent(null);
            }
            MenuPlan menuPlan = MenuPlanCache.getInstance().get(plan_id);
            menuPlanContent = new MenuPlanContent(menuPlan == null ? null: menuPlan.getContent());
        }

        @Override
        protected List<SwMap> getChildren(long id) {
            boolean exc_menu = params.readBool("exc_menu");
            List<SwMap> list = new ArrayList<>();
            List<MenuPlanItem> temList = menuPlanContent.getChildren(id);
            if (!CommUtil.isEmpty(temList)) {
                temList.forEach(bean -> {
                    if (exc_menu && bean.getMenu() > 0) {
                        return;
                    }
                    list.add(bean.getData());
                });
            }
            return list;
        }

        protected void buildMenuRight(String menu_id, SwMap bean) {

        }

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

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

}
