package cc.smtweb.system.bpm.web.sys.dev;

import cc.smtweb.framework.core.common.AbstractEnum;
import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.mvc.service.SqlPara;
import cc.smtweb.framework.core.mvc.service.SwListData;
import cc.smtweb.framework.core.util.StringUtil;
import cc.smtweb.system.bpm.web.design.db.ModelProjectCache;
import cc.smtweb.system.bpm.web.design.form.ModelForm;
import cc.smtweb.system.bpm.web.design.form.ModelFormCache;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;
import cc.smtweb.system.bpm.web.design.form.define.PageDatasets;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageHelper;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageListHandler;
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageLoadHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: tanghp
 * @Date: 2022-08-01 17:47
 * @Desc:
 */
public class ComboHandler {

    static class FormLoadHandler extends DynPageLoadHandler {

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

    static class FieldLoadHandler extends DynPageLoadHandler {

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

    static class EnumLoadHandler extends DynPageLoadHandler {

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


    static class EnumListHandler extends DynPageListHandler {

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

        @Override
        public SwListData buildListData() {
            String cls = params.readMap("filter").readString("cls");
            if (StringUtil.isEmpty(cls)) return SwListData.create(new ArrayList(), 0);
            List<SwMap> list = new ArrayList<>();
            try {
                AbstractEnum e;
                try {
                    e = (AbstractEnum) Class.forName(cls).getDeclaredField("inst").get(null);
                } catch (Exception e1) {
                    e = (AbstractEnum) Class.forName(cls).getDeclaredField("instance").get(null);
                }
                for (Object v : e.mapAll.values()) {
                    AbstractEnum.EnumBean bean = (AbstractEnum.EnumBean) v;
                    SwMap row = new SwMap();
                    row.put("value", bean.value.toString());
                    row.put("name", bean.name);
                    list.add(row);
                }
            } catch (Exception ue) {
                return SwListData.create(new ArrayList(), 0);
            }
            return SwListData.create(list, 0);
        }

        @Override
        public R getTotal() {
            return R.success(buildListData().getTotal());
        }

    }


    static class FieldListHandler extends DynPageListHandler {

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

        @Override
        public SwListData buildListData() {
            ModelForm mf = ModelFormCache.getInstance().get(filter.readLong("f_page"));
            if (mf == null) return SwListData.create(new ArrayList(), 0);
            PageDatasets pageDatasets = mf.getDatasets();
            if (pageDatasets == null) return SwListData.create(new ArrayList(), 0);
            Map<String, SwMap> map = new HashMap<>();
            pageDatasets.list.forEach(data -> {
                data.fields.forEach(field -> {
                    if (map.containsKey(field.field)) return;
                    SwMap swMap = new SwMap();
                    swMap.put("f_id", field.field);
                    swMap.put("f_name", field.label);
                    map.put(field.field, swMap);
                });
            });
            return SwListData.create(new ArrayList<SwMap>(map.values()), 0);
        }

        @Override
        public R getTotal() {
            return R.success(buildListData().getTotal());
        }

    }


    static class FormListHandler extends DynPageListHandler {

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

        @Override
        protected SqlPara buildSqlPara() {
            SwMap swMap = (SwMap) filter.clone();
            swMap.remove("mf_right");
            return DynPageHelper.buildSelectSql(pageDataSet, swMap,us);
        }

        @Override
        protected void afterQuery(List<SwMap> listData) {
            super.afterQuery(listData);
            listData.forEach(row -> {
                row.put("mf_url", getPageUrl(row.readLong("mf_prj_id"), row.readString("mf_name")));
            });
        }
    }

    public static String getPageUrl(long prj_id, String mf_name) {
        return "/" + ModelProjectCache.getInstance().getModule(prj_id) + "." + mf_name;
    }
}
