/**
 * Has a function that converts military time to minutes.
 * Stores the minute time into an array of strings.  Returns
 * the array with each string of the format: 0120-0840,0960-1440, 
 * @author David Peck
 * 
 */
package com.example.model;
public class convertToMinutes {

   // right now days[0] = Monday
   private String[] days = new String[5];

   public convertToMinutes() {
	   for(int i = 0; i < 5; i++) {
		   days[i] = "";
	   }
   }
   
   /**
    * Converts the military time to minutes for easier comparisons
    * @author David Peck
    * @param milTime
    * @return days[]
    */
   public String[] toMinutes(String milTime) {
	   /* The format of the string milTime will look like this: 
	      *** A comma must appear at the end of the string ***
	      M08:30:00-09:30:00,T10:00:00-11:30:00,W13:00:00-14:00:00, */
	   
	   for(int i = 0; i < milTime.length(); i++) {
		   String timeInMinutes = "";
		   
		   switch(milTime.charAt(i))
		   {
		   // only compute the values if currently on a letter, which
		   // means that a new time entry for a day has been found
		   case 'M':
		   case 'T':
		   case 'W':
		   case 'R':
		   case 'F':
			   
			   String startHour, endHour, startMinute, endMinute;
			   Integer sh, eh, sm, em, st, et;
			   
			   // first grab the hour and minute values from the string
			   startHour = milTime.substring(i+1, i+3);
			   endHour = milTime.substring(i+10, i+12);
			   startMinute = milTime.substring(i+4, i+6);
			   endMinute = milTime.substring(i+13, i+15);
			   
			   // then add them together to get the total minute time
			   sh = Integer.valueOf(startHour);
			   eh = Integer.valueOf(endHour);
			   sm = Integer.valueOf(startMinute);
			   em = Integer.valueOf(endMinute);
	
			   int startTotal = (sh.intValue() * 60) + sm.intValue();
			   int endTotal = (eh.intValue() * 60) + em.intValue();
			   st = Integer.valueOf(startTotal);
			   et = Integer.valueOf(endTotal);
			   
			   String startT = st.toString();
			   String endT = et.toString();

			   // if the value is less than 1000, we need to add
			   // a zero to make it 4 chars long for consistency
			   // it won't be less than 480 right now, since 
			   // 8:00 am is the earliest time we're dealing with
			   // i added in the condition to deal with that case
			   // anyway, just to be on the safe side
			   if(startTotal < 100)
				   startT = "00".concat(startT);
			   else if(startTotal < 1000)
				   startT = "0".concat(startT);
			   if(endTotal < 100)
				   endT = "00".concat(endT);
			   else if(endTotal < 1000)
				   endT = "0".concat(endT);
			   
			   // now convert the int back into a string of the proper format
			   timeInMinutes = startT + "-" + endT + ",";

		   default:
			   break;
		   }

		   // now store the minute values into the proper day
		   switch(milTime.charAt(i))
		   {
		   case 'M':
			   // store the times in Monday
			   days[0] = days[0].concat(timeInMinutes);
			   break;
		   case 'T':
			   // store the times in Tuesday
			   days[1] = days[1].concat(timeInMinutes);
			   break;
		   case 'W':
			   // store the times in Wednesday
			   days[2] = days[2].concat(timeInMinutes);
			   break;
		   case 'R':
			   // store the times in Thursday
			   days[3] = days[3].concat(timeInMinutes);
			   break;
		   case 'F':
			   // store the times in Friday
			   days[4] = days[4].concat(timeInMinutes);
			   break;
		   default:
			   break;
		   }
	   }
	   return days;
   }
}


