package nc.ui.hbbb.accountmap.view;

import java.awt.Point;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import nc.ui.pub.beans.UITable;
import nc.vo.hbbb.account.MeasureAccPropVO;
import nc.vo.iufo.measure.MeasureVO;

import com.ufida.report.dnddatasource.DragObject;
import com.ufsoft.iufo.fmtplugin.measure.MeasureModel;
import com.ufsoft.report.sysplugin.dnd.IDndAdapter;
import com.ufsoft.table.CellPosition;
import com.ufsoft.table.CellsModel;
import com.ufsoft.table.CellsPane;

/**
 *  指标与合并科目映射Dnd适配器
 * @author yp
 *
 */
public class MeasureMapRepDndAdapter implements IDndAdapter
{
	/**
	 * 会计科目属性树
	 * 
	 */
	private JTree accPropTree;
	/**
	 * 表格面板
	 */
	private CellsPane curCellsPane;
	
	private UITable curMeasureTable;
	 
//	//指标科目属性映射Map
//	private Map<String, HBAccountPropVO>  measurePropMap = null;

	public MeasureMapRepDndAdapter(JTree accPropTree){
		this.accPropTree = accPropTree;
	}

//	public MeasureMapRepDndAdapter(CellsPane curCPane,Map<String, HBAccountPropVO> measurePropMap){
//		curCellsPane = curCPane;
//		this.measurePropMap = measurePropMap;
//	}
//	
//	public MeasureMapRepDndAdapter(UITable curMeasureTable,Map<String, HBAccountPropVO> measurePropMap){
//		this.curMeasureTable = curMeasureTable;
//		this.measurePropMap = measurePropMap;
//	}

	@Override
	public Object getSourceObject(){
		if (accPropTree != null){
			// 从会计科目属性树上拖动
			return doDragFromAccPropTree();
		} else if(curCellsPane != null){
			// 从报表设计器上拖动
			return doDragFromReportDesigner();
		}else{
			//指标table展示界面拖动
			return doDragFromMeasureTable();
		}
	}
	/**
	 * 从指标列表上拖拽
	 * @return
	 */
	private DragObject doDragFromMeasureTable(){
		int column = curMeasureTable.getSelectedColumn();
		if(column != 4)
			return null;
		int row = curMeasureTable.getSelectedRow();
		MeasureAccPropVO propMapVo = ((MeasureMapTableModel)curMeasureTable.getModel()).getVO(row);
		return new DragObject(curMeasureTable, propMapVo, row);
	}

	/**
	 * 从科目属性树上拖拽
	 * @param path
	 * @return
	 */
	private DragObject doDragFromAccPropTree(){
		TreePath[] paths = accPropTree.getSelectionPaths();
		if (paths == null || paths.length == 0){
			return null;
		}
		if (paths.length == 1){
			// 单选树节点拖动
			return doOneItemDragFromAccPropTree(paths[0]);
		}
		// 多选树节点拖动
//		return doMoreItemDragFromAccPropTree(paths);
		return null;
	}


