package yem.wm.ct.opplugin.validator;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import yem.base.common.utils.StringUtils;
import yem.base.common.utils.YEM;

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

/**
 * 动态表单插件
 */
public class ContractTermsSubmitValidator extends AbstractValidator {

    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntity = this.getDataEntities();
        for (ExtendedDataEntity extendedDataEntity : dataEntity) {
            this.checkHollowing(extendedDataEntity);
        }
    }

    /**
     * 校验挖空字段
     * @param extendedDataEntity
     */
    private void checkHollowing(ExtendedDataEntity extendedDataEntity) {
        DynamicObject dataEntity = extendedDataEntity.getDataEntity();
        String yem_foreigntrade = dataEntity.getString("yem_foreigntrade");
        if (StringUtils.isNotEmpty(yem_foreigntrade)) {
            if (yem_foreigntrade.equals("A")) {
                this.checkChineseAndEnlishSame(dataEntity, extendedDataEntity);
            }
        }

        this.queryFieldControl(extendedDataEntity, dataEntity);
    }

    /**
     * 根据中文的挖空字段判断是否在【字段对照】表中存在
     * @param extendedDataEntity
     * @param dataEntity
     */
    private void queryFieldControl(ExtendedDataEntity extendedDataEntity, DynamicObject dataEntity) {
        String yem_clauseschina = dataEntity.getString("yem_clausecontentchina");
        List<String> clauseschina = new ArrayList<>();
        if (StringUtils.isNotEmpty(yem_clauseschina)) {
            for (int i = 0; i < yem_clauseschina.length(); i++) {
                if (yem_clauseschina.contains("【")) {
                    yem_clauseschina = this.cutOutString(yem_clauseschina, clauseschina);
                }
            }
        }
        if (clauseschina.size() > 0) {
            for (String str : clauseschina) {
                DynamicObject clausecontrast = BusinessDataServiceHelper.loadSingle("yem_bd_clausecontrast", "id", new QFilter[] {new QFilter("yem_clausekey", QCP.equals, str)});
                if (clausecontrast == null) {
                    this.addErrorMessage(extendedDataEntity, String.format("挖空字段: %s在字段对照中不存在!!!", str));
                }
            }
        }
    }

    /**
     * 贸易类型为外贸，判断中文挖空字段和英文挖空字段一致(长度，内容一致)
     * @param dataEntity
     * @param extendedDataEntity
     */
    private void checkChineseAndEnlishSame(DynamicObject dataEntity, ExtendedDataEntity extendedDataEntity) {
        String yem_clauseschina = dataEntity.getString("yem_clausecontentchina");
        String yem_clausesenglish = dataEntity.getString("yem_clausecontentenglish");
        List<String> clauseschina = new ArrayList<>();
        List<String> clausesenglish = new ArrayList<>();
        if (YEM.isNotEmpty(yem_clauseschina) && YEM.isNotEmpty(yem_clausesenglish)) {
            if (StringUtils.isNotEmpty(yem_clauseschina)) {
                for (int i = 0; i < yem_clauseschina.length(); i++) {
                    if (yem_clauseschina.contains("【")) {
                        yem_clauseschina = this.cutOutString(yem_clauseschina, clauseschina);
                    }
                }
            }

            if (StringUtils.isNotEmpty(yem_clausesenglish)) {
                for (int i = 0; i < yem_clausesenglish.length(); i++) {
                    if (yem_clausesenglish.contains("【")) {
                        yem_clausesenglish = this.cutOutString(yem_clausesenglish, clausesenglish);
                    }
                }
            }

            if (clauseschina.size() == clausesenglish.size()) {
                for (String str : clauseschina) {
                    if (!clausesenglish.contains(str)) {
                        this.addErrorMessage(extendedDataEntity, String.format("中文挖空字段: %s在英文挖空字段中不存在!!!", str));
                        return;
                    }
                }

                for (String str : clausesenglish) {
                    if (!clauseschina.contains(str)) {
                        this.addErrorMessage(extendedDataEntity, String.format("英文挖空字段: %s在中文挖空字段中不存在!!!", str));
                        return;
                    }
                }
            }else {
                this.addErrorMessage(extendedDataEntity, "中文内容挖空字段和英文挖空字段长度不一致!!!");
                return;
            }
        }
    }

    /**
     * 根据挖空字段截取字符串
     * @param yem_clauseschina
     * @param clauseschina
     */
    private String cutOutString(String yem_clauseschina, List<String> clauseschina) {
        String substring = yem_clauseschina.substring(yem_clauseschina.indexOf("【") + 1, yem_clauseschina.indexOf("】"));
        String str = yem_clauseschina.substring(0, yem_clauseschina.indexOf("【")) +
                yem_clauseschina.substring(yem_clauseschina.indexOf("】") + 1);
        clauseschina.add(substring);
        return str;
    }
}