package dotplots;

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.swing.JTabbedPane;
import javax.swing.JFrame;
import battery.graph.GraphPanel;
import battery.utility.Battery;
import battery.utility.Points;
import battery.utility.Readings;
import battery.utility.Stock;

public class ReadCSV {
	public static double h;
	public static double[] X= new double[1000];
	
	public static void main(String[] args) {
		X[0] = Double.NEGATIVE_INFINITY;
		X[1] = 2.;
		X[2] = 5.;
		X[3] = 6.;
		X[4] = 10.;
		X[5] = 13.;
		h = 2.;
		dotPlots();
		
		
		JTabbedPane jtp = new JTabbedPane();
		JFrame solutionWindow = new JFrame("Sensor network and Stock");
		solutionWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		solutionWindow.getContentPane().add(jtp);

		jtp.add(new GraphPanel());
		solutionWindow.setBounds(100, 00, 1100, 850);
//		solutionWindow.show();
	}

	public static void dotPlots() {
		double x= 6.1;
		System.out.println(" Count  : " +w(x));	
	}
	public static double w(double x) {
		int count =0;
		double g=g(x);
		System.out.println(" g: " +g);
		
		for(int i=1; X[i]!=0.0; i++){
			if ((X[i]-g)/h < 0.5 && (X[i]-g)/h > -0.5 ){
				count++;
			}
		}
	return count;
	}
	public static double g(double x) {
		int j = getMaxSeparatedBelowX(x);
		System.out.println(" getMaxSeparatedBelowX  : " +X[j]);
		int k = getSmallestValueInStack(x,j);
		System.out.println(" getSmallestValueInStack: " +X[k]);
		int l = getLargestValueInStack(k);
		System.out.println(" getLargestValueInStack : " +X[l]);
		
		double v =0.;
		if (k!=0){
			if (X[k]-X[k-1]>h){
				v=(X[l]-X[k])/2.;
			}
		}
		return (X[k]+v);
	}
	
	public static int getMaxSeparatedBelowX(double x) {
		int j =0;
		for(int i=1; X[i]<x && X[i]!=0.0; i++){
			if (X[i]-X[i-1]>h){
				j=i;
			}
		}
		return j;
	}
	public static int getSmallestValueInStack(double x, int j) {
		int k =0;
		for(int i=1; X[i]<x && X[i]!=0.0; i++){		
			if ((x-floorFunction(i,j)) < h && (x-floorFunction(i,j))>= 0){
				System.out.println(" floorFunction(i,j): " +floorFunction(i,j));	
				k=i;
				break;
			}
		}
		return k;
	}
	public static int getLargestValueInStack(int k) {
		int l =0;
		for(int i=1; X[i]!=0.0; i++){
			if (X[i]-X[k]<h && X[i]-X[k]>0){
				l=i;
			}
		}
		return l;
	}
	public static int floorFunction(int i, int j) {
		int steps = (int) ((X[i]-X[j])/h);
		return (int) (X[j]+ h*steps) ;
	}
}