	/**
	 * 从科目属性树上拖拽
	 * @param path
	 * @return
	 */
	private DragObject doOneItemDragFromAccPropTree(TreePath path){
		DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) path
				.getLastPathComponent();
		Object selObj = selNode.getUserObject();
		Object otherObj = null;
		return new DragObject(accPropTree, selObj, otherObj);
	}

	/**
	 * 从cellspane上拖拽
	 * @return
	 */
	private DragObject doDragFromReportDesigner(){
		CellPosition formatPos = curCellsPane.getDataModel().getSelectModel()
				.getAnchorCell();
		if (formatPos == null)
		{
			return null;
		}
		MeasureModel measureModel = MeasureModel.getInstance(curCellsPane.getDataModel());
		MeasureVO measureVo = measureModel.getMeasureVOByPos(formatPos);
		if(measureVo == null)
			return null;
		
//		HBAccountPropVO propVo = measurePropMap.get(measureVo.getCode());
//		if(propVo == null)
			return null;
		
//		return new DragObject(curCellsPane, propVo, formatPos);
	}

	@Override
	public boolean insertObject(Point ap, Object obj){
		if (accPropTree != null){
			// 树上不增加节点
			return true;
		}
		
//		 if (obj instanceof DragObject)	{
//			Object dragObj = ((DragObject) obj).getDragObj();
			//从科目树拖拽到cellspane和指标列表当中
//			boolean bDragInSelf = false;
//			 if (dragObj instanceof HBAccountPropVO){
//				 //报表表样上的拖拽支持
//				 if (curCellsPane != null) {
//					CellPosition dstPos = getInsertFormatCellPos(ap);
//					if (dstPos == null) {
//						return false;
//					}
//					MeasureModel measureModel = getMeasureModel();
//					MeasureVO measureVo = measureModel.getMeasureVOByPos(dstPos);
//					
//					//如果是在表样上拖拽，如从A3移到B3
//					CellPosition srcPos = (CellPosition)((DragObject) obj).getOtherInfo();
//					
//					if(srcPos != null){
//						MeasureVO srcMeasureVo = measureModel.getMeasureVOByPos(srcPos);
//						measurePropMap.remove(srcMeasureVo.getCode());
//						curCellsPane.repaintCell(srcPos.getRow(), srcPos.getColumn());
//						bDragInSelf = true;
//					}
//					//如果指标为空，直接返回，认为删除了映射关系
//					if (measureVo == null)
//						return false;
//					if(existKeyTypeMappings((HBAccountPropVO) dragObj, measureVo.getKeyCombPK(), bDragInSelf)){
//						MessageDialog.showErrorDlg(curCellsPane, "出错", "该合并科目已经存在相同的关键字的映射关系");
//						return false;
//					}
//					// 插入一个会计科目属性
//					measurePropMap.put(measureVo.getCode(),(HBAccountPropVO) dragObj);
//					curCellsPane.repaintCell(dstPos.getRow(), dstPos.getColumn());
//					
//				//报表列表展示的table支持
//				}else if(curMeasureTable != null){
//					int row = curMeasureTable.rowAtPoint(ap);
//					MeasureAccPropVO propMapVo = ((MeasureMapTableModel)curMeasureTable.getModel()).getVO(row);
//					if(existKeyTypeMappings((HBAccountPropVO) dragObj, propMapVo.getMeasureVo().getKeyCombPK(), bDragInSelf)){
//						MessageDialog.showErrorDlg(curMeasureTable, "出错", "该合并科目已经存在相同的关键字的映射关系");
//						return false;
//					}
//					propMapVo.setPropVo((HBAccountPropVO) dragObj);
//					measurePropMap.put(propMapVo.getMeasureVo().getCode(),(HBAccountPropVO) dragObj);
//					((MeasureMapTableModel)curMeasureTable.getModel()).fireTableRowsUpdated(row, row);
//				}
//				 //在指标列表上来回拖动
//			}else if(dragObj instanceof MeasureAccPropVO){
//				int row = curMeasureTable.rowAtPoint(ap);
//				int col = curMeasureTable.columnAtPoint(ap);
//				//如果列为-1，认为是删除
//				if(row != -1 && col != -1){
//					MeasureAccPropVO propMapVo = ((MeasureMapTableModel)curMeasureTable.getModel()).getVO(row);
//					propMapVo.setPropVo(((MeasureAccPropVO) dragObj).getPropVo());
//					measurePropMap.put(propMapVo.getMeasureVo().getCode(),((MeasureAccPropVO) dragObj).getPropVo());
//				}
//				
//				//删除事件源位置的映射科目
//				Integer srcRow = (Integer)((DragObject) obj).getOtherInfo();
//				((MeasureMapTableModel)curMeasureTable.getModel()).getVO(srcRow).setPropVo(null);
//				measurePropMap.remove(((MeasureAccPropVO) dragObj).getMeasureVo().getCode());
//				
//				if(col != -1)
//					((MeasureMapTableModel)curMeasureTable.getModel()).fireTableRowsUpdated(row, row);
//				((MeasureMapTableModel)curMeasureTable.getModel()).fireTableRowsUpdated(srcRow, srcRow);
//				
//			} 
//		}

		return false;
	}
	
	private MeasureModel getMeasureModel(){
		return MeasureModel.getInstance(curCellsPane.getDataModel());
	}



	private CellPosition getInsertFormatCellPos(Point ap){
		int row = curCellsPane.rowAtPoint(ap);
		int col = curCellsPane.columnAtPoint(ap);
		CellPosition aimPos = CellPosition.getInstance(row, col);
		return aimPos;
	}

	@Override
	public void removeSourceNode(Object obj)
	{
		if (curCellsPane != null)
		{
			CellsModel formatModel = curCellsPane.getDataModel();
			CellPosition[] selectCells = new CellPosition[] { formatModel
					.getSelectModel().getAnchorCell() };
		
			// 重新绘制
			curCellsPane.repaintCell(selectCells[0].getRow(), selectCells[0]
					.getColumn());
		}
	}
	/**
	 * 根据合并科目属性，判断是否存在相同关键字的映射关系
	 * @param propVo
	 * @param keygroupPK
	 * @param bDragInSelf
	 * @return
	 */
//	private boolean existKeyTypeMappings(HBAccountPropVO propVo,String keygroupPK,boolean bDragInSelf){
//		//是否在映射的hashmap当中
//		boolean bExsitInMap = false;
//		if(bDragInSelf)
//			return false;
//		else{
//			for(Iterator<HBAccountPropVO> iter = measurePropMap.values().iterator();iter.hasNext();){
//				HBAccountPropVO vo = iter.next();
//				if(vo.equals(propVo)){
//					bExsitInMap = true;
//					break;
//				}
//			}
//		}
//		if(bExsitInMap)
//			return true;
//		//在数据库中存在映射关系
//		return HBAccountMapSrvUtils.exsitKeyTypeMappings(propVo, keygroupPK);
//	}


	public JTree getAccPropTree() {
		return accPropTree;
	}

	public void setAccPropTree(JTree accPropTree) {
		this.accPropTree = accPropTree;
	}

	public CellsPane getCurCellsPane() {
		return curCellsPane;
	}

	public void setCurCellsPane(CellsPane curCellsPane) {
		this.curCellsPane = curCellsPane;
	}

//	public Map<String, HBAccountPropVO> getMeasurePropMap() {
//		return measurePropMap;
//	}
//
//	public void setMeasurePropMap(Map<String, HBAccountPropVO> measurePropMap) {
//		this.measurePropMap = measurePropMap;
//	}
}
