package com.engine.salary.formlua.func.compare;

import com.alibaba.fastjson.JSON;
import com.engine.salary.formlua.entity.parameter.DataType;
import com.engine.salary.formlua.func.logic.LogicUtils;
import com.engine.salary.formlua.util.ExcelParamUtil;
import com.engine.salary.formlua.util.IgnoreParamFilter;
import com.ql.util.express.instruction.op.OperatorIn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


public class InOperator extends OperatorIn {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Override
	public Object executeInner(Object[] list) throws Exception {
		DataType result=new DataType();
		result.setDataType(DataType.BOOL);
		if(list.length<1||list[1]==null){
			Integer number= IgnoreParamFilter.getSetFuncNumber("IN");
		}
		List<Object> paramList=new ArrayList<>();
		paramList.add(list[0]);
		Object[] objectArray=(Object[])list[1];
		for (int i=0;i<objectArray.length;i++){
			paramList.add(ExcelParamUtil.getParamContent(objectArray[i],"string"));
		}
//		IgnoreParamFilter.filterInFunc(paramList,"IN");
		if(list.length==0){
			result.setContent(false);
			return result;
		}

		paramList=new ArrayList<>();
		paramList.add(null);
		List<Object> dataArray=new ArrayList<>();
		if(list[0] instanceof DataType){
			DataType dataType=(DataType)list[0];
			if(dataType.getDataType().equals(DataType.OPTION)){
				if(dataType.getContent()!=null){
					String[] arrys=dataType.getContent().toString().split(",");
					for (int i=0;i<arrys.length;i++){
						dataArray.add(arrys[i]);
					}
				}
			}else {
				dataArray.add(ExcelParamUtil.getParamContent(dataType,"string"));
			}
		}else {
			dataArray.add(ExcelParamUtil.getParamContent(list[0],"string"));
		}

		for (int i=0;i<objectArray.length;i++){
			if(objectArray[i] instanceof DataType){
				DataType dataType=(DataType)objectArray[i];
				if(dataType.getDataType().equalsIgnoreCase(DataType.OPTION)){
					paramList.add(ExcelParamUtil.getParamContent(objectArray[i],""));
				}else{
					paramList.add(ExcelParamUtil.getParamContent(objectArray[i],"string"));
				}
			}else {
				paramList.add(ExcelParamUtil.getParamContent(objectArray[i],"string"));
			}
		}
		for (Object object:dataArray){
			if(paramList.get(0)!=null){
				paramList.remove(0);
			}
			paramList.add(0,object);
			logger.info("IN函数参数："+ JSON.toJSONString(paramList));
			boolean isIn=(boolean)super.executeInner(paramList.toArray());
			logger.info("IN函数执行结果："+isIn);
			if(!isIn){
				result.setContent(false);
				break;
			}else{
				result.setContent(true);
			}
		}
		if(result.getContent() == null){
			result.setContent(false);
		}
		LogicUtils.buildInFilterParam(result,list);
		return result;
	}

	public InOperator(String aName) {
		super(aName);
	}

	public InOperator(String aAliasName, String aName, String aErrorInfo) {
		super(aAliasName, aName, aErrorInfo);
	}
}
