package com.njackson.util;

import android.util.Log;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: njackson
 * Date: 24/08/2013
 * Time: 20:14
 * To change this template use File | Settings | File Templates.
 */
public class AltitudeGraphReduce {
    
    private static final String TAG = "PB-AltitudeGraphReduce";

    private static AltitudeGraphReduce _instance;

    private ArrayList<Integer> _altitudeBins = new ArrayList<Integer>();
    private long _lastAltitudeBinChange = 0;
    private int _altitudeBinSizeMs = 120000;

    private int _altitudeMax = 0;
    private int _altitudeMin = 99999;

    public void setBinInterval(int value) {
        _altitudeBinSizeMs = value;
    }

    public int getMax() {
        return _altitudeMax;
    }

    public int getMin() {
        return _altitudeMin;
    }

    public ArrayList<Integer> getCache() {
        return _altitudeBins;
    }

    public void setCache(ArrayList<Integer> value) {
         _altitudeBins = value;
    }

    public AltitudeGraphReduce() {

    }

    public static AltitudeGraphReduce getInstance() {
        if(_instance == null)
            _instance = new AltitudeGraphReduce();
        return _instance;
    }

    public void addAltitude(int altitude, long time, float distance) {
        if (time == 0) {
            time = System.currentTimeMillis();
        }
        Log.d(TAG, "addAltitude("+altitude+", "+time+", "+distance+")");
        
        if(_lastAltitudeBinChange == 0) {
            _altitudeBins.add(altitude); // initialise the first bin
            _lastAltitudeBinChange = time;
        }

        if(altitude > _altitudeMax)
            _altitudeMax = altitude;
        if(altitude < _altitudeMin)
            _altitudeMin = altitude;

        if (_lastAltitudeBinChange + _altitudeBinSizeMs > time) {
            Log.d(TAG, _lastAltitudeBinChange + "+" + _altitudeBinSizeMs + "=" + (_lastAltitudeBinChange+_altitudeBinSizeMs) + " > " + time);
            _altitudeBins.set(
                    _altitudeBins.size()-1,
                    (_altitudeBins.get(_altitudeBins.size()-1) + altitude) / 2
            ); // set the current altitude into the bin and average
        } else {
            Log.d(TAG, _lastAltitudeBinChange + "+" + _altitudeBinSizeMs + "=" + (_lastAltitudeBinChange+_altitudeBinSizeMs) + " <= " + time);
            _altitudeBins.add(altitude); // create a new bin and add the altitude
            _lastAltitudeBinChange = time;
        }

    }

    public int[] getGraphData() {
        // calculate our graph based upon the stored data
        double binsPerBar = 14.0 / (double)_altitudeBins.size();
        int[] graphData = new int[14];
        double binCount = binsPerBar;

        if(binsPerBar > 1) {
            binsPerBar = 1;
            binCount = 0;
        }

        int currentBinCount =0;
        int currentBinItems =0;
        int lastBin = 0;

        for(int n=0; n < _altitudeBins.size();n++)
        {
            currentBinCount += _altitudeBins.get(n);
            currentBinItems ++;
            binCount += binsPerBar;

            if((int)binCount == lastBin + 1) {
                graphData[lastBin] = (currentBinCount / currentBinItems);
                lastBin ++;
                currentBinCount = 0;
                currentBinItems = 0;
            }
        }

        return graphData;
    }

    public void restData() {
        _altitudeBins = new ArrayList<Integer>();
        _lastAltitudeBinChange = 0;
        _altitudeMax =0;
        _altitudeMin=99999;
    }

}

@@ -601,16 +601,59 @@
  120000;
+%0A    private int _numberAltitudesInBin = 0;
 %0A%0A    pr
@@ -1940,32 +1940,62 @@
       Log.d(TAG,
+ _numberAltitudesInBin + %22:%22 +
  _lastAltitudeBi
@@ -2240,23 +2240,73 @@
 -1) 
-+ altitude) / 2
+* _numberAltitudesInBin + altitude) / (_numberAltitudesInBin + 1)
 %0A   
@@ -2382,39 +2382,145 @@
     
-%7D else %7B%0A            Log.d(TAG,
+    _numberAltitudesInBin++;%0A        %7D else %7B%0A            _numberAltitudesInBin = 1;%0A            Log.d(TAG, _numberAltitudesInBin + %22:%22 +
  _la

pondered average