import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.util.*;
import java.awt.*;
//This class implements the dialog that pops up when the user clicks on 
//a day in the weekly view panel.

public class DailyView extends Frame implements MouseListener,ActionListener {

  public DailyView(EventId[] evtIdarray,int evtIdCounter,String msg,
		   ClientSocket clientsocket,int yr,int mn,int d,String user)
  {
    
    String[] attendants;
    String title;
    String buf,nextWord,status,meetingId,startHour,startMin,room,reason,owner;
    int endHour,endMin,attendantCounter,duration,boxCounter,numOfRows;
      
    username = user;
    cs = clientsocket;
    eventIdCounter = evtIdCounter;
    eventarray = evtIdarray;
    boxCounter=0;
    buf = msg;
    year = Integer.toString(yr);
    month = Integer.toString(mn);
    date = Integer.toString(d);
    
    StringTokenizer tokenizer = new StringTokenizer(buf, ",");
    nextWord = tokenizer.nextToken();//response code
   
    setSize(400,400);
    
    
    //No meetings are scheduled for this day if evtIdCounter is 0
    if( evtIdCounter == 0 )
      numOfRows = 2;
    else numOfRows = evtIdCounter+1;

    setLayout(new GridLayout(numOfRows,1));
    title = "Daily View: " + year + " " + month + " "  + date;
    Label label = new Label(title);
    Panel titlepanel = new Panel();
    titlepanel.setLayout(null);
    titlepanel.add(label);
    label.setBounds(20,20,150,50);
    Button closebutton = new Button("Close");
    titlepanel.add(closebutton);
    closebutton.setBounds(50,80,40,40);
    closebutton.addActionListener(this);
    add(titlepanel);
    
    //Create a blank dialog since no events are scheduled
    System.out.println("eventidcounter "+ evtIdCounter);
    if( evtIdCounter == 0)
      {
	System.out.println("Creating a noevent dailyview");
	TextArea infotext = new TextArea(200,200);
	add(infotext);
	infotext.addMouseListener(this);
	infotext.append("No Scheduled Events. Click to schedule.");
	
      }
    //There are events scheduled so display them all.    
    else
      for(int i=0; i< evtIdCounter;i++)
      {
	
	TextArea infotext = new TextArea(100,100);
	add(infotext);
	infotext.addMouseListener(this);

	attendants = new String[100];
	attendantCounter=0;
   
	status = tokenizer.nextToken(); //expect PENDING OR END OR CONFIRMED
	meetingId = tokenizer.nextToken();
	year = tokenizer.nextToken("/");
	month = tokenizer.nextToken("/");//  "    month  "     
	date = 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)
	  {
	    nextWord = tokenizer.nextToken(",");
	    attendants[attendantCounter]=nextWord;
	    attendantCounter++;
	  }
	endHour = Integer.parseInt(startHour);
	endMin = (Integer.parseInt(startMin)) + duration;
	while( endMin >= 60 )
	  {
	    endMin=endMin-60;
	    endHour++;
	  }
	//display the information nicely
	infotext.append(Integer.toString(boxCounter));
	infotext.append(".");
	infotext.append("\n");
	infotext.append(startHour);
	infotext.append(":");
	infotext.append(startMin);
	infotext.append(" - ");
	infotext.append(Integer.toString(endHour));
	infotext.append(":");
	infotext.append(Integer.toString(endMin));
	infotext.append(" ");
	infotext.append(status);
	infotext.append("\n");
	infotext.append(" Room: ");
	infotext.append(room);
	infotext.append("\n");
	infotext.append(" Desc: ");
	infotext.append(reason);
	infotext.append("\n");
	infotext.append(" Participants: ");
	for(int j=0;j<attendantCounter;j++)
	  {
	    infotext.append(attendants[j]);
	    infotext.append(" ");
	  }
      
	boxCounter++;
	
      }    
  }

  public void actionPerformed(ActionEvent evt)
  {

    dispose();

  }

  //Clicking on a meeting will bring up a dialog that will allow the user
  //to modify,create,destroy a meeting.
 /*These methods will be removed once an inner class is added*/
  public void mouseClicked(MouseEvent mouseevt)
  {
    MeetingDialog meeting;
    String info,nextWord,hour,min;
    int eventIdIndex;
    System.out.println("mouseclicked\n");
    TextArea txtarea;
    txtarea = (TextArea) mouseevt.getSource();
    info = txtarea.getText();
    System.out.println("inside mousecliked for dailyview");
    System.out.println("txt " + info);

    //The meeting Dialog will allow the user to modify the meeting;
    if (eventIdCounter==0)
      {
	hour = "0";
	min = "0";
	EventId evt = new EventId(username,year,month,date,hour,min);
	meeting = new MeetingDialog(this,cs,evt);
      }
    else
      {				      
	StringTokenizer tokenizer = new StringTokenizer(info,".");
	nextWord = tokenizer.nextToken();
	System.out.println("token " + nextWord);
	eventIdIndex = Integer.parseInt(nextWord);
	System.out.println("eventidnum" + eventIdIndex);	
	meeting = new MeetingDialog(this, cs, eventarray[eventIdIndex] );
      }

    meeting.pack();
    meeting.setVisible(true);
    
  }

  public void mousePressed(MouseEvent evt)
  {
    
  }
  public void mouseReleased(MouseEvent evt)
  {
    
  }
  public void mouseEntered(MouseEvent evt)
  {
    
  }
  public void mouseExited(MouseEvent evt)
  {
    
  }
  private String username;
  private String year,month,date;
  private int eventIdCounter;
  private EventId[] eventarray;
  private String user;
  private ClientSocket cs;

}
