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

import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.team.form.api.entity.FormDefinition;
import cn.stylefeng.team.form.api.entity.FormItem;
import cn.stylefeng.team.form.api.entity.FormItemContent;
import cn.stylefeng.team.form.api.entity.FormItemTable;
import cn.stylefeng.team.form.api.enums.FormComponentEnum;
import cn.stylefeng.team.form.api.factory.FormItemFactory;
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.meta.api.entity.MetaModel;
import cn.stylefeng.team.meta.api.enums.SubTableTypeEnum;
import cn.stylefeng.team.project.modular.filing.constants.BevProdConstants;
import cn.stylefeng.team.project.modular.filing.pojo.generateform.BaseMetaInfo;
import com.alibaba.fastjson2.JSON;

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

import static cn.stylefeng.team.meta.api.constants.MetaConstants.*;

public class FormDefinitionGenerateFactory {

    public static FormDesignVo generateFormDetail(BaseMetaInfo baseMetaInfo, FormDefinition formDefinition) {

        FormDesignVo formConfig = new FormDesignVo();
        formDefinition.setFormConfig(formConfig);

        formConfig.setFormConfig(JSON.parseObject("""
                {
                    "fileId": null,
                    "layout": "vertical",
                    "labelCol": {
                    },
                    "formWidth": null,
                    "rowGutter": 16,
                    "labelWidth": 100,
                    "wrapperCol": {
                    },
                    "labelLayout": "flex"
                    }
                """));

        List<FormItem> formItemList = new ArrayList<>();
        formConfig.setFormItemList(formItemList);




        FormItem mainTitle = createTitle(baseMetaInfo.getMainTitle(), "16px", null);
        formItemList.add(mainTitle);

        FormItem subTitle = createTitle(baseMetaInfo.getSubTitle(), "18px", "500");
        formItemList.add(subTitle);



        createMainTableInfo(formItemList);




        MetaModel mainModel = baseMetaInfo.getMetaModelList().stream().filter(metaModel -> YesOrNotEnum.Y.getCode().equals(metaModel.getMainFlag())).findFirst().orElse(null);
        createSubTableInfo(baseMetaInfo, mainModel, formItemList);

        return formConfig;
    }

    private static FormItem createTitle(String title, String fontSize, String fontWeight) {

        FormItem mainTitle = FormItemFactory.defaultFormItem();

        mainTitle.setMetaModelCode(null);
        mainTitle.setFieldCode(null);

        mainTitle.setComponentCode("title");
        mainTitle.setItemLabelName(title);

        FormItemContent formItemContent = new FormItemContent();
        formItemContent.setFontSize(fontSize);
        formItemContent.setFontColor("#000");
        formItemContent.setLineColor("#000");
        formItemContent.setTextAlign("center");
        formItemContent.setLineHeight("0px");
        formItemContent.setFontWeight(fontWeight);
        mainTitle.setFormItemContent(formItemContent);

        mainTitle.getFormItemStyle().setMarginBottom(0);

        mainTitle.setItemLabelShowFlag(false);

        return mainTitle;
    }

    private static void createMainTableInfo(List<FormItem> formItemList) {

        FormItem filingNo = FormItemFactory.defaultFormItem();
        filingNo.setMetaModelCode("bev_prod_filing_main");
        filingNo.setFieldCode("filing_no");
        filingNo.setComponentCode(FormComponentEnum.INPUT.getComponentCode());
        filingNo.setItemLabelName("编号");
        filingNo.setItemColumnNumber(12);
        formItemList.add(filingNo);

        FormItem versionFormItem = FormItemFactory.defaultFormItem();
        versionFormItem.setMetaModelCode("bev_prod_filing_main");
        versionFormItem.setFieldCode("table_version");
        versionFormItem.setComponentCode(FormComponentEnum.INPUT.getComponentCode());
        versionFormItem.setItemLabelName("版本号");
        versionFormItem.setItemColumnNumber(12);
        formItemList.add(versionFormItem);

        FormItem filingDate = FormItemFactory.defaultFormItem();
        filingDate.setMetaModelCode("bev_prod_filing_main");
        filingDate.setFieldCode("filing_date");
        filingDate.setComponentCode(FormComponentEnum.DATE.getComponentCode());
        filingDate.setItemLabelName("填报日期");
        filingDate.setItemColumnNumber(8);
        formItemList.add(filingDate);

        FormItem prodShift = FormItemFactory.defaultFormItem();
        prodShift.setMetaModelCode("bev_prod_filing_main");
        prodShift.setFieldCode("prod_shift");
        prodShift.setComponentCode(FormComponentEnum.INPUT.getComponentCode());
        prodShift.setItemLabelName("班次");
        prodShift.setItemColumnNumber(8);
        formItemList.add(prodShift);

        FormItem operateUser = FormItemFactory.defaultFormItem();
        operateUser.setMetaModelCode("bev_prod_filing_main");
        operateUser.setFieldCode("operator_user");
        operateUser.setComponentCode(FormComponentEnum.INPUT.getComponentCode());
        operateUser.setItemLabelName("操作员");
        operateUser.setItemColumnNumber(8);
        formItemList.add(operateUser);
    }

