package nc.ui.arap.view;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import nc.bs.arap.util.ArapBillVOUtils;
import nc.itf.arap.fieldmap.IBillFieldGet;
import nc.ui.arap.bill.ArapBillUIUtil;
import nc.ui.arap.bill.LinkHyperListener;
import nc.ui.arap.viewhandler.ArapListHeadDataChangedEvent;
import nc.ui.arap.viewhandler.list.LoadBillListTemplate;
import nc.ui.pub.bill.BillItem;
import nc.ui.pub.bill.BillListPanel;
import nc.ui.pub.bill.BillModel;
import nc.ui.pubapp.uif2app.view.IPaginationDelegate;
import nc.ui.pubapp.uif2app.view.MouseClickShowPanelMediator;
import nc.ui.pubapp.uif2app.view.ShowUpableBillListView;
import nc.ui.uif2.AppEvent;
import nc.ui.uif2.components.pagination.PaginationBar;
import nc.ui.uif2.components.pagination.PaginationModel;
import nc.ui.uif2.model.IRowSelectModel;
import nc.ui.uif2.model.RowOperationInfo;
import nc.vo.arap.basebill.BaseAggVO;
import nc.vo.pub.AggregatedValueObject;

/**
 * 
 * @author miaohl
 * 
 */
@SuppressWarnings("restriction")
public class ArapBillListView extends ShowUpableBillListView {

	private NodeKeyQry nodeKeyQry;

	public NodeKeyQry getNodeKeyQry() {
		return nodeKeyQry;
	}

	public void setNodeKeyQry(NodeKeyQry nodeKeyQry) {
		this.nodeKeyQry = nodeKeyQry;
		if (nodeKeyQry != null) {
			setNodekey(nodeKeyQry.getDefNodekey());
		}
	}

	private static final int DEFAULT_PAGESIZE = 10000;
	private static final long serialVersionUID = 1L;

	private boolean needHeadDataChanged;

	private PaginationBar paginationBar = new PaginationBar();

	private IPaginationDelegate paginationDelegate;

	private MouseClickShowPanelMediator mouseMediator;

	private LoadBillListTemplate loadBillListTemplate;

	private int pageSize = DEFAULT_PAGESIZE;

	public LoadBillListTemplate getLoadBillListTemplate() {
		return loadBillListTemplate;
	}

	public void setLoadBillListTemplate(
			LoadBillListTemplate loadBillListTemplate) {
		this.loadBillListTemplate = loadBillListTemplate;
	}

	public MouseClickShowPanelMediator getMouseMediator() {
		return mouseMediator;
	}

	public void setMouseMediator(MouseClickShowPanelMediator mouseMediator) {
		this.mouseMediator = mouseMediator;
	}

	public void addHeadDataChangedListener() {
		this.needHeadDataChanged = true;
	}

	public IPaginationDelegate getPaginationDelegate() {
		return this.paginationDelegate;
	}

	public void initRealUI() {
		if (this.isComponentVisible()) {
			this.initUI();
		}
	}

	@Override
	public void setComponentVisible(boolean visible) {

		super.setComponentVisible(visible);

		if (visible && this.getBillListPanel() == null) {
			this.initUI();
		} else if (visible && isDirtyFlag()) {
			this.synchronizeDataFromModel();
			setDirtyFlag(false);
		}

	}

	@Override
	public void initUI() {
		this.setPaginationBar(this.paginationBar);
		super.initUI();
		BillItem bodyItem = getBillListPanel().getBodyItem(IBillFieldGet.SCOMMENT);
		initDoubleClickMediator();
		initListPanelLoadTemplateListener();

		PaginationModel paginationModel = this.paginationDelegate
				.getPaginationModel();
		if (paginationModel != null) {
			paginationModel.setMaxPageSize(getPageSize());
		}
		this.paginationBar.setPaginationModel(paginationModel);
		this.paginationBar.onStructChanged();
		this.addHeadDataChangedListener();
		this.synchronizeDataFromModel();
		
		//处理特殊字段， 摘要
		loadSpecialItem(null);
		//2023/10/12 YY 添加列表超链接监听
		String tableName = bodyItem.getTableName();
		if("应付单行".equals(tableName)) {
			if(getBillListPanel().getBodyItem("def41") != null){
				getBillListPanel().getBodyItem("def41").addBillItemHyperlinkListener(new LinkHyperListener(getBillListPanel()));
			}else if(getBillListPanel().getBodyItem("def22") != null) {
				getBillListPanel().getBodyItem("def22").addBillItemHyperlinkListener(new LinkHyperListener(this.getBillListPanel()));
			}
		}
		if("应收单行".equals(tableName)) {
			if(getBillListPanel().getBodyItem("def22") != null){
				getBillListPanel().getBodyItem("def22").addBillItemHyperlinkListener(new LinkHyperListener(getBillListPanel()));
			}
		}
	}

	private int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private void initListPanelLoadTemplateListener() {
		if (getLoadBillListTemplate() != null) {
			getLoadBillListTemplate().setBlp(this);
		}
	}

