package nc.ui.hbbb.adjustrep.input.plugin.action;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.swing.JTabbedPane;

import nc.pub.iufo.cache.IUFOCacheManager;
import nc.ui.hbbb.adjustrep.input.edit.AdjustCombRepDataEditor;
import nc.ui.hbbb.adjustrep.input.edit.AdjustRepDataEditor;
import nc.ui.pub.beans.UITabbedPane;
import nc.util.hbbb.HBBBReportUtil;
import nc.util.hbbb.datacenter.DataCenterType;
import nc.util.hbbb.workdraft.pub.IWorkDraft;
import nc.util.hbbb.workdraft.pub.ReportType;
import nc.util.hbbb.workdraft.pub.WorkDraft;
import nc.vo.iufo.measure.MeasureVO;

import com.ufida.zior.plugin.IPluginActionDescriptor;
import com.ufida.zior.plugin.PluginActionDescriptor;
import com.ufida.zior.plugin.PluginKeys.XPOINT;
import com.ufsoft.iufo.inputplugin.measure.MeasureFmt;
import com.ufsoft.table.AreaPosition;
import com.ufsoft.table.Cell;
import com.ufsoft.table.CellPosition;
import com.ufsoft.table.CellsModel;
import com.ufsoft.table.CellsPane;
import com.ufsoft.table.ReportTable;
import com.ufsoft.table.SelectModel;
/**
 *
 * <p>
 * 折算数据中心区域工作底稿查询
 * </p>
 *
 * 修改记录：<br>
 * <li>修改人：修改日期：修改内容：</li>
 * <br><br>
 *
 * @see
 * @author wangxwb
 * @version V6.0a
 * @since V6.0 创建时间：2011-6-10 下午01:28:58
 */
@SuppressWarnings("restriction")
public class ConvertRacgeDraftAction extends AbsUfocPluginAction {

	private Map<String, Boolean> repIsIntrateMap = new HashMap<String, Boolean>();
	
	@Override
	public boolean isEnabled() {
//		if(getContext() == null){
//    		return false;
//    	}
//		DataCenterType datatype= (DataCenterType) getContext().getAttribute(DataCenterType.HBBB_DATACENTER);
//		if(datatype.equals(DataCenterType.CONVERT) || datatype.equals(DataCenterType.SEP_ADJUST) ||datatype.equals(DataCenterType.HB_ADJUST)){
//			AdjustCombRepDataEditor curView = (AdjustCombRepDataEditor) getCurrentView();
//			String pk_report = curView.getActiveRepDataEditor().getRepPK();
//			return ! isIntrateRep(pk_report);
//		}
//		else {
//			if(getCellsModel() == null){
//	    		return false;
//	    	}
//			//报表是否打开
//	        Viewer curView = getCurrentView();
//	    	if (curView == null || curView instanceof AbsBaseRepDataEditor == false)
//	    		return false;
//	    	
//	    	IRepDataEditor editor = ((AbsBaseRepDataEditor)curView).getActiveRepDataEditor();
//	    	return editor != null && editor.getAloneID() != null && editor.isSWReport() == false;
//		}
		
		boolean isEnable = super.isEnabled();
		if(isEnable){
			DataCenterType datatype= (DataCenterType) getContext().getAttribute(DataCenterType.HBBB_DATACENTER);
			if(datatype.equals(DataCenterType.CONVERT) || datatype.equals(DataCenterType.SEP_ADJUST) ||datatype.equals(DataCenterType.HB_ADJUST)){
				AdjustCombRepDataEditor curView = (AdjustCombRepDataEditor) getCurrentView();
				String pk_report = curView.getActiveRepDataEditor().getRepPK();
				return !isIntrateRep(pk_report);
			}
		}
		return isEnable;
	}
	
	
	private boolean isIntrateRep(String pk_report) {
		if(repIsIntrateMap.get(pk_report) == null) {
			boolean b = HBBBReportUtil.isIntrateRep(pk_report);
			repIsIntrateMap.put(pk_report, new Boolean(b));
			return b;
		}
		else {
			return repIsIntrateMap.get(pk_report);
		}
	}
	
