package com.sagacloud.utils;
/*
 * Author: Jxing
 * Create Time: 2017/12/22
 */

import com.sagacloud.exception.ExpressionException;
import com.sagacloud.parser.simulator.ComputeEngineLexer;
import com.sagacloud.parser.simulator.ComputeEngineParser;
import com.sagacloud.parser.simulator.ComputeEngineScanner;
import com.sagacloud.parser.simulator.ComputeEngineWalker;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;

public class Calculator {
    CommonTreeNodeStream nodes = null;
    ComputeEngineWalker walker = null;
    
    /**
     * 判断表达式是否为求均值
     * @param expression
     * @return
     */
    public static boolean ifAvgExp(String expression) {
    	if(null == expression) {
    		return false;
    	}
    	
    	boolean flag = false;
    	System.out.println(expression.substring(expression.lastIndexOf("/")+1));
    	try {
	    	int varNum = Integer.parseInt(expression.substring(expression.lastIndexOf("/")+1));
	    	expression = expression.substring(1, expression.lastIndexOf(")"));
	    	expression = expression.replace("+", "_");
	    	String[] sa = expression.split("_");
	    	flag = sa.length == varNum;
    	}catch (Exception e) {
			e.printStackTrace();
		}
    	
    	return flag;
    }
    /**
     * 解析表达式，返回表达式中的因数
     * @param expression
     * @return
     * @throws Exception
     */
    public String[] readExpression(String expression) throws Exception
    {
        ANTLRInputStream input = null;
        if(expression == null)
            return null;
        try {
            if(!expression.endsWith("$"))
                expression += "$";
            input = new ANTLRInputStream(new ByteArrayInputStream(expression.getBytes("utf8")));
        }catch (IOException ex)
        {
            ex.printStackTrace();
        }

        ComputeEngineLexer lexer = new ComputeEngineLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ComputeEngineParser parser = new ComputeEngineParser(tokens);
        ComputeEngineParser.prog_return r = null;
        try {
            r = parser.prog();
        }catch (Exception ex)
        {
            throw new ExpressionException(expression + "错误");
        }
        CommonTree t = (CommonTree) r.getTree();
        //System.out.println(t.toStringTree());

        nodes = new CommonTreeNodeStream(t);

        nodes.setTokenStream(tokens);

        ComputeEngineScanner scanner = new ComputeEngineScanner(nodes);
        scanner.prog();
        String[] vars = scanner.varDict.keySet().toArray(new String[0]);
//        for (String var : vars){
//            System.out.println(var);
//        }
        return vars;
    }

    public double compute(String[] varNames, ArrayList<Double> values) throws Exception {
        if(walker == null) {
            if (nodes == null)
                throw new Exception();
            walker = new ComputeEngineWalker(nodes);
        }
        if(varNames.length == 0 || varNames.length != values.size())
            throw new Exception();
        walker.reset();
        walker.clear();
        for(int i = 0; i < varNames.length; ++i)
        {
            walker.put(varNames[i], values.get(i));
        }
        return walker.prog();
    }
    public static void main(String[] args) throws Exception {
		System.out.println(ifAvgExp("(a+b+c)/3"));
 	}
}
