package com.yunzhupaas.base.util.common;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.yunzhupaas.model.visualJson.FieLdsModel;
import com.yunzhupaas.model.visualJson.analysis.FormAllModel;
import com.yunzhupaas.model.visualJson.analysis.FormColumnModel;
import com.yunzhupaas.model.visualJson.analysis.FormColumnTableModel;
import com.yunzhupaas.model.visualJson.analysis.FormEnum;
import com.yunzhupaas.model.visualJson.config.RuleConfig;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.StringUtil;
import com.yunzhupaas.util.visiual.YunzhupaasKeyConsts;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 代码生成器数据处理工具类
 *
 * @author 云筑产品开发平台组
 * @version V3.2.0
 * @copyright 深圳市乐程软件有限公司（http://www.szlecheng.cn）
 * @date 2024/8/26
 */
public class DataControlUtils {

    /**
     * 将字符串的首字母转大写
     *
     * @param name 需要转换的字符串
     * @return
     */
    public static String captureName(String name) {
        char[] ch = name.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if (i == 0) {
                ch[0] = Character.toUpperCase(ch[0]);
            }
        }
        StringBuffer a = new StringBuffer();
        a.append(ch);
        return a.toString();

    }

    public static String initialLowercase(String name) {
        char[] ch = name.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if (i == 0) {
                ch[0] = Character.toLowerCase(ch[0]);
            }
        }
        StringBuffer a = new StringBuffer();
        a.append(ch);
        return a.toString();
    }

    public static String getPlaceholder(String yunzhupaasKey) {
        String placeholderName = "请选择";
        switch (yunzhupaasKey) {
            case YunzhupaasKeyConsts.BILLRULE:
            case YunzhupaasKeyConsts.MODIFYUSER:
            case YunzhupaasKeyConsts.CREATEUSER:
            case YunzhupaasKeyConsts.COM_INPUT:
            case YunzhupaasKeyConsts.TEXTAREA:
                placeholderName = "请输入";
                break;
            default:
                break;
        }
        return placeholderName;
    }

    /**
     * 去重
     *
     * @param keyExtractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    public static FieLdsModel setAbleIDs(FieLdsModel fieLdsModel) {
        if (fieLdsModel.getAbleDepIds() != null) {
            fieLdsModel.setAbleDepIds(JSONObject.toJSONString(fieLdsModel.getAbleDepIds()));
        }
        if (fieLdsModel.getAblePosIds() != null) {
            fieLdsModel.setAblePosIds(JSONObject.toJSONString(fieLdsModel.getAblePosIds()));
        }
        if (fieLdsModel.getAbleUserIds() != null) {
            fieLdsModel.setAbleUserIds(JSONObject.toJSONString(fieLdsModel.getAbleUserIds()));
        }
        if (fieLdsModel.getAbleRoleIds() != null) {
            fieLdsModel.setAbleRoleIds(JSONObject.toJSONString(fieLdsModel.getAbleRoleIds()));
        }
        if (fieLdsModel.getAbleGroupIds() != null) {
            fieLdsModel.setAbleGroupIds(JSONObject.toJSONString(fieLdsModel.getAbleGroupIds()));
        }
        if (fieLdsModel.getAbleIds() != null) {
            fieLdsModel.setAbleIds(JSONObject.toJSONString(fieLdsModel.getAbleIds()));
        }
        //model字段验证reg转换
        if (fieLdsModel.getConfig().getRegList() != null) {
            String o1 = JSONObject.toJSONString(JsonUtil.getObjectToString(fieLdsModel.getConfig().getRegList()));
            fieLdsModel.getConfig().setReg(o1);
        }
        return fieLdsModel;
    }

    /**
     * 单据规则配置获取
     * @param formAllModel
     * @return
     */
    public static Map<String, String> getBillRule(List<FormAllModel> formAllModel) {
        Map<String, String> billRuleMap = new HashMap<>();
        for (FormAllModel t : formAllModel) {
            if (FormEnum.mast.getMessage().equals(t.getYunzhupaasKey())) {
                FieLdsModel fieLdsModel = t.getFormColumnModel().getFieLdsModel();
                String ruleJson = getRuleJson(fieLdsModel);
                if (StringUtil.isNotEmpty(ruleJson)) {
                    billRuleMap.put(fieLdsModel.getVModel(), ruleJson);
                }
            }
            if (FormEnum.mastTable.getMessage().equals(t.getYunzhupaasKey())) {
                FieLdsModel fieLdsModel = t.getFormMastTableModel().getMastTable().getFieLdsModel();
                String ruleJson = getRuleJson(fieLdsModel);
                if (StringUtil.isNotEmpty(ruleJson)) {
                    billRuleMap.put(fieLdsModel.getVModel(), ruleJson);
                }
            }
            if (FormEnum.table.getMessage().equals(t.getYunzhupaasKey())) {
                FormColumnTableModel childModel = t.getChildList();
                String aliasLowName = childModel.getAliasLowName();
                List<FormColumnModel> childList = childModel.getChildList();
                for (FormColumnModel child : childList) {
                    FieLdsModel fieLdsModel = child.getFieLdsModel();
                    String ruleJson = getRuleJson(fieLdsModel);
                    if (StringUtil.isNotEmpty(ruleJson)) {
                        billRuleMap.put(aliasLowName + "_" + fieLdsModel.getVModel(), ruleJson);
                    }
                }
            }
        }
        return billRuleMap;
    }

    private static String getRuleJson(FieLdsModel fieLdsModel) {
        Integer ruleType = fieLdsModel.getConfig().getRuleType();
        if (Objects.equals(ruleType, 2)) {
            RuleConfig ruleConfig = fieLdsModel.getConfig().getRuleConfig();
            if (ruleConfig != null) {
                return JsonUtil.getObjectToString(JsonUtil.getObjectToString(ruleConfig));
            }
        }
        return null;
    }
}

