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

import cc.smtweb.framework.core.common.SwConsts;
import cc.smtweb.framework.core.common.SwEnum;
import cc.smtweb.framework.core.exception.SwException;
import cc.smtweb.system.bpm.web.design.form.ModelForm;
import cc.smtweb.system.bpm.web.design.form.ModelFormHelper;
import cc.smtweb.system.bpm.web.design.form.model.flow.*;
import org.apache.commons.lang3.StringUtils;

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

/**
 * Created by Akmm at 2022-08-14 10:02
 * 不同模型，在保存时候的个性处理
 */
public class ModelFactory {
    private static ModelFactory instance;
    private static Map<String, BaseModelWorker> mapWorker;

    static {
        instance = new ModelFactory();
        mapWorker = new HashMap<>();
        mapWorker.put(SwConsts.DEF_ROOT_ID, new BaseModelWorker());
        mapWorker.put(SwEnum.ModelType.LC_SINGLE.value, new LcSingleWorker());
        mapWorker.put(SwEnum.ModelType.LC_NORMAL.value, new LcNormalWorker());
        mapWorker.put(SwEnum.ModelType.LC_LT.value, new LcLtWorker());
        mapWorker.put(SwEnum.ModelType.LC_LT_2.value, new LcLt2Worker());
        mapWorker.put(SwEnum.ModelType.LC_MS.value, new LcMsWorker());
        mapWorker.put(SwEnum.ModelType.LC_LT_MS.value, new LcLtMsWorker());
        mapWorker.put(SwEnum.ModelType.LC_LC_1.value, new LcLc1Worker());
        mapWorker.put(SwEnum.ModelType.LC_LC_2.value, new LcLc2Worker());
        mapWorker.put(SwEnum.ModelType.FLOW_SINGLE.value, new FlowSingleWorker());
        mapWorker.put(SwEnum.ModelType.FLOW_LC_LC_1.value, new FlowLcLc1Worker());
        mapWorker.put(SwEnum.ModelType.FLOW_LC_LC_2.value, new FlowLcLc2Worker());
        mapWorker.put(SwEnum.ModelType.FLOW_LC_LT.value, new FlowLcLtWorker());
        mapWorker.put(SwEnum.ModelType.FLOW_LC_MS.value, new FlowLcMsWorker());
        mapWorker.put(SwEnum.ModelType.FLOW_LT_MS.value, new FlowLtMsWorker());
        mapWorker.put(SwEnum.ModelType.EMPTY.value, new EmptyWorker());
    }

    public static ModelFactory getInstance() {
        return instance;
    }

    private BaseModelWorker getWorker(ModelForm bean) {
        String model = ModelFormHelper.getFormModel(bean);
        if (StringUtils.isEmpty(model)) return mapWorker.get(SwConsts.DEF_ROOT_ID);
        BaseModelWorker worker = mapWorker.get(model);
        if (worker == null) throw new SwException("该模型暂无接口实现！");
        return worker;
    }

    //各模型的个性化保存实现
    public void saveForm(ModelForm bean, List<ModelForm> listFormChild) {
        BaseModelWorker worker = getWorker(bean);
        worker.saveForm(bean, listFormChild);
    }

    //构建模板
    public void buildSaveModel(ModelForm bean) {
        BaseModelWorker worker = getWorker(bean);
        worker.buildSaveModel(bean);
    }
}
