/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.solr.analytics;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.lang.StringUtils;
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.analytics.util.MedianCalculator;
import org.apache.solr.analytics.util.PercentileCalculator;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.util.ExternalPaths;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.google.common.collect.ObjectArrays;

@SuppressCodecs({"Lucene3x","Lucene40","Lucene41","Lucene42","Appending","Asserting"})
public class AbstractAnalyticsStatsTest extends SolrTestCaseJ4 {
  
  protected static final String[] BASEPARMS = new String[]{ "q", "*:*", "indent", "true", "olap", "true", "rows", "0" };
  protected static final HashMap<String,Object> defaults = new HashMap<String,Object>();

  public static enum VAL_TYPE {
    INTEGER("int"),
    LONG("long"),
    FLOAT("float"),
    DOUBLE("double"),
    STRING("str"),
    DATE("date");

    private VAL_TYPE (final String text) {
      this.text = text;
    }

    private final String text;

    @Override
    public String toString() {
      return text;
    }
  }

  static private Document doc;
  static private XPathFactory xPathFact =  XPathFactory.newInstance();

  static private String rawResponse;

  public static void setResponse(String response) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true); // never forget this!
    DocumentBuilder builder = factory.newDocumentBuilder();
    doc = builder.parse(new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8"))));
    xPathFact = XPathFactory.newInstance();
    rawResponse = response;
  }

  protected String getRawResponse() {
    return rawResponse;
  }

  public Object getStatResult(String section, String name, VAL_TYPE type) throws XPathExpressionException {

    // Construct the XPath expression. The form better not change or all these will fail.
    StringBuilder sb = new StringBuilder("/response/lst[@name='stats']/lst[@name='").append(section).append("']");

    // This is a little fragile in that it demands the elements have the same name as type, i.e. when looking for a
    // VAL_TYPE.DOUBLE, the element in question is <double name="blah">47.0</double>.
    sb.append("/").append(type.toString()).append("[@name='").append(name).append("']");
    String val = xPathFact.newXPath().compile(sb.toString()).evaluate(doc, XPathConstants.STRING).toString();
    try {
      switch (type) {
        case INTEGER: return Integer.parseInt(val);
        case DOUBLE:  return Double.parseDouble(val);
        case FLOAT:   return Float.parseFloat(val);
        case LONG:    return Long.parseLong(val);
        case STRING:  return val;
        case DATE:    return val;
      }
    } catch (Exception e) {
      fail("Caught exception in getStatResult, xPath = " + sb.toString() + " \nraw data: " + rawResponse);
    }
    fail("Unknown type used in getStatResult");
    return null; // Really can't get here, but the compiler thinks we can!
  }


  public <T extends Number & Comparable<T>> Double calculateNumberStat(ArrayList<T> list, String stat) {
    Double result;
    if (stat.equals("median")) {
      result = MedianCalculator.getMedian(list);
    } else if (stat.equals("mean")) {
      double d = 0;
      for (T element : list) {
        d += element.doubleValue();
      }
      result = Double.valueOf(d/list.size());
    } else if (stat.equals("sum")) {
      double d = 0;
      for (T element : list) {
        d += element.doubleValue();
      }
      result = Double.valueOf(d);
    } else if (stat.equals("sumOfSquares")) {
      double d = 0;
      for (T element : list) {
        d += element.doubleValue()*element.doubleValue();
      }
      result = Double.valueOf(d);
    } else if (stat.equals("stddev")) {
      double sum = 0;
      double sumSquares = 0;
      for (T element : list) {
        sum += element.doubleValue();
        sumSquares += element.doubleValue()*element.doubleValue();
      }
      result = Math.sqrt(sumSquares/list.size()-sum*sum/(list.size()*list.size()));
    } else {
      throw new IllegalArgumentException();
    }
    return result;
  }

  public <T extends Comparable<T>> Object calculateStat(ArrayList<T> list, String stat) {
    Object result;
    if (stat.contains("perc_")) {
      double[] perc = new double[]{Double.parseDouble(stat.substring(5))/100};
      result = PercentileCalculator.getPercentiles(list, perc).get(0);
    } else if (stat.equals("count")) {
      result = Long.valueOf(list.size());
    } else if (stat.equals("unique")) {
      HashSet<T> set = new HashSet<T>();
      set.addAll(list);
      result = Long.valueOf((long)set.size());
    } else if (stat.equals("max")) {
      Collections.sort(list);
      result = list.get(list.size()-1);
    } else if (stat.equals("min")) {
      Collections.sort(list);
      result = list.get(0);
    } else {
      result = null;
    }
    return result;
  }

  @SuppressWarnings("unchecked")
  public <T extends Comparable<T>> Long calculateMissing(ArrayList<T> list, String type) {
    T def = (T)defaults.get(type);
    long miss = 0;
    for (T element : list) {
      if (element.compareTo(def)==0) {
        miss++;
      }
    }
    return Long.valueOf(miss);
  }
  
  public static SolrQueryRequest request(String...args){
    return SolrTestCaseJ4.req( ObjectArrays.concat(BASEPARMS, args,String.class) );
  }
  
  public static String[] fileToStringArr(String fileName) throws FileNotFoundException {
    Scanner file = new Scanner(new File(ExternalPaths.SOURCE_HOME, fileName), "UTF-8");
    ArrayList<String> strList = new ArrayList<String>();
    while (file.hasNextLine()) {
      String line = file.nextLine();
      line = line.trim();
      if( StringUtils.isBlank(line) || line.startsWith("#")){
        continue;
      }
      String[] param = line.split("=");
      strList.add(param[0]);
      strList.add(param[1]);
    }
    return strList.toArray(new String[0]);
  }
  

}

@@ -4303,24 +4303,51 @@
 eption e) %7B%0A
+      e.printStackTrace();%0A
       fail(%22

print exception stack trace rather than eating it