    private static void createSubTableInfo(BaseMetaInfo baseMetaInfo, MetaModel mainModel, List<FormItem> totalFormItem) {

        List<MetaModel> allSubTableModel = baseMetaInfo.getMetaModelList()
                .stream().filter(i -> YesOrNotEnum.N.getCode().equals(i.getMainFlag()))
                .toList();

        for (MetaModel thisSubModel : allSubTableModel) {

            FormItem divider = FormItemFactory.defaultFormItem();
            divider.setComponentCode(FormComponentEnum.DIVIDE_LINE.getComponentCode());
            divider.setItemLabelName(thisSubModel.getMetaModelName());
            FormItemContent formItemContent = new FormItemContent();
            formItemContent.setFontSize("16px");
            formItemContent.setFontColor("#000");
            formItemContent.setLineColor("#000");
            formItemContent.setTextAlign("left");
            formItemContent.setFontWeight("500");
            divider.setFormItemContent(formItemContent);
            divider.setItemColumnNumber(24);
            divider.setItemLabelShowFlag(false);
            totalFormItem.add(divider);

            List<MetaField> metaFieldList = baseMetaInfo.getMetaFieldList();
            List<MetaField> thisModelFieldList = metaFieldList.stream()
                    .filter(i -> i.getMetaModelId().equals(thisSubModel.getMetaModelId()))
                    .toList();

            MetaField subTableLinkMainField = thisModelFieldList.stream()
                    .filter(i -> BevProdConstants.LINK_MAIN_ID_FIELD_NAME.equals(i.getFieldCode()))
                    .findFirst().orElse(new MetaField());


            Integer subTableType = thisSubModel.getSubTableType();

            if (SubTableTypeEnum.ONE_TO_ONE.getCode().equals(subTableType)) {
                for (MetaField metaField : thisModelFieldList) {

                    if (validateIsBaseField(metaField)) {
                        continue;
                    }

                    FormItem formItemByField = FormDefinitionItemFactory.createFormItemByField(thisSubModel, metaField);
                    totalFormItem.add(formItemByField);
                }
            }

            else {
                FormItem tableItem = FormItemFactory.defaultFormItem();
                tableItem.setComponentCode(FormComponentEnum.TABLE.getComponentCode());

                tableItem.setMetaModelCode(mainModel.getMetaModelCode());

                tableItem.setFieldCode(thisSubModel.getMetaModelCode() + MetaConstants.DEFAULT_MAIN_MODEL_LINK_TABLE_FIELD_SUFFIX);

                FormItemTable formItemTable = new FormItemTable();
                formItemTable.setShowAddFlag(true);
                formItemTable.setShowBorderFlag(true);
                formItemTable.setShowDeleteFlag(true);
                formItemTable.setShowNumberFlag(true);
                formItemTable.setSubMetaModelDefinitionId(thisSubModel.getCurrentDefinitionId());

                formItemTable.setSubMetaModelLinkFieldId(subTableLinkMainField.getFieldId());
                tableItem.setFormItemTable(formItemTable);
                tableItem.setItemLabelName(thisSubModel.getMetaModelName());

                tableItem.setItemColumnNumber(24);

                List<FormItem> tableFormItemList = new ArrayList<>();
                tableItem.setChildren(tableFormItemList);
                for (MetaField columnField : thisModelFieldList) {

                    if (validateIsBaseField(columnField)) {
                        continue;
                    }

                    FormItem tableColumnFormItem = FormDefinitionItemFactory.createFormItemByField(thisSubModel, columnField);
                    tableColumnFormItem.setItemColumnNumber(24);
                    tableFormItemList.add(tableColumnFormItem);
                }

                totalFormItem.add(tableItem);
            }
        }
    }

    private static boolean validateIsBaseField(MetaField metaField) {
        return metaField.getFieldCode().equals(COMMON_ID_FILED_CODE)
                || metaField.getFieldCode().equals(COMMON_CREATE_TIME_FIELD_CODE)
                || metaField.getFieldCode().equals(COMMON_UPDATE_TIME_FIELD_CODE)
                || metaField.getFieldCode().equals(COMMON_CREATE_USER_FIELD_CODE)
                || metaField.getFieldCode().equals(COMMON_UPDATE_USER_FIELD_CODE)
                || metaField.getFieldCode().equals(COMMON_DEL_FLAG_FIELD_CODE)
                || metaField.getFieldCode().equals(COMMON_VERSION_FLAG_FIELD_CODE)
                || metaField.getFieldCode().equals(COMMON_TENANT_ID_FIELD_CODE)

                || metaField.getFieldCode().equals(BevProdConstants.LINK_MAIN_ID_FIELD_NAME);
    }

}
