package cn.stylefeng.team.project.modular.businessrender.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.entity.FormItemTable;
import cn.stylefeng.team.form.api.enums.FormComponentEnum;
import cn.stylefeng.team.form.api.pojo.FormDesignVo;
import cn.stylefeng.team.meta.api.MetaModelServiceApi;

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

public class FormDataFactory {

    public static Map<String, Map<String, Object>> createFormData(List<FormDesignVo> businessFormDesignList, Map<String, List<Map<String, Object>>> businessData) {

        Map<String, Map<String, Object>> result = new HashMap<>();

        for (FormDesignVo formDesignVo : businessFormDesignList) {

            Map<String, Object> thisFormDefinitionData = new HashMap<>();

            List<FormItem> formItemList = formDesignVo.getFormItemList();

            List<FormItem> newExpandFormItemList = new ArrayList<>();
            expandFormItem(formItemList, newExpandFormItemList);

            for (FormItem formItem : newExpandFormItemList) {
                if (ObjectUtil.isEmpty(formItem.getFieldCode())) {
                    continue;
                }
                Object formItemValue = getFormItemValue(formItem, businessData);
                thisFormDefinitionData.put(formItem.getFieldCode(), formItemValue);
            }

            result.put(formDesignVo.getFormDefinitionId().toString(), thisFormDefinitionData);
        }

        return result;
    }

    private static Object getFormItemValue(FormItem formItem, Map<String, List<Map<String, Object>>> businessData) {

        String tableName = formItem.getMetaModelCode();

        String fieldCode = formItem.getFieldCode();

        if (!FormComponentEnum.TABLE.getComponentCode().equals(formItem.getComponentCode())) {

            List<Map<String, Object>> dataRecords = businessData.get(tableName);
            if (ObjectUtil.isEmpty(dataRecords)) {
                return null;
            }

            if (dataRecords.get(0) == null) {
                return null;
            }

            return dataRecords.get(0).get(fieldCode);
        }

        else {

            FormItemTable formItemTable = formItem.getFormItemTable();
            if (formItemTable == null) {
                return new ArrayList<>();
            }

            Long modelDefinitionId = formItemTable.getSubMetaModelDefinitionId();
            if (ObjectUtil.isEmpty(modelDefinitionId)) {
                return new ArrayList<>();
            }

            MetaModelServiceApi bean = SpringUtil.getBean(MetaModelServiceApi.class);
            String modelDefinitionTableName = bean.getModelDefinitionTableName(modelDefinitionId);
            return businessData.get(modelDefinitionTableName);
        }

    }

    public static void expandFormItem(List<FormItem> formItemList, List<FormItem> newExpandFormItemList) {
        for (FormItem formItem : formItemList) {
            if (FormComponentEnum.COLLAPSE.getComponentCode().equals(formItem.getComponentCode())) {
                expandFormItem(formItem.getChildren(), newExpandFormItemList);
            } else {
                newExpandFormItemList.add(formItem);
            }
        }
    }

}
