package com.example.model;
import java.util.*;
import java.lang.*;
import java.sql.*;

/** This class will process a String array to find free schedule times.
 *  Each index of the array represents a day. (ex: array[0] would be Monday
 *  and array[4] would br friday) The times entered in each day are the busy times
 *  of all users. The Scheduler class takes these times and returns a new String[]
 *  consisting of a UNION of the User's free times.
 *  @author Matt Arbaugh
 */  
public class Scheduler {

	/*There are 1440 minutes in a day
	 so create an integer array of size 1440
	 */
	private String[] freeTimes = new String[1440];

	private String[] resultingFreeTimes = new String[5];

	/** This is the default constructor. An array of size 1440 is created with all
	 *  of the elements set to '0'. These elements represent the minutes in one day.
	 *  @author Matt Arbaugh	 
	 *  @param None
	 */    
	public Scheduler() {
		for (int a = 0; a < 1440; a++) {
			freeTimes[a] = "0";
		}
		
		for(int i = 0; i < 5; i++) {
		    resultingFreeTimes[i] = "";
		}
	}

  /** getFreeTimes will take a string[] of busy times and constraints (if entered)
   *  and return a new string[] consisting of the free times inside the contraints.
   *  @author Matt Arbaugh
   *  @param String[] classTimes, String startTime, String finishTime
   *  @return String[]
   */              
	public String[] getFreeTimes(String[] classTimes, String startTime, String finishTime) {

		/*Step through each day of the classTimes array,
		 classTimes[0], classTimes[1], classTimes[2],
		 classTimes[3], classTimes[4] and take the times
		 that are not free and set them equal to 1 in the freeTimes array */

		String classStart = "";
		String classEnd = "";
		int startConstraint = 0;
		int finishConstraint = 0;

		convertToHours convertHours = new convertToHours();

		/* If constaints were entered in, convert them to minutes */
		if(startTime != "" && finishTime != ""){
			    
		    /* Convert the constraints to minutes */
		    String st[] = startTime.split(":");
		    startConstraint = (Integer.valueOf(st[0]) * 60) + Integer.valueOf(st[1]);
		    
		    st = finishTime.split(":");	
		    finishConstraint = (Integer.valueOf(st[0]) * 60) + Integer.valueOf(st[1]);   
   	}

		for (int i = 0; i < 5; i++) {

			int j = 0;


                        /* Reset the time array */
			for (int a = 0; a < 1440; a++) {
			    freeTimes[a] = "0";
			}

			/* Set the constraint times to busy, if they were entered */
			if(startConstraint != 0 && finishConstraint != 0 && startConstraint < finishConstraint){
			    
			    for(int k = 0; k < startConstraint-1; k++){
				freeTimes[k] = "1";
			    }
			    
			    for(int k = finishConstraint-1; k < 1440; k++){
				freeTimes[k] = "1";
			    }
			}

			while (j < classTimes[i].length()) {

				/*parse out the start and end time of each busy time*/
				classStart = classTimes[i].substring(j, j + 4);
				classEnd = classTimes[i].substring(j + 5, j + 9);
				
				int end = Integer.valueOf(classEnd).intValue();
				int start = Integer.valueOf(classStart).intValue();
			
				/*set all the elements inbetween the start and end class times equal to 1 (not free) */
				for (int c = start-1; c < end-1; c++) {
					freeTimes[c] = "1";
				}

				/*increment j to get the next busy time*/
				j += 10;
			}

			/*Now take the freeTimes array and find the avaiable times (elements that are still 1)*/

			int walker = 0;
			Integer freeStart = 0;
			Integer freeEnd = 0;
			String timeStartString = "";
			String timeEndString = "";
			String holder;

			while (walker < 1440) {
             
             /* Free Times Found */
            if(freeTimes[walker] == "0"){
        
               /* Store the start minute in proper format */
               if(walker == 0)
                  freeStart = walker;
               else
                  freeStart = walker+1;
               
               if(freeStart < 10)
                  timeStartString = "000" + freeStart.toString();
               
               else if(freeStart < 100)
                  timeStartString = "00" + freeStart.toString();
                  
               else if(freeStart < 1000)
                  timeStartString = "0" + freeStart.toString();
               
               else
                  timeStartString = freeStart.toString();
 
                 
               /* Traverse until the free times are done or the array is exhausted */
               while(walker < 1440 && freeTimes[walker] == "0"){
                  walker++;
                  ///////
               }
               
               if(walker == 1440)
                  freeEnd = walker;
               
               else 
                  freeEnd = walker+1;
                  
               /* By this point we know we are done the free time interval */
               if(freeEnd < 10)
                  timeEndString = "000" + freeEnd.toString();
               
               else if(freeEnd < 100)
                  timeEndString = "00" + freeEnd.toString();
                  
               else if(freeEnd < 1000)
                  timeEndString = "0" + freeEnd.toString();
               
               else
                  timeEndString = freeEnd.toString();               
                
					/*convert the times back to hours*/

					
					timeStartString = convertHours.toHours(freeStart.toString());
					timeEndString = convertHours.toHours(freeEnd.toString());

					holder = timeStartString + "-" + timeEndString + ",";
					/*place the time created into the resultingFreeTimes array on the correct day*/
					//resultingFreeTimes[i] = resultingFreeTimes[i].concat(holder);
					resultingFreeTimes[i] += holder;
				}
			
			walker++;
			}
		}

		return resultingFreeTimes;
	}
}
