package com.yem.wm.bc.change.dynamic;

import kd.bos.dataentity.entity.LocaleString;
import kd.bos.dataentity.metadata.IDataEntityType;
import kd.bos.dataentity.metadata.clr.DataEntityType;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.dataentity.serialization.DcJsonSerializer;
import kd.bos.dataentity.serialization.ListDcxmlBinder;
import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.dataentity.utils.Uuid16;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IClientViewProxy;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.ClickListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.form.plugin.FormViewPluginProxy;
import kd.bos.form.plugin.IFormPlugin;
import kd.bos.lang.Lang;
import kd.bos.metadata.entity.validation.AbstractValidationParamPlugin;
import kd.bos.metadata.entity.validation.ConditionValidation;
import kd.bos.metadata.entity.validation.PreCondition;

import java.util.*;

/**
 * @author 夜子
 * @ClassName ValiditionEditPlugin
 * @description: 变更校验条件表单插件
 * @date 2023年08月10日
 * @version: 1.0
 */
public class ValiditionEditPlugin extends AbstractFormPlugin implements ClickListener {
    private static final String Key_Desc = "desc";
    private static final String Key_OK = "buttonok";
    private static final String FORMID_VALIDATE = "ide_validateopt";
    private ConditionValidation cv;

    public ValiditionEditPlugin() {
    }

    public void registerListener(EventObject e) {
        this.addClickListeners("buttonok");
    }

    public void afterCreateNewData(EventObject arg0) {
        Object ruleObj = this.getView().getFormShowParameter().getCustomParam("Rule");
        if (ruleObj != null) {
            List<IDataEntityType> dTypes = new ArrayList<>();
            dTypes.add(DataEntityType.getDataEntityType(ConditionValidation.class));
            ListDcxmlBinder binder = new ListDcxmlBinder(false, dTypes);
            Map<String, Object> parameter =  SerializationUtils.fromJsonString(ruleObj.toString(), Map.class);
            DcJsonSerializer ds = new DcJsonSerializer(binder);
            ds.setIsLocaleValueFull(true);
            this.cv = (ConditionValidation)ds.deserializeFromMap(parameter, (Object)null);
            if (this.cv != null) {
                IDataModel model = this.getModel();
                if (this.cv.getMessage() != null) {
                    parameter.put("Message", this.cv.getMessage().getItem(Lang.get().toString()));
                }

                this.showValidateOption(parameter);
                if (this.cv.getDescription() != null) {
                    parameter.put("Description", this.cv.getDescription().getItem(Lang.get().toString()));
                    model.setValue("desc", this.cv.getDescription().getItem(Lang.get().toString()));
                } else if (StringUtils.isNotBlank(parameter.get("Description"))) {
                    model.setValue("desc", parameter.get("Description"));
                }
            }
        } else {
            this.showValidateOption((Object)null);
        }

    }

    public void click(EventObject evt) {
        switch (((Control)evt.getSource()).getKey()) {
            case "buttonok":
                this.returnData();
            default:
        }
    }

    private void returnData() {
        ConditionValidation validation = null;
        IDataModel model = this.getModel();
        String validType = "FormValidType";
        String pageId = this.getPageCache().get("ValidType_PageId");
        IFormView optFormView = null;
        if (pageId != null) {
            optFormView = this.getView().getView(pageId);
            this.getView();
        }

        AbstractValidationParamPlugin<?> validateOptPlug = this.getValidateOptPlug(optFormView);
        if (validateOptPlug != null) {
            StringBuilder sBuilder = new StringBuilder();
            if (!validateOptPlug.checkValidation(sBuilder)) {
                this.getView().showTipNotification(sBuilder.toString());
                return;
            }

            validation = (ConditionValidation)validateOptPlug.getValidation();
        } else {
            validation = new ConditionValidation();
        }

        String newDesc = StringUtils.isBlank(model.getValue("desc")) ? ResManager.getLocaleString("校验条件", "ValiditionEditPlugin_0", "yem-yc-changemodel").toString() : model.getValue("desc").toString();
        if (validation != null) {
            Object ruleObj = this.getView().getFormShowParameter().getCustomParam("Rule");
            if (ruleObj != null) {
                List<IDataEntityType> dTypes = new ArrayList<>();
                dTypes.add(DataEntityType.getDataEntityType(ConditionValidation.class));
                ListDcxmlBinder binder = new ListDcxmlBinder(false, dTypes);
                Map<String, Object> parameter = SerializationUtils.fromJsonString(ruleObj.toString(), Map.class);
                DcJsonSerializer ds = new DcJsonSerializer(binder);
                ds.setIsLocaleValueFull(true);
                this.cv = (ConditionValidation)ds.deserializeFromMap(parameter, (Object)null);
                if (this.cv != null) {
                    validation.setId(this.cv.getId());
                    if (this.cv.getDescription() != null) {
                        this.cv.getDescription().setItem(Lang.get().toString(), newDesc);
                    } else {
                        this.cv.setDescription(new LocaleString(Lang.get().toString(), newDesc));
                    }

                    validation.setDescription(this.cv.getDescription());
                    if (this.cv.getMessage() != null) {
                        this.cv.getMessage().setItem(Lang.get().toString(), validation.getMessage().getItem(Lang.get().toString()));
                        validation.setMessage(this.cv.getMessage());
                    }
                }
            }

            if (validation.getDescription() == null) {
                validation.setDescription(new LocaleString(Lang.get().toString(), newDesc));
            } else {
                validation.getDescription().put(Lang.get().toString(), newDesc);
            }

            if (StringUtils.isBlank(validation.getId())) {
                validation.setId(Uuid16.create().toString());
            }

            validation.setRuleType(validType);
            validation.setPreCondition((PreCondition)null);
            validation.setLevelId("2");
            this.getView().returnDataToParent(validation);
            this.getView().close();
        }

    }

    private void showValidateOption(Object parameter) {
        String pageId = this.getPageCache().get("ValidType_PageId");
        if (pageId != null) {
            Map<String, Object> arg = new HashMap<>(2);
            arg.put("pageId", pageId);
            ((IClientViewProxy)this.getView().getService(IClientViewProxy.class)).addAction("closeWindow", arg);
            this.getPageCache().remove("ValidType_PageId");
        }

        List<Map<String, Object>> formContext = (List)this.getView().getFormShowParameter().getCustomParams().get("MetaContext");
        FormShowParameter paraSettings = new FormShowParameter();
        paraSettings.getOpenStyle().setShowType(ShowType.InContainer);
        paraSettings.getOpenStyle().setTargetKey("FValidPanel");
        paraSettings.setFormId("ide_validateopt");
        paraSettings.setCustomParam("MetaContext", formContext);
        paraSettings.setCustomParam("Parameter", parameter);
        this.getView().showForm(paraSettings);
        this.getPageCache().put("ValidType_PageId", paraSettings.getPageId());
    }

    private AbstractValidationParamPlugin<?> getValidateOptPlug(IFormView view) {
        if (view == null) {
            return null;
        } else {
            FormViewPluginProxy proxy = (FormViewPluginProxy)view.getService(FormViewPluginProxy.class);
            if (proxy != null) {

                for (IFormPlugin plugin : proxy.getPlugIns()) {
                    if (plugin instanceof AbstractValidationParamPlugin) {
                        return (AbstractValidationParamPlugin) plugin;
                    }
                }
            }

            return null;
        }
    }
}
