package nc.ui.wa.ruleex.formular;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.bs.logging.Logger;
import nc.ui.hr.formula.HRFormulaItem;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.UIRefPane;
import nc.ui.uif2.model.AbstractUIAppModel;
import nc.ui.wa.item.view.ItemFormularFactory;
import nc.ui.wa.ref.WaPrmlvRefModel;
import nc.ui.wa.ref.WaSeclvRefModel;
import nc.ui.wa.rule.model.WaRuleModel;
import nc.vo.ml.Language;
import nc.vo.ml.LanguageVO;
import nc.vo.ml.MultiLangContext;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.formulaedit.FormulaItem;
import nc.vo.pub.formulaset.IFormulaValidateType;
import nc.vo.pub.formulaset.VarryTypeDataDict;
import nc.vo.uap.busibean.exception.BusiBeanException;
import nc.vo.wa.grade.AggWaGradeVO;
import nc.vo.wa.grade.IWaGradeCommonDef;
import nc.vo.wa.grade.WaGradeVO;
import nc.vo.wa.grade.WaPrmlvVO;
import nc.vo.wa.grade.WaSeclvVO;

import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;

/**
 * 薪资规则表-公式属性工厂<BR>
 * <BR>
 * 
 * @author: xuhw
 * @date: 2010-4-14 下午02:20:30
 * @since: eHR V6.0
 * @走查人:
 * @走查日期:
 * @修改人:
 * @修改日期:
 */
public class WaRuleFormularFactory extends ItemFormularFactory implements IWaRuleVariableFactory
{

	private static AbstractUIAppModel model;
	private WaRuleTableCreator tableCreator;
	private Map<String, SuperVO[]> prmSecVOCache = new HashMap<String, SuperVO[]>();
	private final Map<String, List<FormulaItem>> tableFileds = new HashMap<String, List<FormulaItem>>();
	private final HashMap<String, UIRefPane> refPaneMap = new HashMap<String, UIRefPane>();
	private AggWaGradeVO[] aggWaGradeVos;

	public WaRuleFormularFactory(AbstractUIAppModel model)
	{
		this.model = model;
	}

	public WaRuleFormularFactory()
	{}

	/**
	 * 得到某个表的所有字段 同时向VarryTypeDataDict中进行注册
	 */
	@Override
	protected List<FormulaItem> createAllFieldItems(String tablename)
	{
		List<FormulaItem> fieldItems = new ArrayList<FormulaItem>();
		if (tablename != null && ((tablename.endsWith(WA_PRMLV_DES) || (tablename.endsWith(WA_SECLV_DES)))))
		{
			aggWaGradeVos = getGradePrmSecInfo();
			try
			{
				processGrade4Formular(aggWaGradeVos, fieldItems, tablename);
			}
			catch (BusinessException e)
			{
				Logger.error(e.getMessage(), e);
				MessageDialog.showErrorDlg(model.getContext().getEntranceUI(), null, e.getMessage());
				return null;
			}
		}
		else
		{
			return super.createAllFieldItems(tablename);
		}

		return fieldItems;
	}

//	@Override
//	public List<FormulaItem> getAllFieldItems(String tablename)
//	{
//		if (tableFileds.get(tablename) != null)
//		{
//			return tableFileds.get(tablename);
//		}
//		else
//		{
//			List<FormulaItem> temp = createAllFieldItems(tablename);
//			 addItems2DataDict(temp);
//			tableFileds.put(tablename, temp);
//			return temp;
//		}
//	}

	private AggWaGradeVO[] getGradePrmSecInfo()
	{
		// 构建薪资标准类别
		// 根据可见性权限过滤
		// 格式 薪资标准类别【级别】
		// 薪资标准类别【档别】
		if (!ArrayUtils.isEmpty(aggWaGradeVos))
		{
			return aggWaGradeVos;
		}
		try
		{
			aggWaGradeVos = ((WaRuleModel) getModel()).queryWagradeVOs(getModel().getContext());
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
			MessageDialog.showHintDlg(getModel().getContext().getEntranceUI(), null, e.getMessage());
			return new AggWaGradeVO[0];
			// 发生异常 给与提示，并且返回基本的属性
		}

		return aggWaGradeVos;
	}

