package cc.smtweb.system.bpm.web.design.form;

import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwConsts;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.mvc.service.DefaultLoadHandler;
import cc.smtweb.framework.core.util.FileUtil;
import cc.smtweb.framework.core.util.JsonUtil;
import cc.smtweb.system.bpm.web.sys.base.billType.BillTypeCache;
import org.apache.commons.lang3.StringUtils;

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

/**
 * Created by Akmm at 2022/5/9 16:17
 */
public class ModelFormLoadHandler extends DefaultLoadHandler<ModelForm> {
    public ModelFormLoadHandler() {
        super(ModelForm.ENTITY_NAME);
    }

    protected ModelForm localLoadForm() {
        String id = params.readString("id");
        if (StringUtils.isEmpty(id)) id = params.readString("pageId");
        if (StringUtils.isEmpty(id)) id = params.readString("pageName");
        ModelForm bean = ModelFormHelper.getFromCache(id);
        if (bean == null) throw new BizException("没有找到指定的页面定义信息！id=" + id);
        long billType = bean.getBillType();
        if (billType > 0L) {
            SwMap opts = bean.getOpts();
            opts.put(ModelFormHelper.KEY_BILL_TYPE + SwConsts.TEXT_SUFFIX, BillTypeCache.getInstance().getName(billType));
            bean.setOption(JsonUtil.encodeString(opts));
        }

        return bean;
    }

    @Override
    public R load() {
        return R.success(localLoadForm());
    }

    //页面设计-加载数据集定义
    public R loadDataset() {
        ModelForm bean = localLoadForm();
        return R.success(ModelFormHelper.buildReqDataset(bean.getDatasets()));
    }

    //页面设计 - 加载页面model定义
    public R loadModel() {
        ModelForm bean = localLoadForm();
        return R.success(ModelFormHelper.buildReqModel(bean));
    }

    //页面引擎-获取页面模型
    public R loadForm() {
        ModelForm bean = localLoadForm();
        return R.success(ModelFormHelper.buildEngineModel(bean, params, us));
    }

    //获取页面使用的控件的filter信息
    public R loadWidgetFilter() {
        ModelForm bean = localLoadForm();
        return R.success(ModelFormHelper.buildWidgetFilter(bean));
    }

    //加载页面js方法
    public R loadJsFuncs() {
        ModelForm form = localLoadForm();

        List<String> listRet = new ArrayList<>();
        R r = R.success(listRet);

        String codeJavaPath = ModelFormHelper.getCodePath(form);
        if (StringUtils.isEmpty(codeJavaPath)) {
            return r;
        }
        String path = ModelFormHelper.getCodePath(form) + "/src/main/resources/static/event/" + ModelFormHelper.getEventPath(form).replaceAll("\\.", "/") + "/" + form.getName() + ".js";
        String js = FileUtil.readFileStr(path);
        if (StringUtils.isEmpty(js)) {
            return r;
        }
        //解析，比如以【return 】
        int index = js.lastIndexOf("return ");
        if (index < 0) {
            return r;
        }
        index = js.indexOf("{", index);
        js = js.substring(index + 1, js.indexOf("}", index)).trim();
        String[] fs = js.split(",");
        for (String s : fs) {
            if (StringUtils.isNotBlank(s)) {
                listRet.add(s.trim());
            }
        }
        return R.success(listRet);
    }
}
