import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;


/*
 * Retrieves system parameters at a predefined interval
 */
public class SystemProfiler {

	public LlamaAdaptive algo = new LlamaAdaptive();
	public WindowsSystemParameters sysparams = new WindowsSystemParameters();
	public NetworkTask app = new NetworkTask();
	FileHandler filehandler = new FileHandler();
	ClientServerSync csync = new ClientServerSync();
	public static String old_date="null";
	public static String new_date="null";
	public static int file_count=0;
	
	
	public SystemProfiler(int SleepInterval)
	{
		this.setSleepInterval(SleepInterval);
	}

	//Checks OS type and runs the timer to reequest for parameters at a specified interval
	public void getSystemProfile() throws InterruptedException, FileNotFoundException, IOException{
		String os_name, os_arch;
		boolean learned = false;
		os_name = System.getProperty( "os.name" );
		os_arch = System.getProperty( "os.arch" );
       
		long delay = 0;
		long period = 10000;
		
		
		Timer timer = new Timer();
		timer.schedule(new ClientTimerTask(),delay,period);
		
		LearningTool learn = new LearningTool();
		
		List params = sysparams.Scheduler();
		
		boolean filepresent = learnedearlier(PropertyFileValues.appenergyinfo);
		
		if(filepresent)
		{
			learned = true;
		}
		else if(new Integer(String.valueOf(params.get(5))).intValue() == 0)
		{
		  learn.learnenergypattern();
		}
		
		// set up a time to check for whether you plugged in
		
		
		
		
		ClientServerSync sync = new ClientServerSync();
		
		
		while(true)
		{
			// do the
			boolean charging = false;
			params = sysparams.Scheduler();
			
			
		
			File dir = new File(PropertyFileValues.destination_dir);
			String[] file_list = dir.list();
			
			// uploading a file to the server
			if(file_list.length > 1)
			{
				String filename =  "Logger_"+String.valueOf(params.get(0)).substring(0, 10);
				for(int i=0;i<file_list.length;i++)
				{
					String filetobesend = file_list[i];
					if(filetobesend.compareTo(filename) != 0)
					{
						System.out.println("Mac address "+String.valueOf(params.get(6)));
						
						String retval = sync.connect(String.valueOf(params.get(6)), filetobesend);
						if(retval.compareTo("success") == 0)
						{
							System.out.println("File Send  "+file_list[i]);
							FileHandler.moveFileToArchive(filetobesend);
						}
					}
				}
			}
			
			rechargeinfo time = new rechargeinfo();
			if(new Integer(String.valueOf(params.get(5))).intValue() == 1)
			{
				System.out.println("Charging");
				charging= true;
				
			}
			
			
			time.dotask = false;
			
				
				
			if(learned == true && !charging)
			{
				System.out.println("running algorithm");
			  time = algo.runalgorithm(params,charging);
			}
			else if(charging == true)
			{
				time = algo.runalgorithm(params, charging);
			}
			
	
			
			if(time.dotask)
			 app.runapplication(time.brightnesslevel, PropertyFileValues.sleepinterval);
			
			if(learned == false)
			{
				if(new Integer(String.valueOf(params.get(5))).intValue() == 0)
				{
					  learn.learnenergypattern();	
				}
			}
			
			filepresent = learnedearlier(PropertyFileValues.appenergyinfo);
			
			if(filepresent)
				learned = true; 
		}
	}
	
	
	private boolean learnedearlier(String fname)
	{
		try
		{
		FileReader f = new FileReader(new File(fname));
		BufferedReader fr = new BufferedReader(f);
		int count = 0;
		
		String line = fr.readLine();
		
		while(line != null)
		{
			count ++;
			line = fr.readLine();
		}
		System.out.println("Count "+count);
		if(count < 3)
			return false;
		
		fr.close();
		
		
		return true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}






	/*******************Setters and Getters******************/
	protected int sleepInterval;




	public int getSleepInterval() {
		return sleepInterval;
	}

	public void setSleepInterval(int sleepInterval) {
		this.sleepInterval = sleepInterval;
	}




}

