package nckd.jxccl.sit.hcsi.formplugin.web.tp;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.Search;
import kd.bos.form.control.events.SearchEnterEvent;
import kd.bos.form.control.events.SearchEnterListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.util.StringUtils;

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

/**
 * Tyx 2025-10-17
 * 结果表内容选择右表搜索插件 nckd_sititemselect
 */
public class SitItemSelectItemEntryPlugin extends AbstractFormPlugin implements SearchEnterListener {

    public void registerListener(EventObject e) {
        super.registerListener(e);
        Search search = (Search)this.getView().getControl("columnsearchap");
        search.addEnterListener(this);
    }


    @Override
    public void search(SearchEnterEvent searchEnterEvent) {
        String searchText = searchEnterEvent.getText();
        if (StringUtils.isEmpty(searchText)) {
            return;
        }

        DynamicObjectCollection entryDatas = this.getModel().getEntryEntity("itemselectentry");
        if (ObjectUtils.isEmpty(entryDatas)) {
            return;
        }

        EntryGrid grid = (EntryGrid) this.getView().getControl("itemselectentry");
        int[] selectRows = grid.getSelectRows();
        List<Integer> matchLine = this.getMatchColumn(entryDatas, searchText);

        if (matchLine.isEmpty()) {
            this.getView().showTipNotification(ResManager.loadKDString("搜索已完成，未找到搜索项。",
                    "CalResultTplItemTreePlugin_0", "swc-hsas-formplugin", new Object[0]));
            return;
        }

        if (selectRows.length <= 1) {
            handleSingleSelection(grid, selectRows, matchLine);
        } else {
            grid.selectRows(matchLine.get(0), true);
        }
    }

    private void handleSingleSelection(EntryGrid grid, int[] selectRows, List<Integer> matchLine) {
        if (matchLine.size() == 1) {
            grid.selectRows(matchLine.get(0), true);
            return;
        }

        if (selectRows.length == 0 || !isMatch(selectRows, matchLine)) {
            grid.selectRows(matchLine.get(0), true);
            return;
        }

        int currentRow = selectRows[0];
        int firstMatch = matchLine.get(0);
        int lastMatch = matchLine.get(matchLine.size() - 1);

        if (currentRow == firstMatch) {
            grid.selectRows(matchLine.get(1), true);
        } else if (currentRow == lastMatch) {
            grid.selectRows(firstMatch, true);
        } else {
            selectNextMatch(grid, currentRow, matchLine);
        }
    }

    private void selectNextMatch(EntryGrid grid, int currentRow, List<Integer> matchLine) {
        for (int i = 0; i < matchLine.size(); i++) {
            if (matchLine.get(i) == currentRow) {
                grid.selectRows(matchLine.get(i + 1), true);
                break;
            }
        }
    }

    public boolean isMatch(int[] selectRows, List<Integer> matchLine) {
        if (selectRows == null || selectRows.length == 0) {
            return false;
        }
        return matchLine.contains(selectRows[0]);
    }

    public boolean isMatch(int i, List<Integer> matchLine) {
        return matchLine.contains(i);
    }

    public List<Integer> getMatchColumn(DynamicObjectCollection entryDatas, String searchText) {
        List<Integer> matchLine = new ArrayList(10);
        int size = entryDatas.size();

        for(int i = 0; i < size; ++i) {
            DynamicObject dataObj = (DynamicObject)entryDatas.get(i);
            String itemName = dataObj.getString("itemname");
            String itemNumber = dataObj.getString("itemnumber");
            if (itemName.contains(searchText) || itemNumber.contains(searchText)) {
                matchLine.add(i);
            }
        }

        return matchLine;
    }

}