package A10Park;

public class ParserTimes
{
	

  public String formatFreeTimes(String busyTimes)		//do it for only a single day
  
  /* 
  array of 16 ints
  from 9-4
  */
  {
  	 
    int min = -1;
    int max = -1;
    int m = 0;
    String result="";
    int hour=0;

/*
if even # then
i-i/2 +9
*/
      for(int h = 0; h < 16; h++)
      {
      	if(h%2==0)
      	{
      		m=0;
      		hour = h/2 + 9;
      	}
      	else
      	{
      		m = 30;
      		hour = (h-1)/2 + 9;
      	}
          if(busyTimes.charAt(h) == '0' && min == -1)
            min = (hour) * 100 + m;
          if(busyTimes.charAt(h) == '0' && max != -1)
          {
          	min = (hour) * 100 + m;
          	max = -1;
          }
          if(busyTimes.charAt(h) == '1' && min!=-1)
          {
          	max = (hour) * 100 + m;
          	result += timeConvert(min, max);
          	min = -1;
          }
          if(h==15 && min != -1)
          	result += timeConvert(min, 1700);            
        }
    return result;            
    }
    
  

  /** Takes a minimum and maximum time (in military time) and returns a String
      in the format 11:00AM-2:30PM, where min=1100 and max=1430
      @param min the minimum time
      @param max the maximum time
      @return formatted String of the form 11:00AM-2:30PM
  */
  public static String timeConvert(int min, int max)
  {
  	String amPM;
  	String result = "";
  	
  	if(min >= 1300)
  	{
  		min = min - 1200;
  		amPM = "PM";
  	}
  	else if(min >= 1200 && min < 1300)
  		amPM = "PM";
  	else
  		amPM = "AM";
  	
  	int m = min % 100;
  	int h = min / 100;
  	
  	if(m == 0)
  		result += (h + ":00" + amPM + "-");
  	else
  		result += (h + ":" + m + amPM + "-");

	 if(max >= 1300)
	 {
	 	max = max - 1200;
	 	amPM = "PM";
	}
  		

	 

	else if(max >= 1200 && max < 1300)
		amPM = "PM";
	else
		amPM = "AM";
      h = max / 100;
	 m = max % 100;		
	if(m == 0)
		result += (h + ":00" + amPM + ", ");
	else
		result += (h + ":" + m + amPM + ", ");
    return result;
  }
	
}
