package yem.wmc.ydn.formplugin.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.ClickListener;
import kd.bos.form.control.events.ItemClickListener;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import yem.base.common.module.meta.propselect.helper.MetaPropertyHelper;

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

/**
 * @author lipan
 * @date 2024/4/28 17:35
 * @className DataregistrationEdit
 * @description
 */
public class DataregistrationEdit  extends AbstractFormPlugin implements ItemClickListener, ClickListener {
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners(new String[]{"yem_billcol","yem_targebillcol"});
    }




    @Override
    public void click(EventObject evt) {
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();
        switch (key) {
            case "yem_billcol":
                this.clickBillCol(evt, key, "yem_requirebill", true, true);
                break;
            case "yem_targebillcol":
                this.clickBillCol(evt, key, "yem_targetbill", true, true);
                break;
        }
    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
//        DynamicObject entityObj = (DynamicObject) this.getModel().getValue("yem_requirebill");
//        String entityNumber = "";
//        if(entityObj != null) {
//            entityNumber = entityObj.getString("number");
//        }
//        this.fillEntityCombo(entityNumber);
    }


//    private void fillEntityCombo(String entityNumber) {
//        ComboEdit entryEditor = (ComboEdit) this.getView().getControl("yem_selectentity");
//        if(!entityNumber.isEmpty()) {
//            MainEntityType mainType = EntityMetadataCache.getDataEntityType(entityNumber);
//            List<ComboItem> entryItems = this.buildEntryComboItems(mainType);
//            entryEditor.setComboItems(entryItems);
//            String yem_selectentity = (String) this.getModel().getValue("yem_selectentity");
//            String selectentity = "";
//            for (ComboItem comboItem : entryItems) {
//                if(comboItem.getValue().equals(yem_selectentity)) {
//                    selectentity = yem_selectentity;
//                }
//            }
//            this.getModel().setValue("yem_selectentity", selectentity);
//        }else {
//            entryEditor.setComboItems(null);
//        }
//    }

    private void clickBillCol(EventObject evt, String key, String baseCol, boolean isMulti, boolean isAddEntry) {
        DynamicObject requireBill = MetaPropertyHelper.getAndCheckF7Value(this.getView(), baseCol);
        if(requireBill != null) {
            String formKey = requireBill.getString("number");
            String yem_selectentity = (String) this.getModel().getValue("yem_selectentity");
            List<String> entityKeys = new ArrayList<String>();
            if(isAddEntry) {
                entityKeys.add(yem_selectentity);
            }
            entityKeys.add(formKey);
            MetaPropertyHelper.showPropertySelect(this, key, formKey, isMulti, entityKeys);
        }
    }

//    private List<ComboItem> buildEntryComboItems(MainEntityType mainType) {
//        ArrayList<ComboItem> entryItems = new ArrayList<>();
//        ComboItem blankItem = new ComboItem();
//        blankItem.setValue("");
//        blankItem.setCaption(new LocaleString());
//        entryItems.add(blankItem);
//        if (mainType != null) {
//            Map<String, EntityType> entitys = mainType.getAllEntities();
//            for (EntityType entity : entitys.values()) {
//                if (!(entity instanceof LinkEntryType) && entity instanceof EntryType && !(entity instanceof SubEntryType)) {
//                    ComboItem entryItem = new ComboItem();
//                    entryItem.setValue(entity.getName());
//                    entryItem.setCaption(entity.getDisplayName());
//                    entryItems.add(entryItem);
//                }
//            }
//        }
//
//        return entryItems;
//    }

    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        super.closedCallBack(e);
        String key = e.getActionId();
        IDataModel model = this.getModel();
        switch (key) {
            case "yem_billcol":
                MetaPropertyHelper.callBackEntryData(model, e, "yem_colsmap", "yem_billcolno", "yem_billcol", false);
                break;
            case "yem_targebillcol":
                MetaPropertyHelper.callBackEntryData(model, e, "yem_colsmap", "yem_targebillcolno", "yem_targebillcol", false);
                break;
        }
    }

}
