package com.calendar;

import java.util.*;

/**
    Returns the times that ALL users are available, based on the times that each individual user is available.
    @author Brian Johnson
    @version 1.1 (March 20, 2006)
*/
public class Controller
{
    private ArrayList<String> theList;
    private final int INCR = 16; //  was 17

    /**
        @param theStuff an ArrayList of type String that contains a String for each user, representing the times each user is available
    */
    public Controller(ArrayList<String> theStuff)
    {
        theList = theStuff;
    }

    /**
        @return A String that represents the times ALL USERS are available.
    */
    public String funnelData()
    {
        //set result to a template
        String result = "M:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;T:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;W:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;T:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;F:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;";
        String result17 = "M:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;T:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;W:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;T:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;F:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;";

        //loop for 5 days
        for(int day=0; day<5; day++)
        {
           //loop for the # of time increments
           for(int time=1; time<=INCR; time++)
           {
              int orResult = 0;    //anything || 0 is itself

              //loop for each person
              for(int person=0; person<theList.size(); person++)
              {
                 //get the string for the given person
                 String temp = theList.get(person);

                 //extract person's availability for the given time increment
                 int tempOr = (int)temp.charAt((day*(INCR*2+2))+time*2) - (int)'0';

                 //logical OR with the previous persons' availability
                 orResult = orResult | tempOr;
              }

              //update the result string for the given time increment
              char[] theStr = result.toCharArray();
              theStr[(day*(INCR*2+2))+time*2] = (char)(orResult + (int)'0');
              result = new String(theStr);
           }
        }
        return result;
    }
}
