package com.calendar;
import java.util.*;

/**
    Converts HTML form data (9:00-11:30,3:30-5:00) to the proper format to populate the calendar (M:1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1).
    @author Jim Meek
    @version 1.8 (March 27, 2006)
*/
public class Parser {

/**
    @deprecated
*/
  public static void main(String args[]) {
	 // Constructor  -- not used
//   String input = "9:00-11:30,3:30-5:00";
//   Parser P = new Parser();
//   System.out.println(P.doParse(input));
  }

/* Parser will take a string of the form:
"9:00-11:30,3:30-5:00";  and return one of the form <br>
"1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,"   -- 16 half hour time blocks for 8 hour day<br>
  this can be displayed by a jsp such as that at:<br>
<a href="http://porsche.cis.udel.edu:8123/calendar> href="http://porsche.cis.udel.edu:8123/calendar</a><br>

Procedure:to create an array holding the integer values produced,<br>
split first on ';' in username:sometimestring to split out username<br>
then split on ',' to get 9:00-2:00, 3:00-5:00 split into two meetings<br>
then split on ':' to get start and end times<br>
parse first on ';' in "9:00-11:30,3:30-5:00"<br>
NEEDED: handle case of no ':' ,eg 4-5:30<br>
for each start and stoptime pair convert to 24 hr time by assuming no meetings at UD before 7<br>
<br>then multiply hour *2, add 1 if >= 30 min, subtract the start time of the display<br>
assemble the parsed string from the integer values and return to the caller<br>
*/

/**
    @param myinput a String containing data from the HTML form, in the format of "9:00-11:30,3:30-5:00"
    @return A String resulting from converting the input into the following format: "M:1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1"
*/
  public static String doParse (String myinput) {

  //initialize
  //MAGIC NUMBERS
    int NBLOCKS = 16; // 8 hrs in half hour blocks
    int IFIRST= 18; // define first block of display--9:00 am
    int timeArr[] = new int[NBLOCKS];
    StringBuffer SB= new StringBuffer();
    String noMeetings = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"; //N=16


  //START PARSING
    for (int k=0; k<NBLOCKS; k++) {timeArr[k]=0;}
     //PARSE INTO MEETINGS
       StringTokenizer STmeet = new StringTokenizer(myinput, ",");
       String meetings[] = new String[16];
       int meetingNumber= 0;
       while(STmeet.hasMoreTokens()) {
          meetings[meetingNumber] = STmeet.nextToken();
          meetingNumber++;
       }
       if (meetingNumber == 0) {
           return noMeetings;
       }
       for (int i=0; i<meetingNumber; i++) {

       //PARSE INTO START AND END TIMES
          StringTokenizer STends = new StringTokenizer(meetings[i], "-");
          String start = STends.nextToken();
          String end = STends.nextToken();

        //PARSE START INTO HOUR, MIN, adjust for am pm
          StringTokenizer ST = new StringTokenizer(start,":");
          String hour = ST.nextToken();
          String minute = ST.nextToken();
          int SHour= java.lang.Integer.parseInt(hour);
          if (SHour< 7) {  // calculate am, pm: no meetings before 7am
              SHour= SHour+12;
          }
          int SMin= java.lang.Integer.parseInt(minute);
          if (SMin< 30)
             {SMin= 0;}
           else
             {SMin= 1; }
      // convert hour, min into half hour blocks from first meeting block
         int iStart = (2* SHour) + SMin - IFIRST;

       //REPEAT FOR END
         ST = new StringTokenizer(end,":");
         hour = ST.nextToken();
         minute = ST.nextToken();
         int EHour= java.lang.Integer.parseInt(hour);
         if (EHour< 7) {EHour= EHour+12;}
         int EMin= java.lang.Integer.parseInt(minute);
         if (EMin< 30)
             {EMin= 0;}
           else
              {EMin=1;}
          int iEnd= (2 * EHour) + EMin -IFIRST;
// System.out.println(iStart + "---" + iEnd);

      //make a time array for busy or free
        for (int j= iStart; j< iEnd; j++) {
            timeArr[j]=1;
            }
        }
      //convert the time array to a string
        for (int m= 0; m<16; m++) {
            SB.append(timeArr[m]);
            if (m < 15) {
              SB.append(",");
            }
       }
     return(SB.toString());
 }
}


