/*This class is responsible for displaying the information in the week view*/


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.util.*;



public class DayTextArea extends TextArea implements MouseListener {

  //This constructor is called when the calendar is first displayed//
  //It uses the current date as a default start date//
  public DayTextArea(String name,WeekPanel panel,ClientSocket cs,String user)
  {

    super(name,50,100,TextArea.SCROLLBARS_VERTICAL_ONLY);
    String sEndMin,sEndHour;
    int endMin,endHour;
   
    username = user;
    clientsocket = cs;
    wkpanel = panel;
    TodaysDate todayobject = new TodaysDate();
    
    
    day = (todayobject.dayToInt(name));
    month = panel.getMonth();
    year = panel.getYear();
   
    this.addMouseListener(this);
    this.setBackground(Color.white);
    this.setEditable(false);
  
    TodaysDate today = new TodaysDate();
    date = (today.getCurrentDate()) + (day - (today.getCurrentDay())) +1 ;    
    if (date > today.getnDaysInMonth())
      {
	date = date - today.getnDaysInMonth();
	month++;
	if(month>11)
	  {
	    month=0;
	    year++;
	  }
	    
      }
    append(" ");
    append(Integer.toString(date));
    
    if (day==0)
      wkpanel.setStartDate(date);
    
    

     System.out.println("day " + day + "year " + year + "\n" + "month " + month + "\n" + "date " + date);

    addDescription();
  }

  //This constructor is used when the next or previous buttons are pressed.//
  //The start date is passed to this constructor//
  public DayTextArea(String dayName,WeekPanel panel,ClientSocket cs,String user,int startDate)
  {
    super(dayName,50,100,TextArea.SCROLLBARS_VERTICAL_ONLY);

    username = user;
    clientsocket = cs;
    this.addMouseListener(this);
    this.setBackground(Color.white);
    this.setEditable(false);
    
    month = panel.getMonth();
    year = panel.getYear();
    TodaysDate today = new TodaysDate();
    date = startDate + today.dayToInt(dayName);
    if (date > panel.getnDaysInMonth() )
      {
	date = date - panel.getnDaysInMonth();
	month++;
	if(month > 11)
	  {
	    year++;
	    month=1;
	  }

      }
    append(" ");
    append(Integer.toString(date));
    
   

    System.out.println("day " + day + "year " + year + "\n" + "month " + month + "\n" + "date " + date);
    
    addDescription();
  }



  //This method writes the description of each day in the week view//
  private void addDescription()
  {
   
    String room,reason,status,startHour,startMin,owner;
    int duration,endHour,endMin;
  
    String command,meetingId,buf;
    String sYear,sMonth,sDate,nextWord;
    evtIdarray = new EventId[24];
    
    
    System.out.println("inside addDescription");   
    System.out.println("year/month/date as ints " + year + month + date);
    
    sYear = Integer.toString(year);
    sMonth = Integer.toString(month);
    sDate = Integer.toString(date);
    command = "GETCALENDAR,D," + sYear + "/" + sMonth + "/" + sDate + "/09/00";  
    System.out.println("command " + command );
    
    evtIdCounter=0;

    try
      {
	clientsocket.Write(command);
	buf = clientsocket.Read();
	dayDescription = buf;
	System.out.println("response" + buf );		
      }
    catch(IOException io)
      {
	System.out.println("IO error");
	return;
      }
	
    //I will soon format the appointments nicely
    
    
    StringTokenizer tokenizer = new StringTokenizer(buf, ",");

    nextWord = tokenizer.nextToken();
    if ( (nextWord.equals("102")) == false )
      return;

    status = tokenizer.nextToken(); //expect PENDING OR END OR CONFIRMED
   
    while((status.equals("END")) == false )
      {
	meetingId = tokenizer.nextToken();
	nextWord = tokenizer.nextToken("/");//ignore year because we already know it
	nextWord = tokenizer.nextToken("/");//  "    month  "     
	nextWord = tokenizer.nextToken("/");//  "    date   "
	startHour = tokenizer.nextToken("/");
	startMin = tokenizer.nextToken("/,");
	nextWord = tokenizer.nextToken(",");
	duration = Integer.parseInt(nextWord);
	owner = tokenizer.nextToken(",");
	room = tokenizer.nextToken(",");
	reason = tokenizer.nextToken(",");
	nextWord = tokenizer.nextToken(",");
	while(nextWord.equals("ENDMEETING") == false)//ignore users
	  {
	    nextWord = tokenizer.nextToken(",");
	   
	  }
	
	
	//Note:eventId takes the user logged in and not the owner of the meeting
	evtIdarray[evtIdCounter] =  new EventId(username,sYear,sMonth,sDate,startHour,startMin);
	evtIdCounter++;
	
	endHour = Integer.parseInt(startHour);
	endMin = (Integer.parseInt(startMin)) + duration;
	while( endMin >= 60 )
	  {
	    endMin=endMin-60;
	    endHour++;
	  }
	append("\n");
	append(startHour);
	append(":");
	append(startMin);
	append(" - ");
	append(Integer.toString(endHour));
	append(":");
	append(Integer.toString(endMin));
	append(" ");
	append(status);
	append("\n");
	append(" Room: ");
	append(room);
	append("\n");
	append(" Desc: ");
	append(reason);

	
	status = tokenizer.nextToken(","); //expect END or PENDING
	
      }
    
	

  }



  /*These methods will be removed once an inner class is added*/
  public void mouseClicked(MouseEvent evt)
  {
    System.out.println("mouseclicked\n");

    DailyView dayframe = new DailyView(evtIdarray,evtIdCounter,
				       dayDescription,clientsocket,
				       year,month,date,username);
    dayframe.setVisible(true);
    dayframe.show();

  }
  public void mousePressed(MouseEvent evt)
  {
    
  }
  public void mouseReleased(MouseEvent evt)
  {
    
  }
  public void mouseEntered(MouseEvent evt)
  {
    
  }
  public void mouseExited(MouseEvent evt)
  {
    
  }

  private String username;
  private ClientSocket clientsocket;
  private int day;
  private int date;
  private int month;
  private int year;
  private WeekPanel wkpanel;
 
  private int evtIdCounter;
  private EventId[] evtIdarray;
  private String dayDescription;
 

}












