import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.Array;
 
/**
 * CSC 442F:    Software Engineering
 * Software:    Calendar Client
 * Component:   AttendeesPanel class
 * Description: The AttendeesPanel class produces an attendees panel from a 
 *              calendar server. This panel lists all internal users registered
 *              on the calendar server. The admin user and hidden users are not
 *              shown. 
 *
 * @author David Tam
 * @version 1.0
 */
public class AttendeesPanel extends Panel implements ActionListener
{ 
  // Classes
  protected ClientSocket socket;
  protected String username;

  // AWT classes
  protected GridBagConstraints constraints;
  protected Label attendees;
  public List attendeesList;
  protected Button clear;
  
 /** 
  * Constructor
  * ===========
  * @param socket_in     The socket which is used to talk to the server.
  * @param username_in   The name of the current user. This parameter is used
  *                      to exclude this person as well as admin from the 
  *                      userlist.
  */
  public AttendeesPanel(ClientSocket socket_in, String username_in)
  {
    // Variable initialization
    socket = socket_in;
    username = username_in;
    
    // AWT class instantiation
    attendees = new Label("Attendees");
    attendeesList = new List(5);
    attendeesList.setMultipleMode(true);
    clear = new Button("Clear");

    // Button event reciever
    clear.addActionListener(this);

    // Constraints instantiation
    constraints = new GridBagConstraints();   

    // Panel layout
    setLayout(new GridBagLayout());
    constraints.gridx=0;
    constraints.gridy=0;
    constraints.anchor=constraints.WEST;
    add(attendees, constraints);
    constraints.gridy=1;
    add(attendeesList, constraints);
    constraints.gridy=2;
    constraints.insets=new Insets(2,0,0,0);
    add(clear, constraints);    
  }
  

 /**
  * Method to fetch user list from server
  * =====================================
  */
  public void fetchData()
  {
    GetUsers getUsers = new GetUsers(socket, attendeesList, username);
  }
  
  
 /**
  * Method to send user list to server
  * ==================================
  * 
  * @return Returns a String in the form of the protocol
  */
  public String buildResponse()
  {
    // Local variables
    String [] attendeesList_out = attendeesList.getSelectedItems();
    StringBuffer preresult = new StringBuffer();
    String result;

    // If there is at least one attendee selected
    if (Array.getLength(attendeesList_out) != 0)
    {
      // Concatenate <numberOfUsers>
      preresult.append(Array.getLength(attendeesList_out));
      preresult.append(",");

      // Concatenate attendees
      for (int i = 0 ; i < Array.getLength(attendeesList_out) ; i++)
      {
        preresult.append(attendeesList_out[i]);
        preresult.append(",");
      }
      
      // Convert to final result and remove trailing comma at end of list
      result = preresult.toString();
      result = result.substring(0,result.length()-1);
    }
    // If no attendees selected, add shadow user
    else
    {
      // Concatenate <numberOfUsers>
      preresult.append("1,");
      
      // Concatenate attendees
      preresult.append(username);
      preresult.append("_personal");
      
      // Convert to final result
      result = preresult.toString();
    }
        
    // Return string    
    return result;
  }

  
 /**
  * Method to handle action events
  * ==============================
  */
  public void actionPerformed(ActionEvent event)
  {
  for (int i = 0 ; i< attendeesList.getItemCount() ; i++)
    attendeesList.deselect(i);
  }
  
  
 /**
  * Method to toggle input fields between editable and noneditable
  * ==============================================================
  *
  * @param value   A boolean value that set field to editable or noneditable
  */
  public void setEditable(boolean value)
  {    
//  attendeesList.setEnabled(value);
    clear.setEnabled(value);
  }

}
