package udo.storage;

import java.util.ArrayList;

import edu.emory.mathcs.backport.java.util.Collections;
import udo.storage.Task.TaskType;

public class TimeSlots {
    private static final String CONTENT_FREE_SLOT = "Free slot";

	private ArrayList<Task> occupiedSlots;
	private ArrayList<Task> freeSlots;
	
	public TimeSlots(){
		occupiedSlots = new ArrayList<Task>();
		freeSlots = new ArrayList<Task>();
	}
	
	public TimeSlots(ArrayList<Task> taskList){
		occupiedSlots = new ArrayList<Task>();
		freeSlots = new ArrayList<Task>();
		
		addEventTasks(taskList);
		
		Collections.sort(occupiedSlots, new StartTimeComparator());
		
		mergeSlots();
		setFreeSlots();
	}

	private void addEventTasks(ArrayList<Task> taskList) {
		for (int i = 0; i < taskList.size(); i++){
			if (taskList.get(i).getTaskType() == TaskType.EVENT){
				occupiedSlots.add(taskList.get(i).copy());
			}
		}
	}

	private void mergeSlots(){
		for (int i = 0; i <occupiedSlots.size() -1; i++){
			if (occupiedSlots.get(i).getEnd().compareTo(occupiedSlots.get(i+1).getStart()) >= 0){
				occupiedSlots.get(i).setEnd(occupiedSlots.get(i+1).getEnd());
				occupiedSlots.remove(occupiedSlots.get(i+1));
				i--;
			}
		}
	}
	
	private void setFreeSlots(){
		for (int i = 0; i < occupiedSlots.size() -1; i++){
			Task temp = new Task();

			temp.setTaskType(TaskType.EVENT);
			temp.setContent(CONTENT_FREE_SLOT);
			temp.setStart(occupiedSlots.get(i).getEnd());
			temp.setEnd(occupiedSlots.get(i+1).getStart());
			
			temp.setIndex(freeSlots.size());
			freeSlots.add(temp);
		}
	}

	public ArrayList<Task> getOccupiedSlots(){
		return occupiedSlots;
	}
	
	public ArrayList<Task> getFreeSlots(){
		return freeSlots;
	}
}

@@ -1054,16 +1054,106 @@
  %3E= 0)%7B%0A
+%09%09%09%09%09if(occupiedSlots.get(i).getEnd().compareTo(occupiedSlots.get(i+1).getEnd()) %3C= 0)%7B%0A%09%09
 %09%09%09%09occu
@@ -1206,24 +1206,34 @@
 .getEnd());%0A
+%09%09%09%09%09%7D%09%0A%09%09
 %09%09%09%09occupied
@@ -1266,24 +1266,26 @@
 .get(i+1));%0A
+%09%09
 %09%09%09%09i--;%0A%09%09%09

fix bug find free slots