	/**
	 * 把薪资标准转化为公式编辑器识别的形式
	 * 
	 * @author xuhw on 2010-4-14
	 * @param aggWaGradeVos
	 * @param map
	 * @throws BusinessException
	 */
	private void processGrade4Formular(AggWaGradeVO[] aggWaGradeVos, List<FormulaItem> fieldItems, String tablename) throws BusinessException
	{
		if (aggWaGradeVos == null)
		{
			return;
		}
		// 显示名称
		String strDisplayName = null;
		// 薪资级别
		WaPrmlvVO[] prmlvvos = null;
		// 薪资档别
		WaSeclvVO[] seclvvos = null;
		for (AggWaGradeVO aggWaGradeVO : aggWaGradeVos)
		{
			strDisplayName = getDisPlayName((WaGradeVO) aggWaGradeVO.getParentVO());
			prmlvvos = (WaPrmlvVO[]) aggWaGradeVO.getTableVO(IWaGradeCommonDef.WA_PRMLV);
			seclvvos = (WaSeclvVO[]) aggWaGradeVO.getTableVO(IWaGradeCommonDef.WA_SECLV);

			if (tablename.endsWith(WA_PRMLV_DES))
			{
				if (!ArrayUtils.isEmpty(prmlvvos))
				{
					String strPrmDisplayName = strDisplayName + WA_PRMLV_DES;
					HRFormulaItem item = new HRFormulaItem(LEFT_SEPERATOR + aggWaGradeVO.getParentVO().getPrimaryKey() + RIGHT_SEPERATOR + WA_CRITERION + ITEM_SEPERATOR + WA_PRMLV_TABLE, strPrmDisplayName, getStdDes(GRADENAME, strPrmDisplayName), getStdDes(GRADENAME, strPrmDisplayName));
					UIRefPane refpanel = new UIRefPane();
					refpanel.setRefModel(new WaPrmlvRefModel(aggWaGradeVO.getParentVO().getPrimaryKey()));
					item.setEditor(refpanel);
					fieldItems.add(item);
					VarryTypeDataDict.getInstance().addVarryType(LEFT_SEPERATOR + aggWaGradeVO.getParentVO().getPrimaryKey() + RIGHT_SEPERATOR + WA_CRITERION + ITEM_SEPERATOR + WA_PRMLV_TABLE, IFormulaValidateType.STRING);
					getAllFieldRefPane().put(LEFT_SEPERATOR + aggWaGradeVO.getParentVO().getPrimaryKey() + RIGHT_SEPERATOR + WA_CRITERION + ITEM_SEPERATOR + WA_PRMLV_TABLE, (UIRefPane) item.getEditor());
				}
			}
			else
			{
				if (!ArrayUtils.isEmpty(seclvvos))
				{
					String strSecDisplayName = strDisplayName + WA_SECLV_DES;
					HRFormulaItem item = new HRFormulaItem(LEFT_SEPERATOR + aggWaGradeVO.getParentVO().getPrimaryKey() + RIGHT_SEPERATOR + WA_CRITERION + ITEM_SEPERATOR + WA_SECLV_TABLE, strSecDisplayName, getStdDes(GRADENAME, strSecDisplayName), getStdDes(GRADENAME, strSecDisplayName));
					UIRefPane refpanel = new UIRefPane();
					refpanel.setRefModel(new WaSeclvRefModel(aggWaGradeVO.getParentVO().getPrimaryKey()));
					item.setEditor(refpanel);
					fieldItems.add(item);
					VarryTypeDataDict.getInstance().addVarryType(LEFT_SEPERATOR + aggWaGradeVO.getParentVO().getPrimaryKey() + RIGHT_SEPERATOR + WA_CRITERION + ITEM_SEPERATOR + WA_SECLV_TABLE, IFormulaValidateType.STRING);
					getAllFieldRefPane().put(LEFT_SEPERATOR + aggWaGradeVO.getParentVO().getPrimaryKey() + RIGHT_SEPERATOR + WA_CRITERION + ITEM_SEPERATOR + WA_SECLV_TABLE, (UIRefPane) item.getEditor());
				}
			}
		}
	}

	/**
	 * 根据语言环境得到多余文本对应名称
	 * 
	 * @author xuhw on 2010-4-14
	 * @param gradevo
	 * @return
	 */
	private String getDisPlayName(WaGradeVO gradevo)
	{
		LanguageVO langVO = MultiLangContext.getInstance().getCurrentLangVO();
		if (Language.TRAD_CHINESE_CODE.equals(langVO.getLangcode()))
		{
			if (!StringUtils.isBlank(gradevo.getName2()))
			{
				return gradevo.getName2();
			}
		}
		else if (Language.ENGLISH_CODE.equals(langVO.getLangcode()))
		{
			if (!StringUtils.isBlank(gradevo.getName3()))
			{
				return gradevo.getName3();
			}
		}
		else
		{
			return gradevo.getName();
		}

		return gradevo.getName();
	}

	public void setPrmSecVOCache(Map<String, SuperVO[]> prmSecVOCache)
	{
		this.prmSecVOCache = prmSecVOCache;
	}

	public Map<String, SuperVO[]> getPrmSecVOCache()
	{
		return prmSecVOCache;
	}

	/**
	 * 根据用户输入的表名，得到相关的变量
	 * 
	 * @author xuanlt on 2010-3-17
	 * @param tablename
	 * @return
	 * @throws BusiBeanException
	 * @return Map<String,FormulaItem>
	 */
	@Override
	public List<FormulaItem> getAllTables() throws BusiBeanException
	{
		return getTableCreator().getAllTables();
	}

	public WaRuleTableCreator getTableCreator()
	{
		if (tableCreator == null)
		{
			tableCreator = new WaRuleTableCreator();
		}
		return tableCreator;
	}

	/**
	 * @author xuanlt on 2010-5-11
	 * @see nc.ui.wa.formular.HRFormularTableFieldCreator#getAllFieldRefPane()
	 */
	@Override
	public Map<String, UIRefPane> getAllFieldRefPane()
	{
		return refPaneMap;
	}
}
