package battery.utility;

import java.io.*;
import java.util.*;

public class Battery {

	public static int MAX_SENSOR = 30;
	public static int MAX_DAYS = 31;
	public static int MAX_LENGHT = 10000;

	public static long[] sensorID = new long[MAX_SENSOR];
	public static long[] capacity = new long[MAX_SENSOR];
	public static int row;

	public static long[][] level = new long[MAX_SENSOR][MAX_LENGHT];
	public static int[][][] levelOneDay = new int[MAX_SENSOR][MAX_DAYS][1440];
	public static int[][] levelAverageDay = new int[MAX_SENSOR][1440];
	public static int[][] levelOneDayL = new int[MAX_SENSOR][MAX_DAYS];
	public static long[][] levelNormal = new long[MAX_SENSOR][MAX_LENGHT];
	public static Date[][] datetime = new Date[MAX_SENSOR][MAX_LENGHT];
	public static long[][][] datetimeOneDay = new long[MAX_SENSOR][MAX_DAYS][1440];
	public static long[][] datetime2 = new long[MAX_SENSOR][MAX_LENGHT];
	public static int[] lenght = new int[MAX_SENSOR];

	public static int getIndex(int s) {
		for (int i = 0; i < row; i++) {
			if (s == sensorID[i]) {
				return i;

			}
		}
		return -1;

	}

	public static void PrintBattery() {
		getlevel();
		// print();
		normalizeBatteryLevel();
		averageBatteryLevel();
	}

	public static void getlevel() {
		for (int i = 0; i < row; i++) {
			int index = 0;
			for (int j = 0; j < Readings.row; j++) {
				if (sensorID[i] == Readings.sensor[j]) {
					level[i][index] = Readings.batteryLevel[j];
					datetime[i][index] = Readings.datetime[j];
					datetime2[i][index] = Readings.datetime2[j];
					index++;
				}
			}
			lenght[i] = index;
			System.out.println("battery:  " + sensorID[i] + " lenght[i]:  "
					+ lenght[i]);
		}
	}

	public static void normalizeBatteryLevel() {
		for (int i = 0; i < row; i++) {
			// System.out.println("sensorID: "+ sensorID[i]);

			if (lenght[i] >= 2) {
				levelNormal[i][0] = level[i][0];
				levelNormal[i][lenght[i] - 1] = level[i][lenght[i] - 1];
			}
			int max = 100;
			for (int j = 1; j < lenght[i] - 1; j++) {
				if (level[i][j] > level[i][j - 1] + max
						&& level[i][j] > level[i][j + 1] + max) {
					levelNormal[i][j] = (level[i][j - 1] + level[i][j + 1]) / 2;
				} else if (j < lenght[i] - 2
						&& level[i][j] > level[i][j - 1] + max
						&& level[i][j + 1] > level[i][j + 2] + max) {
					levelNormal[i][j] = (level[i][j - 1] + level[i][j + 2]) / 2;
					;
					levelNormal[i][j + 1] = (level[i][j - 1] + level[i][j + 2]) / 2;
					//System.out.println("     sensorID: " + sensorID[i]
					//		+ " Datetime: " + datetime[i][j]);
					j++;
				} else
					levelNormal[i][j] = level[i][j];
			}
			for (int j = 0; j < lenght[i]; j++) {
				if (levelNormal[i][j] > capacity[i]) {
					levelNormal[i][j] = capacity[i];
				}
			}
		}
	}

	public static void averageBatteryLevel() {
		for (int s = 0; s < row; s++) {
			Date date = new Date(Readings.minDate.getTime());
			Date date2 = new Date(Readings.minDate.getTime());
			date2.setDate(date.getDate() + 1);
			for (int i = 0; !date.after(Readings.maxDate); i++) {
				// System.out.println(" Row:"+i+" Date"+ date + " Date2: "+
				// date2);
				int count = 0;
				for (int j = 0; j < lenght[s]; j++) {
					if (!datetime[s][j].before(date)
							&& datetime[s][j].before(date2)) {
						levelOneDay[s][i][count] = (int) levelNormal[s][j];
						datetimeOneDay[s][i][count] = datetime2[s][j];
						// if (i==7)
						// System.out.println(" sensorID:"+sensorID[s]+"
						// Datetime "+ datetime[s][j] + " level: "+
						// level[s][j]);
						count++;
					}
				}
				levelOneDayL[s][i] = count;
				date.setDate(date.getDate() + 1);
				date2.setDate(date.getDate() + 1);
			}
			for (int j = 0; j < 31; j++) {
//				System.out.println("Sensor: " + sensorID[s] + " Days :" + j
//						+ " levelOneDayL:" + levelOneDayL[s][j]);
			}
		}

		// =========== AVERAGE
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < 31; j++) {
				extendLevel(i, j);
			}
		}
		int[] workingDays = new int[MAX_SENSOR];
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < 31; j++) {
				if ((levelOneDayL[i][j] > 10)) {
					average(i, j);
		//			System.out.println("Sensor-Date: " + sensorID[i] + " " + j
		//					+ " levelOneDayL:" + levelOneDayL[i][j]);
					workingDays[i] = workingDays[i] + 1;
				} 
			}
		}
		for (int i = 0; i < row; i++) {
		//	System.out.println("@@@@@@Sensor-Date: " + sensorID[i] + " workingDays: " + workingDays[i]) ;
			for (int j = 0; j < 1440; j++) {
				if (workingDays[i]>0)
					AverageBattery.level[i][j]= AverageBattery.level[i][j]/workingDays[i];
			}
		}
			
	}

	public static void extendLevel(int s, int n) {
		int pre = AverageBattery.MAX_LENGHT - 1;
		for (int j = 0; j < levelOneDayL[s][n]; j++) {
			Date d = new Date(datetimeOneDay[s][n][j]);
			int hour = d.getHours();
			int minute = d.getMinutes();
			int m = hour * 60 + minute;
			for (int i = pre; i >= m; i--) {
				AverageBattery.levelOneDay[s][n][i] = levelOneDay[s][n][j];
				pre--;
			}
		}
		for (int i = pre; i >= 0; i--) {
			AverageBattery.levelOneDay[s][n][i] = 0;
			pre--;
		}
	}

	
	
	
	public static void average(int s, int d) {
		for (int i = 0; i < 1440; i++) {
			AverageBattery.level[s][i] = AverageBattery.level[s][i]
					+ AverageBattery.levelOneDay[s][d][i];
		}
	}

	public static void print() {
		for (int i = 0; i < row; i++) {
			System.out.print("Row" + i + "  ");
			System.out.print(sensorID[i]);
			System.out.print("  ");
			System.out.print(capacity[i]);
			System.out.println("  ");
		}

	}
}
