import java.util.Calendar;
import java.util.GregorianCalendar;

/*Pehr Hovey
 * DeweyTree.java
 * 
 * A class to encapsulate the information needed to display a dewey tree representing one day.
 * 
 */
public class DeweyTree {
int NUM_COUNTS_COLS = 11;
int NUM_COUNTS_ROWS = 10;
int ROW_SUM_INDEX = 10;

int id; //index into trees array in parent class, used for color_buffer stuff


//x,y coords of center of this tree when in grid view, set externally when it is plotted since it changes if the grid is changed
float grid_x;
float grid_y;
boolean grid_selected = false; //if it was selected in grid view


	String ckidate; //which date this represents format: 2008-03-01
	String date_label;//Displayable
	//Since we need to put this at the right cell for the right day, store
	//index into grid as if it was one row and many cols, will need to convert to row,col at draw time
	int month;
	int day_of_month; 
	int year;
	int grid_cell_number; 
	
	int[][] deweyCounts; //the counts for 10 major categories ... the main levels of the tree
	//each row is a main cat (100, 200...), each col 0-9 is subcat (100, 110, 120) col 10 is the total for that row
	double[][] percentages;
	
	double grand_total; //# of transactions total in this tree
	
	DeweyTree(int _id,String[] sections){
//		System.out.println("Making tree for "+sections[0]);
		id = _id;
		ckidate = sections[0];
		
		//parse ckidate to figure out which cell of the grid to put this in.
		//This will be similar to the day # except it may be offset if the month does not start on a sunday
		day_of_month = Integer.parseInt(ckidate.substring(8,10));
		year = Integer.parseInt(ckidate.substring(0,4));
		month = Integer.parseInt(ckidate.substring(5,7));
		
		//Figure out what day of the week the month started on, shift all days accordingly
		 Calendar cal = new GregorianCalendar(year, month-1, 1);  		 
		 int offset = cal.get(Calendar.DAY_OF_WEEK)-1;  //First day of week is returned as 1
		grid_cell_number = day_of_month+offset-1; //want it to be 0-based
		
		String day_name = DeweyForest.day_names[offset]; //0 is sunday, 6 is saturday
//		System.out.println("This is "+month+"/"+day_of_month+"/"+year+" "+". Month started on a "+day_name+" so the offset is "+offset+" and cell# is "+grid_cell_number);
		
		
		
		date_label = ckidate.substring(5,10); //month-day
		 deweyCounts = new int[10][11];
			 for(int row = 0; row < NUM_COUNTS_ROWS; row++){
				 for(int col=0; col < NUM_COUNTS_COLS; col++){
				 deweyCounts[row][col] = Integer.parseInt(sections[1+col+NUM_COUNTS_COLS*row]);
			 }
		 }
			 calculatePercentages();
//			 printAll();
	}
	
	//given deweyCounts calculate what % of the total each main section is to help inform the display
private void calculatePercentages() {
		// TODO Auto-generated method stub
		grand_total = 0;
		percentages = new double[10][11]; //last col is whole 100 cat, other rows are for sub-10 cats. 
		
		for(int i=0; i < NUM_COUNTS_ROWS; i++){
			grand_total+=deweyCounts[i][ROW_SUM_INDEX];
		}
		double tot=0;
		
		for(int i=0; i < NUM_COUNTS_ROWS; i++){
			for(int c=0; c<NUM_COUNTS_COLS; c++){
				if(c==10)
					percentages[i][c] = deweyCounts[i][ROW_SUM_INDEX] / grand_total;
				else{
					//percentages[i][c] = deweyCounts[i][c] / (double)deweyCounts[i][ROW_SUM_INDEX];
					percentages[i][c] = Math.log(500*(deweyCounts[i][c] / (double)deweyCounts[i][ROW_SUM_INDEX]))/Math.log(500);
					
				}
				
//			tot+=percentages[i];
//			System.out.println(percentages[i]+" "+(tot));
			}
		}
		
	}


//	DeweyTree(String date, int[][] counts){
//		ckidate = date;
//		deweyCounts = counts;
//		if(counts == null)
//			deweyCounts = new int[10][11];
//		
//	}
	
	//print just row sums
	public String toString(){
		String result = ckidate+">>";
		for(int i=0; i < deweyCounts.length; i++)
			result+=("d"+i+"00:"+deweyCounts[i][10]+" ");
		return result;
	}
	
	
	public void printAll(){ //print whole array in a formatted grid
		String result = ckidate+":\n";
		for(int r=0; r < 10; r++){
			result+="d"+r+"00:\t";
			for(int c=0; c < 11; c++){
				if(c<10)
					result+=(deweyCounts[r][c]+"\t");
				else
					result+=("sum:"+deweyCounts[r][c]);
			}
			result+="\n";
	    }
		
		System.out.println(result);
	}
	
}

