package cn.stylefeng.team.view.modular.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.entity.FormViewConfig;
import cn.stylefeng.team.form.api.enums.FormComponentEnum;
import cn.stylefeng.team.form.api.pojo.FormDesignVo;
import cn.stylefeng.team.meta.api.constants.MetaConstants;
import cn.stylefeng.team.meta.api.entity.MetaField;
import cn.stylefeng.team.view.api.enums.ParserTypeEnum;
import cn.stylefeng.team.view.api.pojo.ViewButton;
import cn.stylefeng.team.view.api.pojo.ViewColumnField;
import cn.stylefeng.team.view.api.pojo.ViewConfig;
import cn.stylefeng.team.view.api.pojo.ViewSearchField;
import cn.stylefeng.team.view.modular.pojo.response.ViewFieldResult;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ViewConfigFactory {

    public static void fillViewConfigIds(ViewConfig viewConfig) {
        List<ViewColumnField> viewColumnFieldList = viewConfig.getViewColumnFieldList();
        if (ObjectUtil.isNotEmpty(viewColumnFieldList)) {
            for (ViewColumnField viewColumnField : viewColumnFieldList) {
                viewColumnField.setFieldId(IdWorker.getIdStr());
            }
        }

        List<ViewSearchField> viewSearchFieldList = viewConfig.getViewSearchFieldList();
        if (ObjectUtil.isNotEmpty(viewSearchFieldList)) {
            for (ViewSearchField viewSearchField : viewSearchFieldList) {
                viewSearchField.setFieldId(IdWorker.getIdStr());
            }
        }

        List<ViewButton> viewButtonList = viewConfig.getViewButtonList();
        if (ObjectUtil.isNotEmpty(viewButtonList)) {
            for (ViewButton viewButton : viewButtonList) {
                viewButton.setButtonId(IdWorker.getIdStr());
            }
        }
    }

    public static ViewFieldResult createFormBasedFieldList(List<MetaField> businessMainModelFields, List<FormDesignVo> businessFromDesignList) {

        ViewFieldResult viewFieldResult = new ViewFieldResult();
        viewFieldResult.setViewColumnFieldList(new ArrayList<>());
        viewFieldResult.setViewSearchFieldList(new ArrayList<>());

        if (ObjectUtil.isEmpty(businessMainModelFields) || ObjectUtil.isEmpty(businessFromDesignList)) {
            return viewFieldResult;
        }

        for (FormDesignVo formDesignVo : businessFromDesignList) {

            List<FormItem> formItemList = formDesignVo.getFormItemList();
            if (ObjectUtil.isEmpty(formItemList)) {
                continue;
            }

            for (FormItem formItem : formItemList) {

                FormViewConfig formViewConfig = formItem.getFormViewConfig();
                if (formViewConfig == null) {
                    continue;
                }

                if (!formItemInMainModel(formItem.getFieldCode(), businessMainModelFields)) {
                    continue;
                }

                if (formViewConfig.getShowSelectColumn()) {
                    ViewColumnField viewColumnField = createViewColumnField(formDesignVo, formItem);
                    viewFieldResult.getViewColumnFieldList().add(viewColumnField);
                }

                if (formViewConfig.getOpenQueryParam()) {
                    ViewSearchField viewSearchField = createViewSearchField(formItem);
                    viewFieldResult.getViewSearchFieldList().add(viewSearchField);
                }
            }
        }

        addCommonField(viewFieldResult.getViewColumnFieldList(), businessMainModelFields);

        return viewFieldResult;
    }

    public static ViewColumnField createViewColumnField(FormDesignVo formDesignVo, FormItem formItem) {

        ViewColumnField viewColumnField = new ViewColumnField();
        viewColumnField.setFieldId(IdWorker.getIdStr());
        viewColumnField.setFieldName(formItem.getFieldCode());
        viewColumnField.setShowName(formItem.getItemLabelName());

        viewColumnField.setParserType(ParserTypeEnum.FORM_PARSER.getCode());
        viewColumnField.setFormDefinitionId(formDesignVo.getFormDefinitionId());
        viewColumnField.setFieldCode(formItem.getFieldCode());
        viewColumnField.setColumnShowFlag(true);

        if (FormComponentEnum.UPLOAD_PIC.getComponentCode().equals(formItem.getComponentCode())) {
            viewColumnField.setPicShowFlag(true);
        } else {
            viewColumnField.setPicShowFlag(false);
        }

        return viewColumnField;
    }

    public static ViewSearchField createViewSearchField(FormItem formItem) {

        ViewSearchField viewSearchField = new ViewSearchField();
        viewSearchField.setFieldId(IdWorker.getIdStr());
        viewSearchField.setFieldName(formItem.getFieldCode());
        viewSearchField.setShowName(formItem.getItemLabelName());

        viewSearchField.setSearchShowFlag(true);
        viewSearchField.setConditionInputType("text");

        return viewSearchField;
    }

    public static boolean formItemInMainModel(String formFieldCode, List<MetaField> businessMainModelFields) {
        Set<String> mainModelFieldCodeList = businessMainModelFields.stream().map(MetaField::getFieldCode).collect(Collectors.toSet());
        return mainModelFieldCodeList.contains(formFieldCode);
    }

    public static void addCommonField(List<ViewColumnField> viewColumnFieldList, List<MetaField> businessMainModelFields) {

        for (MetaField modelField : businessMainModelFields) {
            if (YesOrNotEnum.Y.getCode().equals(modelField.getKeyFlag())) {

                ViewColumnField keyField = new ViewColumnField();
                keyField.setFieldId(IdWorker.getIdStr());
                keyField.setFieldName(modelField.getFieldCode());
                keyField.setShowName(modelField.getDescription());
                keyField.setColumnShowFlag(false);

                viewColumnFieldList.add(0, keyField);
            }
        }

        for (MetaField modelField : businessMainModelFields) {

            if (MetaConstants.COMMON_CREATE_TIME_FIELD_CODE.equals(modelField.getFieldCode())) {
                ViewColumnField createTimeField = new ViewColumnField();
                createTimeField.setFieldId(IdWorker.getIdStr());
                createTimeField.setFieldName(modelField.getFieldCode());
                createTimeField.setShowName(modelField.getDescription());
                createTimeField.setColumnShowFlag(true);

                viewColumnFieldList.add(createTimeField);
            }

            else if (MetaConstants.COMMON_CREATE_USER_FIELD_CODE.equals(modelField.getFieldCode())) {

                ViewColumnField createTimeField = new ViewColumnField();
                createTimeField.setFieldId(IdWorker.getIdStr());
                createTimeField.setFieldName(modelField.getFieldCode());
                createTimeField.setShowName(modelField.getDescription());
                createTimeField.setColumnShowFlag(true);

                createTimeField.setParserType(ParserTypeEnum.COMMON_PARSER.getCode());
                createTimeField.setParserName("userName");

                viewColumnFieldList.add(createTimeField);
            }
        }
    }

}
