//package Application;

import java.io.*;
import java.util.Random;
import java.util.Timer;

public class NetworkTask extends Application {

	String[] webpages={"http://www.cnn.com","http://www.ndtv.com", "http://www.indiatimes.com",
		"http://www.cnbc.com","http://www.samachar.com", "http://tvnz.co.nz",
		"http://www.wwltv.com","http://www.timeofindia.com",
		"http://www.wsdu.com",
		"www.nytimes.com"};
	
	public void runapplication(double params, long interval) throws IOException, InterruptedException {
		
		if(params == 0.25)
		{
		long intervalsleep = 0;
		while(intervalsleep < interval)
		{	
			System.out.println("Started download");
			Random rand = new Random();
			int index = Math.abs(rand.nextInt()) % 10;
			String filename = webpages[index];
			
		    Runtime rexec = Runtime.getRuntime();
		    String[] commands = {"wget","-P","webpages", filename};
		    Process proc = rexec.exec(commands);
		    Timer timer = new Timer();
		    timer.schedule(new killtask(proc),10000);
		    proc.waitFor();
		    System.out.println("Downloaded "+filename);
		    intervalsleep += 120000;
			Thread.sleep(120000);
			boolean success = deleteDirectory(new File("webpages"));
			if(success)
			{
				System.out.println("Deleted the webpages");
			}
			else
			{
				System.out.println("Could not delete the webpages");
			}
		}
		}
		else if(params == 0.5)
		{
			long intervalsleep = 0;
			
			while(intervalsleep <= interval)
			{	
				Random rand = new Random();
				int index = Math.abs(rand.nextInt()) % 10;
				String filename = webpages[index];
				
			    Runtime rexec = Runtime.getRuntime();
			    String[] commands = {"wget","-P","webpages", filename};
			    Process proc = rexec.exec(commands);
			    Timer timer = new Timer();
			    timer.schedule(new killtask(proc),10000);
			    proc.waitFor();
			    System.out.println("Downloaded "+filename);
			    intervalsleep += 60000;
				Thread.sleep(60000);
				boolean success = deleteDirectory(new File("webpages"));
				if(success)
				{
					System.out.println("Deleted the webpages");
				}
				else
				{
					System.out.println("Could not delete the webpages");
				}
			}
		}
		else if(params == 0.75)
		{
			long intervalsleep = 0;
			while(intervalsleep <= interval)
			{			
				Random rand = new Random();
				int index = Math.abs(rand.nextInt()) % 10;
				String filename = webpages[index];
			    Runtime rexec = Runtime.getRuntime();
			    String[] commands = {"wget","-P","webpages",filename};
			    Process proc = rexec.exec(commands);
				Timer timer = new Timer();
			    timer.schedule(new killtask(proc),10000);
			    proc.waitFor();
			    intervalsleep += 30000;
			    System.out.println("Downloaded "+filename);
				Thread.sleep(30000);
				boolean success = deleteDirectory(new File("webpages"));
				if(success)
				{
					System.out.println("Deleted the webpages");
				}
				else
				{
					System.out.println("Could not delete the webpages");
				}
			}	
		}
		else if(params == 1)
		{
			long intervalsleep = 0;
			while(intervalsleep <= interval)
			{			
				Random rand = new Random();
				int index = Math.abs(rand.nextInt()) % 10;
				String filename = webpages[index];
			    Runtime rexec = Runtime.getRuntime();
			    String[] commands = {"wget","-P","webpages",filename};
			    Process proc = rexec.exec(commands);
				Timer timer = new Timer();
			    timer.schedule(new killtask(proc),10000);
			    proc.waitFor();
			    intervalsleep += 15000;
			    System.out.println("Downloaded "+filename);
				Thread.sleep(15000);
				boolean success = deleteDirectory(new File("webpages"));
				if(success)
				{
					System.out.println("Deleted the webpages");
				}
				else
				{
					System.out.println("Could not delete the webpages");
				}
			}
		}
	}
	
	public boolean deleteDirectory(File path) {
	    if( path.exists() ) {
	      File[] files = path.listFiles();
	      for(int i=0; i<files.length; i++) {
	         if(files[i].isDirectory()) {
	           deleteDirectory(files[i]);
	         }
	         else {
	           files[i].delete();
	         }
	      }
	    }
	    return( path.delete() );
	  }

}