package cn.stylefeng.team.project.modular.businessrender.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.team.form.api.FormItemServiceApi;
import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.pojo.FormDesignVo;
import cn.stylefeng.team.project.modular.businessrender.pojo.ColumnQueryVo;
import cn.stylefeng.team.project.modular.businessrender.service.FormToMetaConverter;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

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

@Service
public class FormToMetaConverterImpl implements FormToMetaConverter {

    @Resource
    private FormItemServiceApi formItemServiceApi;

    @Override
    public Map<String, List<Map<String, Object>>> formDataToMetaData(Long businessId, Map<String, Map<String, Object>> formDetail) {

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

        List<FormDesignVo> businessFromDesignList = formItemServiceApi.getBusinessFromDesignList(businessId);

        for (Map.Entry<String, Map<String, Object>> formDataTotal : formDetail.entrySet()) {

            String formDefinitionId = formDataTotal.getKey();

            Map<String, Object> formDefinitionData = formDataTotal.getValue();

            for (FormDesignVo formDesignVo : businessFromDesignList) {
                if (formDesignVo.getFormDefinitionId().toString().equals(formDefinitionId)) {

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

                    List<ColumnQueryVo> columnQueryVos = new ArrayList<>();

                    for (FormItem formItem : formItemList) {
                        if (formItem.getFormItemTable() != null) {
                            continue;
                        }

                        ColumnQueryVo columnQueryVo = new ColumnQueryVo();
                        columnQueryVo.setTableName(formItem.getMetaModelCode());
                        columnQueryVo.setFieldCode(formItem.getFieldCode());
                        columnQueryVo.setFieldValue(formDefinitionData.get(formItem.getFieldCode()));
                        columnQueryVos.add(columnQueryVo);
                    }

                    for (ColumnQueryVo columnQueryVo : columnQueryVos) {

                        if (result.containsKey(columnQueryVo.getTableName())) {

                            List<Map<String, Object>> maps = result.get(columnQueryVo.getTableName());
                            if (ObjectUtil.isEmpty(maps)) {
                                maps = new ArrayList<>();
                                maps.add(new HashMap<>());
                            }

                            maps.get(0).put(columnQueryVo.getFieldCode(), columnQueryVo.getFieldValue());

                        }

                        else {
                            List<Map<String, Object>> tableData = new ArrayList<>();

                            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                            stringObjectHashMap.put(columnQueryVo.getFieldCode(), columnQueryVo.getFieldValue());
                            tableData.add(stringObjectHashMap);

                            result.put(columnQueryVo.getTableName(), tableData);
                        }
                    }
                }
            }
        }

        return result;
    }

}
