package com.yem.em.bc.list;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import com.yem.em.utils.BaseDataFilterUtils;
import com.yem.em.utils.EmptyUtils;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.ITreeModel;
import kd.bos.entity.tree.TreeNode;
import kd.bos.filter.FilterContainer;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.Search;
import kd.bos.form.control.TreeView;
import kd.bos.form.control.events.RefreshNodeEvent;
import kd.bos.form.control.events.SearchEnterEvent;
import kd.bos.form.control.events.SearchEnterListener;
import kd.bos.form.control.events.TreeNodeEvent;
import kd.bos.form.events.BeforeBuildTreeNodeEvent;
import kd.bos.form.events.BeforeTreeNodeClickEvent;
import kd.bos.list.IListView;
import kd.bos.list.ITreeListView;
import kd.bos.list.events.BeforeShowBillFormEvent;
import kd.bos.list.events.BuildTreeListFilterEvent;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.list.plugin.ITreeListPlugin;
import kd.bos.mvc.list.ListView;
import kd.bos.mvc.list.TreeListModel;
import kd.bos.orm.query.QFilter;

/**
 * @author 夜子
 * @ClassName TreeOrgCtrList
 * @description: 带左树列表通用插件
 * @date 2023年07月11日
 * @version: 1.0
 */
public class TreeOrgCtrList extends AbstractListPlugin implements ITreeListPlugin, SearchEnterListener {
    protected ITreeListView treeListView;
    private ITreeModel treeModel;

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Search search = this.getView().getControl("searchap");
        if (search != null) {
            search.addEnterListener(this);
        }
        FilterContainer filterContainer = this.getControl("filtercontainerap");
        filterContainer.addAfterSearchClickListener((searchClickEvent) -> this.refreshTreeRootNode());
    }

    @Override
    public void setTreeListView(ITreeListView treeListView) {
        this.treeListView = treeListView;
    }

    protected ITreeModel getTreeModel() {
        if (this.treeModel == null) {
            this.treeModel = this.getTreeListView().getTreeModel();
        }
        return this.treeModel;
    }

    private void refreshTreeRootNode() {
        String rootId = this.treeListView.getTreeModel().getRoot().getId();
        this.treeListView.getTreeModel().setCurrentNodeId(rootId);
        TreeView treeview = this.getTreeListView().getTreeView();
        treeview.focusNode(this.treeListView.getTreeModel().getRoot());
        this.treeListView.refresh();
    }

    public ITreeListView getTreeListView() {
        if (this.treeListView == null && this.getView() instanceof IListView) {
            this.treeListView = ((IListView) this.getView()).getTreeListView();
        }
        return this.treeListView;
    }

    @Override
    public void initialize() {
        super.initialize();
        this.getTreeListView();
    }

    @Override
    public void refreshNode(RefreshNodeEvent e) {
        if (this.getControlFilters() != null) {
            String orgId = this.getPageCache().get("createOrg");
            ListView listView = (ListView) this.getView();
            ITreeListView treeListView = listView.getTreeListView();
            TreeListModel treeListModel = (TreeListModel) treeListView.getTreeModel();
            if (!orgId.equals("100000")) {
                QFilter baseDataFilter = BaseDataFilterUtils.getBaseDataFilter("yem_em_comlefttree",
                        Long.parseLong(orgId));
                treeListModel.getTreeFilter().add(baseDataFilter);
            }
        }
    }

    @Override
    public void search(SearchEnterEvent arg0) {
    }

    @Override
    public void beforeBuildTreeNode(BeforeBuildTreeNodeEvent arg0) {
    }

    @Override
    public void beforeTreeNodeClick(BeforeTreeNodeClickEvent arg0) {
    }

    @Override
    public void buildTreeListFilter(BuildTreeListFilterEvent arg0) {
        QFilter filter = this.nodeClickFilter();
        if (filter != null) {
            arg0.addQFilter(filter);
            arg0.setCancel(true);
        }
    }

    @Override
    public void expendTreeNode(TreeNodeEvent arg0) {
        String nodeId = (String) arg0.getNodeId();
        TreeNode node = this.getTreeListView().getTreeModel().refreshNode(nodeId);
        arg0.setExpandedNode(node);
    }

    @Override
    public void initTreeToolbar(EventObject arg0) {

    }

    @Override
    public void initializeTree(EventObject arg0) {
        ListView listView = (ListView) this.getView();
        ITreeListView treeListView = listView.getTreeListView();
        TreeListModel treeListModel = (TreeListModel) treeListView.getTreeModel();

        TreeNode root = this.getTreeModel().createRootNode();
        String orgId = this.getPageCache().get("createOrg");
        if (StringUtils.isNotBlank(orgId)) {
            QFilter baseDataFilter = BaseDataFilterUtils.getBaseDataFilter("yem_em_comlefttree", Long.parseLong(orgId));
            this.treeModel.getTreeFilter().add(baseDataFilter);
        }

        FormShowParameter formShowParameter = this.getView().getFormShowParameter();
        Map<String, Object> cusMap = formShowParameter.getCustomParams();
        if (cusMap.containsKey("maintainproject")) {
            String maintainproject = (String) cusMap.get("maintainproject");
            if (EmptyUtils.isNotEmpty(maintainproject)) {
                QFilter qFilter = new QFilter("tp_etcnumber.number", "=", maintainproject);
                treeListModel.getTreeFilter().add(qFilter);
            }
        }

        this.treeModel.setRoot(root);
        this.treeModel.setCurrentNodeId(root.getId());
    }

    @Override
    public void treeNodeClick(TreeNodeEvent arg0) {

    }

    @Override
    public void treeToolbarClick(EventObject arg0) {

    }

    @Override
    public void beforeShowBill(BeforeShowBillFormEvent e) {
        if (this.treeModel != null && this.treeModel.getGroupProp() != null) {
            if (e.getParameter().getStatus() == OperationStatus.ADDNEW) {
                Object node = this.treeModel.getCurrentNodeId();
                if (node != null && node.toString().compareTo(this.treeModel.getRoot().getId()) != 0) {
                    Map<String, Object> currNodeId = new HashMap<>();
                    currNodeId.put("key", this.treeModel.getGroupProp().getName());
                    currNodeId.put("value", this.treeModel.getCurrentNodeId());
                    e.getParameter().setCustomParam("tree_parent_id", currNodeId);
                }
            } else if (e.getParameter().getStatus() == OperationStatus.EDIT) {
                TreeNode node1 = this.treeModel.getRoot().getTreeNode(e.getParameter().getPkId().toString(), 10);
                if (node1 == null) {
                    return;
                }
                String currNodeId1 = node1.getId();
                if (currNodeId1 != null && currNodeId1.compareTo(this.treeModel.getRoot().getId()) != 0) {
                    Map<String, Object> parentInfo = new HashMap<>();
                    parentInfo.put("key", this.treeModel.getGroupProp().getName());
                    parentInfo.put("value", currNodeId1);
                    e.getParameter().setCustomParam("tree_curr_id", parentInfo);
                }
            }
        }
    }

    protected QFilter nodeClickFilter() {
        return null;
    }
}