//   Course Number: COSC 650 Data Comm/Networks
//    Program Name: RoomSelectPanel.java
//  Project Number: 2
//         Authors: Brian Hoffman
//                  Mike McKinney
//                  Javier Carrasco
//     Description:  The RoomSelectPanel comprises the major portion of the GUI 
//                   available to the user after they login. It provides a JList
//                   of the available chatrooms down the left side and create, 
//                   join, and exit buttons on the right. The major functionality 
//                   lies in the actions of the buttons. The create button pops  
//                   up a dialog asking for the room's name and then askes the 
//                   MultiChatClient to send a createRoom message to the server.
//                   the join button results in the MultiChatClient being asked 
//                   to send a joinRoom message. The server's reply to both of 
//                   these messages is handled entirely by the MultiChatClient.
//                   Finally, the end button informs the MultiChatClient to close
//                   the connection and redisplay the login panel.

// Java Core Packages
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.Vector;

// Jave Extension Packages 
import javax.swing.*;
import javax.swing.border.*;

public class RoomSelectPanel extends GriddedPanel
{   
    // private instance variables
    MultiChatClient client;
    DefaultListModel rooms;
    
    // RoomSelectPanel GUI Components
    private Container container; 
    private GriddedPanel roomSelectPanel;
    private JPanel roomMenuPanel;
    private JPanel optionsMenuPanel;
    private JList roomList;
    private JButton createButton;
    private JButton joinButton;
    private JButton exitButton;
    
    /**
     * ------------------------------------------------------------------------
     * constructor - sets up the GUI
     * ------------------------------------------------------------------------
     */
    public RoomSelectPanel(MultiChatClient inClient, Vector inRooms)
    {
        super();
        client = inClient;
        
        // create roomMenuPanel for the roomSelectPanel 
        roomMenuPanel = new JPanel();
        roomMenuPanel.setLayout(new BorderLayout());
        roomMenuPanel.setBorder(BorderFactory.createTitledBorder(
                      BorderFactory.createEtchedBorder(), "Chatrooms",
                      TitledBorder.CENTER, TitledBorder.TOP ));
        rooms = new DefaultListModel();
        for (int i=0; i<inRooms.size(); i++)
        {
            rooms.addElement(inRooms.get(i));
        }     
        roomList = new JList(rooms);
        roomList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        roomMenuPanel.add(new JScrollPane(roomList), BorderLayout.CENTER);
        
        // create optionsMenuPanel for the roomSelectPanel
        optionsMenuPanel = new JPanel();
        optionsMenuPanel.setLayout(new GridLayout(3,1));
        optionsMenuPanel.setBorder(BorderFactory.createTitledBorder(
                      BorderFactory.createEtchedBorder(), "Options",
                      TitledBorder.CENTER, TitledBorder.TOP ));
        createButton = new JButton("Create");
        createButton.addActionListener( new createButtonHandler());
        joinButton = new JButton("Join");
        joinButton.addActionListener( new joinButtonHandler());
        exitButton = new JButton("Exit");
        exitButton.addActionListener(new exitButtonHandler());
        optionsMenuPanel.add(createButton);
        optionsMenuPanel.add(joinButton);
        optionsMenuPanel.add(exitButton);
     
        // add the subpanels to the main room select panel
        addFilledComponent(roomMenuPanel, 1, 1, 2, 3, GridBagConstraints.BOTH);
        addAnchoredComponent(optionsMenuPanel, 1, 3, 1, 3, GridBagConstraints.NORTH);   
    }

    /**
     * ------------------------------------------------------------------------
     * createButtonHandler
     * ------------------------------------------------------------------------
     */
    private class createButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String roomName;
            roomName = JOptionPane.showInputDialog("Enter the name of  the room to create");
            if (roomName != null)
            {
                ChatMessage msg = new ChatMessage("createRoom", client.getUserName(),
                                                   roomName, "");
                client.sendMessage(msg);
            }
        }
    } 
    
    /**
     * ------------------------------------------------------------------------
     * joinButtonHandler
     * ------------------------------------------------------------------------
     */   
    private class joinButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            ChatMessage msg = new ChatMessage("joinRoom", client.getUserName(),
                                      (String) roomList.getSelectedValue(), "");
            client.sendMessage(msg);
        }
    }
    
    /**
     * ------------------------------------------------------------------------
     * exitButtonHandler
     * ------------------------------------------------------------------------
     */
    private class exitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            client.setNormalExit();
        }
    }
    
    /**
     * ------------------------------------------------------------------------
     * addRoom - adds the name of the provided chatroom to the JList along the 
     *           left side
     * ------------------------------------------------------------------------
     */
    public void addRoom(String roomName)
    {
        rooms.addElement(roomName);
    }
    
    /**
     * ------------------------------------------------------------------------
     * removeRoom - removes the provided chatroom from the JList 
     * ------------------------------------------------------------------------
     */
    public void removeRoom(String roomName)
    {
        int pos;
        String temp;
        
        for(pos=0; pos<rooms.getSize(); pos++)
        {
            temp = (String) rooms.getElementAt(pos);
            if (temp.equals(roomName)) break;
        }
        if(pos == rooms.getSize()) return;
        rooms.removeElementAt(pos);
    }
}