package com.yunzhupaas.base.util.common;

import com.yunzhupaas.base.entity.VisualdevEntity;
import com.yunzhupaas.base.model.DownloadCodeForm;
import com.yunzhupaas.base.model.form.DraftJsonModel;
import com.yunzhupaas.base.model.form.FormDraftJsonModel;
import com.yunzhupaas.model.OnlineDevData;
import com.yunzhupaas.model.visualJson.FieLdsModel;
import com.yunzhupaas.model.visualJson.FormCloumnUtil;
import com.yunzhupaas.model.visualJson.FormDataModel;
import com.yunzhupaas.model.visualJson.TableModel;
import com.yunzhupaas.model.visualJson.analysis.*;
import com.yunzhupaas.model.visualJson.config.ConfigModel;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.StringUtil;

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

/**
 * 功能流程公共工具
 *
 * @author 云筑产品开发平台组
 * @version V5.2.0
 * @copyright 深圳市乐程软件有限公司（http://www.szlecheng.cn）
 * @date 2024/8/25
 */
public class FunctionFormPublicUtil {

    public static VisualdevEntity exportFlowFormJson(VisualdevEntity entity, GenerateParamModel generateParamModel) {
        VisualdevEntity visualdevEntity = new VisualdevEntity();
        // visualdevEntity.setId(entity.getId());
        visualdevEntity.setEnCode(entity.getEnCode());
        visualdevEntity.setFullName(entity.getFullName());
        visualdevEntity.setType(OnlineDevData.FORM_TYPE_SYS);
        visualdevEntity.setWebType(entity.getWebType());
        visualdevEntity.setCategory(entity.getCategory());
        visualdevEntity.setDescription(entity.getDescription());
        visualdevEntity.setSortCode(entity.getSortCode());
        visualdevEntity.setCreatorTime(entity.getCreatorTime());
        visualdevEntity.setCreatorUserId(entity.getCreatorUserId());
        visualdevEntity.setVisualTables(entity.getVisualTables());
        visualdevEntity.setDbLinkId(entity.getDbLinkId());
        // 填写默认url
        DownloadCodeForm downloadCodeForm = generateParamModel.getDownloadCodeForm();
        String appUrl = "/pages/apply/" + generateParamModel.getClassName();
        visualdevEntity.setAppUrlAddress(appUrl);
        String formFileName = "/Form.vue";
        String webUrl = "extend/" + generateParamModel.getClassName().toLowerCase() + formFileName;
        visualdevEntity.setUrlAddress(webUrl);
        String downloadClassName = generateParamModel.getClassName().substring(0, 1).toUpperCase()
                + generateParamModel.getClassName().substring(1);
        String interfaceUrl = "/api/" + downloadCodeForm.getModule() + "/" + downloadClassName;
        visualdevEntity.setInterfaceUrl(interfaceUrl);

        List<FormAllModel> formAllModel = new ArrayList<>();
        forDataMode(entity, formAllModel);
        List<FormAllModel> mastList = formAllModel.stream()
                .filter(t -> FormEnum.mast.getMessage().equals(t.getYunzhupaasKey())).collect(Collectors.toList());
        List<DraftJsonModel> tempJson = new ArrayList<>();
        for (FormAllModel mastModel : mastList) {
            FieLdsModel fieLdsModel = mastModel.getFormColumnModel().getFieLdsModel();
            String model = fieLdsModel.getVModel();
            ConfigModel config = fieLdsModel.getConfig();
            if (StringUtil.isNotEmpty(model)) {
                DraftJsonModel engineModel = new DraftJsonModel();
                String label = config.getLabel();
                engineModel.setFiledId(model);
                engineModel.setFiledName(label);
                engineModel.setRequired(config.isRequired());
                engineModel.setYunzhupaasKey(config.getYunzhupaasKey());
                engineModel.setMultiple(fieLdsModel.getMultiple());
                tempJson.add(engineModel);
            }
        }
        List<FormAllModel> tableList = formAllModel.stream()
                .filter(t -> FormEnum.table.getMessage().equals(t.getYunzhupaasKey())).collect(Collectors.toList());
        Map<String, AliasModel> tableListAll = generateParamModel.getTableAliseMap();
        for (FormAllModel model : tableList) {
            String table = model.getChildList().getTableName();
            String name = DataControlUtils.initialLowercase(tableListAll.get(table).getAliasName());
            FormColumnTableModel childList = model.getChildList();
            String label = childList.getLabel();
            boolean required = childList.isRequired();
            DraftJsonModel engineModel = new DraftJsonModel();
            engineModel.setFiledId(name + "List");
            engineModel.setFiledName(label);
            engineModel.setRequired(required);
            tempJson.add(engineModel);
            for (FormColumnModel columnModel : model.getChildList().getChildList()) {
                String vModel = columnModel.getFieLdsModel().getVModel();
                String childLable = columnModel.getFieLdsModel().getConfig().getLabel();
                ConfigModel config = columnModel.getFieLdsModel().getConfig();
                if (StringUtil.isNotEmpty(vModel)) {
                    DraftJsonModel childModel = new DraftJsonModel();
                    childModel.setFiledId(name + "List-" + vModel);
                    childModel.setFiledName(label + "-" + childLable);
                    childModel.setRequired(config.isRequired());
                    childModel.setYunzhupaasKey(config.getYunzhupaasKey());
                    childModel.setMultiple(columnModel.getFieLdsModel().getMultiple());
                    tempJson.add(childModel);
                }
            }
        }
        List<FormAllModel> mastTableList = formAllModel.stream()
                .filter(t -> FormEnum.mastTable.getMessage().equals(t.getYunzhupaasKey())).collect(Collectors.toList());
        for (FormAllModel mastTableModel : mastTableList) {
            FormMastTableModel formMastTableModel = mastTableModel.getFormMastTableModel();
            FieLdsModel fieLdsModel = formMastTableModel.getMastTable().getFieLdsModel();
            String model = formMastTableModel.getVModel();
            ConfigModel config = fieLdsModel.getConfig();
            if (StringUtil.isNotEmpty(model)) {
                DraftJsonModel engineModel = new DraftJsonModel();
                String label = config.getLabel();
                engineModel.setFiledId(model);
                engineModel.setFiledName(label);
                engineModel.setRequired(config.isRequired());
                engineModel.setYunzhupaasKey(config.getYunzhupaasKey());
                engineModel.setMultiple(fieLdsModel.getMultiple());
                tempJson.add(engineModel);
            }
        }
        FormDraftJsonModel draftJsonModel = new FormDraftJsonModel();
        String Tem = JsonUtil.getObjectToString(tempJson);
        visualdevEntity.setFormData(Tem);
        draftJsonModel.setDraftJson(Tem);
        draftJsonModel.setTableJson(entity.getVisualTables());
        return visualdevEntity;
    }

    /**
     * 封装数据
     *
     * @param entity
     * @param formAllModel
     */
    private static void forDataMode(VisualdevEntity entity, List<FormAllModel> formAllModel) {
        // formTempJson
        FormDataModel formData = JsonUtil.getJsonToBean(entity.getFormData(), FormDataModel.class);
        List<FieLdsModel> list = JsonUtil.getJsonToList(formData.getFields(), FieLdsModel.class);
        List<TableModel> tableModelList = JsonUtil.getJsonToList(entity.getVisualTables(), TableModel.class);
        RecursionForm recursionForm = new RecursionForm(list, tableModelList);
        FormCloumnUtil.recursionForm(recursionForm, formAllModel);
    }

}