	private void initDoubleClickMediator() {
		if (getMouseMediator() != null) {
			getMouseMediator().setListView(this);
			try {
				Method declaredMethod = getMouseMediator().getClass()
						.getDeclaredMethod("match");
				declaredMethod.setAccessible(true);
				declaredMethod.invoke(getMouseMediator());
			} catch (Exception e) {
				// FIXME
			}
		}
	}

	public void removeHeadDataChangedListener() {
		this.needHeadDataChanged = false;
	}

	public void setPaginationDelegate(IPaginationDelegate paginationDelegate) {
		this.paginationDelegate = paginationDelegate;
	}

	@Override
	public void handleEvent(AppEvent event) {
		if (getBillListPanel() == null)
			return;

		if (!this.isComponentVisible()) {
			setDirtyFlag(true);
			return;
		}

		super.handleEvent(event);

	}
	
	@Override
	protected void handleSelectionChanged() {
		super.handleSelectionChanged();
		BaseAggVO selectedData = (BaseAggVO)getModel().getSelectedData();
		if(selectedData == null ){
			return;
		}
		// 列表界面查询后摘要不显示的问题
		loadSpecialItem(selectedData);
	}

	private void loadSpecialItem(BaseAggVO selectedData) {
		BillItem scommentBodyItem = getBillListPanel().getBodyItem(IBillFieldGet.SCOMMENT);
		ArapBillUIUtil.loadHeadScomment(selectedData,scommentBodyItem);
	}
	static class ArapBillListPanelValueSetter extends
			VOBillListPanelValueSetter {
		final Map<Integer, BaseAggVO> bills;

		public ArapBillListPanelValueSetter(Map<Integer, BaseAggVO> bills) {
			this.bills = bills;
		}

		@Override
		protected void processLoadRelation(BillListPanel listPanel,
				String[] tabcodes) {
			for (String tabcode : tabcodes) {
				BillModel bodyModel = listPanel.getBodyBillModel(tabcode);
				if (bodyModel != null) {
				}
			}
		}
	}

	protected void refreshDataUpdate(AppEvent event) {
		IBillListPanelValueSetter billListPanelValueSetter = getBillListPanelValueSetter();
		Map<Integer, BaseAggVO> bills = new HashMap<Integer, BaseAggVO>();
		RowOperationInfo operationInfo = (RowOperationInfo) event
				.getContextObject();
		if (getModel() instanceof IRowSelectModel) {
			for (int i = 0; i < operationInfo.getRowIndexes().length; i++) {
				if (operationInfo.getRowIndexes()[i] != getModel()
						.getSelectedRow()) {
					BaseAggVO bill = (BaseAggVO) operationInfo.getRowDatas()[i];
					if (bill.getLoadRelationItemValue() != null
							&& bill.getLoadRelationItemValue().booleanValue()) {
						bills.put(i, bill);
					}
				}
			}
		}

		if (bills.size() != 0) {
			IBillListPanelValueSetter billListPanelValueSetter2 = new ArapBillListPanelValueSetter(
					bills);
			setBillListPanelValueSetter(billListPanelValueSetter2);

			super.handleEvent(event);

			setBillListPanelValueSetter(billListPanelValueSetter);
		} else {
			super.handleEvent(event);
		}

	}

	// 单据列表，卡片界面在不显示时不响应所有事件，只设置dirtyflag，在控件显示时根据标志和model进行同步
	private boolean dirtyFlag = false;

	public boolean isDirtyFlag() {
		return dirtyFlag;
	}

	public void setDirtyFlag(boolean dirtyFlag) {
		this.dirtyFlag = dirtyFlag;
	}

	@Override
	protected void synchronizeDataFromModel() {
		if (!this.isComponentVisible()) {
			setDirtyFlag(true);
			return;
		}

		Object[] datas = this.getModel().getData().toArray();

		if (datas == null || datas.length == 0) { // 如果没有数据，则清除
			billListPanel.getHeadBillModel().clearBodyData();
			billListPanel.getBodyBillModel().clearBodyData();
		} else {
			for (Object vo : datas) {
				if (vo != null && vo instanceof AggregatedValueObject) {
					ArapBillVOUtils.removeDebetTransferinfo(new AggregatedValueObject[]{(AggregatedValueObject) vo});
				}
			}

			getModel().setSelectedRow(0);// 第一次进入页面点击刷新，设置选中行为第一行
			getBillListPanelValueSetter().setHeaderDatas(billListPanel, datas);
			
			getModel().directlyUpdate(datas);
			getBillListPanelValueSetter().setBodyData(billListPanel,
					getModel().getSelectedData());
			setHeadTableHighLightByModelSelection();

			setCheckBoxMultiUnstate();
			this.getBillListPanel().getParentListPanel().setTotalRowShow(true);
			this.getBillListPanel().getChildListPanel().setTotalRowShow(true);

		}

		if (this.needHeadDataChanged) {
			ArapListHeadDataChangedEvent ev = new ArapListHeadDataChangedEvent(
					this);
			this.getModel().fireEvent(ev);
		}
	}

	@Override
	public PaginationBar getPaginationBar() {
		return paginationBar;
	}

	@Override
	public void setPaginationBar(PaginationBar paginationBar) {
		this.paginationBar = paginationBar;
		super.setPaginationBar(paginationBar);
	}

}
