import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.RemoteException;

import javax.swing.*;

import java.awt.Dimension;

/**
 * Java Chat - A simple chatroom application - OptionsFrame Class.
 *
 * This is the OptionsFrame class. It provides a user with additional
 * options, such as changing their username. 
 *
 * @author Sean Handley
 * @version November, 2006
 */
public class OptionsFrame extends JFrame implements ActionListener {

    private static final long serialVersionUID = -2120820012411543758L;
    private ChatClient cc;
    private final String windowTitle = "Chat Options";
    private JLabel lblUsername;
    private JTextField txtUsername;
    private JButton btnOk;

    /**
     * Constructor for the options frame. Takes a chat client
     * reference and adds it to a local variable to allow methods
     * to be called by GUI events.
     * 
     * @param cc
     */
    public OptionsFrame(ChatClient cc)
    {
        super();
        //add listener to call exit procedure on close
        addWindowListener(
                new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        exitProcedure();
                    }
                }
            );
        this.cc = cc;
    }
    /**
     * Build the GUI using Swing components.
     */
    public void buildGUI()
    {
        //Start building the GUI
        Container contentPane = getContentPane();
        contentPane.setLayout(new FlowLayout());

        lblUsername = new JLabel("Username:");
        txtUsername = new JTextField(cc.getUserName());
        txtUsername.setPreferredSize(new Dimension(200,20));
        btnOk = new JButton("Ok");
        btnOk.addActionListener(this);

        contentPane.add(lblUsername);
        contentPane.add(txtUsername);
        contentPane.add(btnOk);

        getRootPane().setDefaultButton(btnOk);
        pack();
        setLocationRelativeTo(null);
        setTitle(windowTitle);
        setResizable(false);
        setVisible(true);
        txtUsername.requestFocus();
    }
    /*
     * Disposes the window.
     */
    private void exitProcedure()
    {
        dispose();
    }
    /**
     * Respond to component events. Implements the ActionListener
     * interface.
     * 
     * @param event
     */
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == btnOk)
        {
            if(txtUsername.getText().length() > 0) //make sure the name is not null
            {
                //if the username hasn't changed...
                if(cc.getUserName().equalsIgnoreCase(txtUsername.getText()))
                {
                    exitProcedure();
                }
                else
                {
                    if(cc.isLoggedIn())
                    {
                        try
                        {
                            if(!cc.checkUser(txtUsername.getText())) //if the name isn't taken...
                            {
                                cc.changeName(txtUsername.getText());
                                exitProcedure();
                            }
                            else
                            {
                                JOptionPane.showMessageDialog(this,"Username is in use. Please select another.");
                            }
                        }
                        catch(RemoteException e)
                        {
                            System.err.println(e.toString());
                        }
                    }
                    else
                    {
                        try
                        {
                            if(!cc.checkUser(txtUsername.getText()))
                            {
                                cc.setUserName(txtUsername.getText());
                                exitProcedure();
                            }
                            else
                            {
                                JOptionPane.showMessageDialog(this,"Username is in use. Please select another.");
                            }
                        }
                        catch(RemoteException e)
                        {
                            System.err.println(e.toString());
                        }
                    }
                }
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Please enter a username.");
            }
        }
    }
}