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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.salary.formlua.core.exception.ExcelRunTimeException;
import com.engine.salary.formlua.entity.parameter.DataType;
import com.engine.salary.formlua.util.ErrorUtil;
import com.engine.salary.formlua.util.ExcelParamUtil;
import com.engine.salary.formlua.util.IgnoreParamFilter;
import com.ql.util.express.ArraySwap;
import com.ql.util.express.InstructionSetContext;
import com.ql.util.express.OperateData;
import com.ql.util.express.instruction.op.OperatorIf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class IfOperator extends OperatorIf {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public OperateData executeInner(InstructionSetContext parent, ArraySwap list) throws Exception {
        Integer number = IgnoreParamFilter.getSetFuncNumber("IF");
        if (list == null || list.length != 3) {
            JSONObject errorJson = ErrorUtil.buildError("IF", number, number, "IF函数只允许三个参数");
            throw new ExcelRunTimeException("IF函数只允许三个参数");
        }
        Object obj = list.get(0).getObject(parent);
        logger.info("IF参数：" + JSON.toJSONString(obj));
        boolean isDataTypeBool = false;
        DataType boolDtaType = null;
        if (obj != null && obj instanceof DataType) {
            boolDtaType = (DataType) obj;
            if (boolDtaType.getContent() != null && (boolDtaType.getContent() instanceof Boolean)) {
                isDataTypeBool = true;
                obj = boolDtaType.getContent();
            } else {
                switch (obj + "") {
                    case "true":
                        obj = true;
                        isDataTypeBool = true;
                        break;
                    case "false":
                        obj = false;
                        isDataTypeBool = true;
                        break;
                    default:
                        isDataTypeBool = false;
                        break;
                }
            }
        } else if (obj instanceof Boolean) {
            isDataTypeBool = true;
        }
        if (obj == null) {
            JSONObject errorJson = ErrorUtil.buildError("IF", number, number, "IF函数参数不能为空");
            throw new RuntimeException(errorJson.getString("msg"));
        } else if (!(obj instanceof Boolean) && (!(obj instanceof DataType))) {
            JSONObject errorJson = ErrorUtil.buildError("IF", number, number, "IF函数判断条件必须是真假值");
            throw new RuntimeException(errorJson.getString("msg"));
        } else if (!isDataTypeBool) {
            JSONObject errorJson = ErrorUtil.buildError("IF", number, number, "IF函数判断条件必须是真假值");
            throw new RuntimeException(errorJson.getString("msg"));
        } else {
            if (((Boolean) obj).booleanValue() == true) {
                OperateData operateData = list.get(1);
                Object object = operateData.getObject(parent);
//				if(object instanceof DataType){
//					String type= ExcelParamUtil.getParamType(object);
//					if(type.equalsIgnoreCase("date")){
//						object=ExcelParamUtil.getParamContent(object,"string");
//					}
//				}
                OperateData operateData1 = new OperateData(ExcelParamUtil.getParamContent(object, ""), operateData.getType(parent));
                return operateData1;
            } else {
                OperateData operateData = list.get(2);
                Object object = operateData.getObject(parent);
//				if(object instanceof DataType){
//					String type=ExcelParamUtil.getParamType(object);
//					if(type.equalsIgnoreCase("date")){
//						object=ExcelParamUtil.getParamContent(object,"string");
//					}
//				}
                OperateData operateData1 = new OperateData(ExcelParamUtil.getParamContent(object, ""), operateData.getType(parent));
                return operateData1;
            }
        }
    }

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

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