package nccloud.web.hrwa.waruleex.action.formula.factory;

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

import nc.bs.framework.core.util.ObjectCreator;
import nc.ui.bd.ref.AbstractRefModel;
import nc.ui.bd.ref.model.DefdocDefaultRefModel;
import nc.ui.bd.ref.model.DefdocGridRefModel;
import nc.ui.bd.ref.model.DefdocPkTreeRefModel;
import nc.ui.bd.ref.model.DefdoclistDefaultRefModel;
import nc.ui.bd.ref.model.RegionDefaultRefTreeModel;
import nc.ui.hr.formula.MDEnumRefModel;
import nc.ui.om.ref.PostRefModel;
import nc.ui.wa.ref.WaPrmlvRefModel;
import nc.ui.wa.ref.WaSeclvRefModel;
import nc.vo.bd.ref.RefInfoVO;
import nc.vo.pub.BusinessException;
import nc.vo.uif2.LoginContext;
import nccloud.commons.collections.CollectionUtils;
import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;
import nccloud.web.hr.formula.HRFormulaItem;
import nccloud.web.hr.formula.HRFormulaType4Web;
import nccloud.web.hr.formula.RefInfoAccessor;
import nccloud.web.hr.formula.convert.FuncConvertor;
import nccloud.web.hr.formula.convert.HRVariableConvertor;
import nccloud.web.hr.formula.convert.HrIIfConvertor;
import nccloud.web.hr.formula.convert.HrLogicConvertor;
import nccloud.web.hr.formula.convert.HrRefConvertor;
import nccloud.web.hr.formula.convert.IConvertor;
import nccloud.web.hr.formula.tranlate.AbstractFormulaTranslateFactory;

/**
 * 薪资发放项目公式翻译器
 * 
 * @author YuCheng
 *
 */
public class WaRuleExFormulaTransFactory extends AbstractFormulaTranslateFactory {

	private List<HRFormulaItem> itemVOs;

	/**
	 * variable参照(此数据现在用不到，暂时设置为空，后续发现有用再进行使用)
	 */
	private Map<String, AbstractRefModel> variableAllFieldRefMap = new HashMap<>();
	/**
	 * variable项目
	 */
	private List<HRFormulaItem> variableItemList = new ArrayList<>();

	private List<HRFormulaType4Web> soruceItemList;

	public WaRuleExFormulaTransFactory(List<HRFormulaType4Web> soruceItemList, List<HRFormulaType4Web> tableList,
			String condition,String dynPara) throws BusinessException {
		String[] strs = null;
		if (StringUtils.isNotEmpty(dynPara)) {
			strs = dynPara.split(",");
		}
		// 数据库信息
		if (!CollectionUtils.isEmpty(tableList)) {
			List<HRFormulaItem> itemList = new ArrayList<HRFormulaItem>();
			for (HRFormulaType4Web table : tableList) {
				List<HRFormulaItem> items = table.getItemList();
				if (CollectionUtils.isEmpty(items))
					continue;
				itemList.addAll(items);

				/**
				 * 处理HRVariableConvertor转换器的数据 (此数据现在用不到，暂时设置为空，后续发现有用再进行使用)
				 */
				String tablename = table.getTypeId();
				if ("hi_psnjob".equals(tablename) || "bd_psndoc".equals(tablename)||"wa_prmlv".equals(tablename) || "wa_seclv".equals(tablename)) {
					if (!items.isEmpty()) {
						for (HRFormulaItem item : items) {
							if (item.isMDEnumType()) {
								// 处理枚举
								variableAllFieldRefMap.put(item.getCode(), new MDEnumRefModel(item.getDataTypeid()));
							} else if (item.isRefType()) {
								// 处理参照
								RefInfoVO refInfoVO = RefInfoAccessor.getRefInfoVOByName(item.getRefModelName());
								AbstractRefModel refModel = (AbstractRefModel) ObjectCreator
										.newInstance(refInfoVO.getRefclass());
								// 特殊处理岗位参照
								if (refModel instanceof PostRefModel) {
									refModel = (AbstractRefModel) ObjectCreator.newInstance("hrwa",
											"nc.ui.wa.ref.WaMsPostRefModel");
								}else if (refModel instanceof WaSeclvRefModel) {
									Object[]obs=new Object[]{item.getPara3()};
									refModel = (AbstractRefModel) ObjectCreator.newInstance("hrwa",
											"nc.ui.wa.ref.WaSeclvRefModel",obs);
								}else if (refModel instanceof WaPrmlvRefModel) {
									Object[]obs=new Object[]{item.getPara3()};
									refModel = (AbstractRefModel) ObjectCreator.newInstance("hrwa",
											"nc.ui.wa.ref.WaPrmlvRefModel",obs);
								}else if (refModel instanceof RegionDefaultRefTreeModel) {
										RegionDefaultRefTreeModel refmodel = (RegionDefaultRefTreeModel) refModel;
										if(ArrayUtils.isNotEmpty(strs)){
											for (String str : strs) {
												if (StringUtils.isBlank(str)) {
													continue;
												}
												if (item.getCode().equals("bd_psndoc.nativeplace")
														&& str.contains("nativeplace")) {
													refmodel.setPk_country(str.split("_")[1]);
												}
												if (item.getCode().equals("bd_psndoc.permanreside") && str.contains("permanreside")) {
													refmodel.setPk_country(str.split("_")[1]);
												}
												
											}
										}
										//自定义项默认赋值为中国
										if (item.getCode().contains("glbdef")) {
											refmodel.setPk_country("0001Z010000000079UJJ");
										}
										//refmodel.setPk_country(condition);
								} else if(refModel instanceof DefdocGridRefModel || 
										refModel instanceof DefdocPkTreeRefModel || 
										refModel instanceof DefdoclistDefaultRefModel || 
										refModel instanceof DefdocDefaultRefModel) {
									refModel.setPara1(item.getPara2());
									refModel.setPara2("bd_defdoc");
								}
								refModel.reset();
								/// 修正特殊参照处理逻辑
//								refModel.reset();
//								if (StringUtils.isNotBlank(condition)) {
//									if (refModel instanceof RegionDefaultRefTreeModel) {
//										RegionDefaultRefTreeModel refmodel = (RegionDefaultRefTreeModel) refModel;
//										refmodel.setPk_country(condition);
//									} else {
//										refModel.setPara1(condition);
//									}
//								}
								variableAllFieldRefMap.put(item.getCode(), refModel);
							}
						}
					}
				}
			}
			this.variableItemList = itemList;
			this.itemVOs = itemList;
		}
		// 数据源信息
		this.soruceItemList = soruceItemList;
	}

	@Override
	protected List<IConvertor> getConvertorList(LoginContext context) {
		List<IConvertor> list = new ArrayList<IConvertor>();
		list.add(new HrLogicConvertor());
		list.add(new HrIIfConvertor());
		list.add(new HRVariableConvertor(variableAllFieldRefMap, variableItemList));
		if (!CollectionUtils.isEmpty(soruceItemList)) {
			for (HRFormulaType4Web formulaType : soruceItemList) {
				list.add(new FuncConvertor(formulaType.getItemList()));
			}
		}

		list.add(new HrRefConvertor(variableAllFieldRefMap, context));
		return list;
	}

}
