package udo.storage;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.PriorityQueue;

import udo.storage.Task.TaskType;

public class Reminder {
	public static final String EOL = System.getProperty("line.separator");
	//private static File storageFile;
	private static PriorityQueue<Task> taskQueue;

	public static void main(String[] args) throws IOException{

		PriorityQueue<Task> pq = PQ();
		System.out.println("taskQueue "+pq);
		remind();

	}

	//read from json file
	static String lastPath;
	public static PriorityQueue<Task> PQ(){
		taskQueue = new PriorityQueue<Task>(10, new ReminderComparator());
		ArrayList<Task> taskList = new ArrayList<Task>();
		try {
			System.out.println("Reading JSON file from setting");
			FileReader fr = new FileReader("setting.txt");
			BufferedReader br = new BufferedReader(fr);
			lastPath = br.readLine();
			br.close();
			taskList = JsonProcessor.readJson(lastPath);
			/*
			taskList.add(new Task(TaskType.DEADLINE, "fighting", new GregorianCalendar(2010,01,03), 
					new GregorianCalendar(2011,01,02),new GregorianCalendar(2010,01,03), 5,new GregorianCalendar(2010,01,03),
					"personal", false, false));
			taskList.add(new Task(TaskType.DEADLINE, "jogging", new GregorianCalendar(2010,01,03), 
					new GregorianCalendar(2011,01,02),new GregorianCalendar(2010,01,03), 10,new GregorianCalendar(2010,01,02),
					"personal", false, false));
			taskList.add(new Task(TaskType.DEADLINE, "sleeping", new GregorianCalendar(2010,01,03), 
					new GregorianCalendar(2011,01,02),new GregorianCalendar(2010,01,03), 15,new GregorianCalendar(2010,01,05),
					"personal", false, false));
			taskList.add(new Task(TaskType.DEADLINE, "crying", new GregorianCalendar(2010,01,03), 
					new GregorianCalendar(2011,01,02),new GregorianCalendar(2010,01,03), 15,new GregorianCalendar(2010,01,05),
					"personal", false, true));
			*/
			for (int i =0; i<taskList.size(); i++) {
				if (!taskList.get(i).isDone())
					taskQueue.add(taskList.get(i));
			}
		} catch (Exception ex) {
			PQ();
		}
		return taskQueue;
	}
	public static ArrayList<Task> remind() {
		GregorianCalendar current = new GregorianCalendar();
		ArrayList<Task> remindList = new ArrayList<Task>();
		Iterator<Task> ite = taskQueue.iterator(); 
		while (ite.hasNext()) {
			Task curTask = ite.next();
			//	System.out.println("test "+curTask.getReminder().getTime());
			//System.out.println("vs "+current.getTime());
			if (curTask.getReminder().getTime().compareTo(current.getTime())<=0)
				remindList.add(curTask);
			else 
				break;
		}
		//System.out.println("HERE"+remindList);
		return remindList;
	}
}

@@ -223,16 +223,68 @@
 tyQueue;
+%0Aimport java.util.Timer;%0Aimport java.util.TimerTask;
 %0A%0Aimport
@@ -334,16 +334,34 @@
 eminder 
+extends TimerTask 
 %7B%0A%09publi
@@ -655,16 +655,57 @@
 mind();%0A
+%09%09System.out.println(%22HERE %22+remindList);
 %0A%09%7D%0A%0A%09//
@@ -1169,14 +1169,8 @@
 h);%0A
-%09%09%09/*%0A
 %09%09%09t
@@ -2112,13 +2112,8 @@
 ));%0A
-%09%09%09*/
 %0A%09%09%09
@@ -2341,521 +2341,319 @@
 %7B%0A%09%09
-GregorianCalendar current = new GregorianCalendar();%0A%09%09ArrayList%3CTask%3E remindList = new ArrayList%3CTask%3E();%0A%09%09Iterator%3CTask%3E ite = taskQueue.iterator(); %0A%09%09while (ite.hasNext()) %7B%0A%09%09%09Task curTask = ite.next();%0A%09%09%09//%09System.out.println(%22test %22+curTask.getReminder().getTime());%0A%09%09%09//System.out.println(%22vs %22+current.getTime());%0A%09%09%09if (curTask.getReminder().getTime().compareTo(current.getTime())%3C=0)%0A%09%09%09%09remindList.add(curTask);%0A%09%09%09else %0A%09%09%09%09break;%0A%09%09%7D%0A%09%09//System.out.println(%22HERE%22+remindList);%0A%09%09return remindList
+Timer timer = new Timer();%0A%09%09if (!taskQueue.isEmpty()) %7B%0A%09%09timer.schedule(new Reminder(), taskQueue.peek().getReminder().getTime());%0A%09%09%7D%0A%09%09return remindList;%0A%09%7D%0A%09static ArrayList%3CTask%3E remindList = new ArrayList%3CTask%3E();%0A%09public Reminder() %7B%0A%09%7D%0A%09@Override%0A%09public void run() %7B%0A%09%09remindList.add(taskQueue.poll())
 ;%0A%09%7D

Edit Reminder