	@Override
	public void execute(ActionEvent e) {
		AdjustCombRepDataEditor curView = (AdjustCombRepDataEditor) getCurrentView();
		if (curView != null) {
			UITabbedPane uiTabbedPane = curView.getUITabbedPane();
			nc.ui.iufo.input.ufoe.comp.ExKTabbedPane workdraftSHowTab = (nc.ui.iufo.input.ufoe.comp.ExKTabbedPane) uiTabbedPane.getSelectedComponent();
			AdjustRepDataEditor selectedComponent = (AdjustRepDataEditor) workdraftSHowTab.getSelectedComponent();
			IWorkDraft workdraft = selectedComponent.getWorkdraft();
			if(workdraft.isdraft()) return;
			ReportTable table = selectedComponent.getTable();
			CellsPane cells = table.getCells();
			SelectModel selectionModel = cells.getSelectionModel();
			CellsModel cellsModel = selectionModel.getCellsModel();
			AreaPosition[] selectedAreas = selectionModel.getSelectedAreas();
			
			
			
			
			
			ArrayList<MeasureVO>  selectedMeasureCol = new ArrayList<MeasureVO>();
		//	ArrayList<Cell>  selectedcell = new ArrayList<Cell>();
			for (int i = 0; i < selectedAreas.length; i++) {
				Set<String> measurePks = new HashSet<String>();
				AreaPosition areaPosition = selectedAreas[i];
				Cell[][] cells2 = cellsModel.getCells(areaPosition);
				for(int j = 0; j < cells2.length ; j++){
					Cell[] cellj = cells2[j];
					for(int m = 0 ; m < cellj.length; m++){
						Cell cell = cells2[j][m];
				//		selectedcell.add(cell);
						//得到当前选择单元格的指标格式信息,支持动态区
						MeasureFmt fmt = (MeasureFmt) cell.getExtFmt(MeasureFmt.EXT_FMT_MEASUREINPUT);
						if(fmt != null)
							measurePks.add(fmt.getCode());
						
//						Map<CellPosition, MeasureVO> mainMeasureVOByArea = CellsModelOperator.getMeasureModel(cellsModel).getMainMeasureVOByArea(areaPosition);
					}
				}
				MeasureVO[] vos = IUFOCacheManager.getSingleton().getMeasureCache().getMeasures(measurePks.toArray(new String[measurePks.size()]));
				selectedMeasureCol.addAll(Arrays.asList(vos));
				DataCenterType datatype= (DataCenterType) getContext().getAttribute(DataCenterType.HBBB_DATACENTER);
				
				
				JTabbedPane jTabbedPane = curView.getTabbePaneMap().get(workdraft.getPk_report());
				ReportType convertDraft = ReportType.CONVERT_DRAFT;
				if(datatype.equals(DataCenterType.CONVERT)){
					convertDraft = ReportType.CONVERT_DRAFT;
				}else if(datatype.equals(DataCenterType.HB)){
					convertDraft = ReportType.HB_DRAFT;
					//动态表的区域底稿-modified by jiaah at 20130619
					if(vos != null && vos.length > 0 && vos[0] != null &&  (!vos[0].getKeyCombPK().equals(workdraft.getHbSchemevo().getPk_keygroup())))
						convertDraft = ReportType.HB_DYN1_DRAFT;
				}else if(datatype.equals(DataCenterType.HB_ADJUST)){
					convertDraft = ReportType.HB_ADJ_DRAFT;
				}else if(datatype.equals(DataCenterType.SEP_ADJUST)){
					convertDraft = ReportType.SEP_ADJ_DRAFT;
				}
				WorkDraft workdraftNew= new WorkDraft(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("pub_0","01830001-0056")/*@res "区域工作底稿"*/, workdraft.getPk_report(),convertDraft,"",selectedMeasureCol.toArray(new MeasureVO[0] ),workdraft.getPk_hbscheme());
				//xiangymf   ncm
				//xiangymf  专向用加的方法  ncm区域底稿只展示一个对方单位 ，默认a列为关键字且被选中单元格列不超过w  有问题以后再说  20220526
				if(selectedAreas.length==1&&!selectedAreas[0].toString().contains(":")){
					/*AreaPosition are = selectedAreas[0];
					CellPosition cellstar = are.getStart();
					CellPosition end = are.getEnd();*/
					String mstr =  selectedAreas[0].getStart().toString();
					CellPosition cell = CellPosition.getInstance("A"+mstr.substring(1)); 
					Cell[][] cells3 = cellsModel.getCells(cell); 
					getContext().setAttribute("Cell",cells2);
					workdraftNew.setCells(cells3);
					
				}
				AdjustRepDataEditor subEditor = new AdjustRepDataEditor(workdraftNew, curView);
				jTabbedPane.add(workdraftNew.getName(), subEditor);
				subEditor.initRepDataEditor();

				//设置查看底稿为区域工作底稿
				workdraftSHowTab.setSelectedIndex(workdraftSHowTab.getTabCount()-1);
			}
		}
	}

	@Override
	public IPluginActionDescriptor getPluginActionDescriptor() {
		PluginActionDescriptor desc = new PluginActionDescriptor(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("pub_0","01830001-0056")/*@res "区域工作底稿"*/);
		desc.setGroupPaths(doGetDataMenuPaths("ConvertRacgeDraftAction"));
		desc.setExtensionPoints(XPOINT.MENU,XPOINT.POPUPMENU);
//		desc.setIcon("/images/reportcore/refresh.gif");
		desc.setCode("ConvertRacgeDraftAction");
		return desc;
	}
	
	 /**
     * 得到“数据”菜单路径
     * @return
     */
    private String[] doGetDataMenuPaths(String strGroup){
        return new String[]{nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1413007_0","01413007-0022")/*@res "数据"*/,strGroup};//"数据"
    }


}