package jsbook.chapter10.collab;

import java.awt.*;
import java.awt.event.*;

import net.jini.space.JavaSpace;

public class Login extends Frame 
    implements ActionListener {
        
    private Messenger messenger;
    private JavaSpace space;
         
    // variables for user interface components
    private Label usernameLabel;
    private TextField usernameTextField;
    private Label passwordLabel;
    private TextField passwordTextField;
    private Button loginButton;
    private Label statusLabel;
        
    public Login(Messenger messenger) { 
        this.messenger = messenger;
        this.space = messenger.space();
        
        setLayout(null);
        setSize(350,175);
        
        // add label and text entry field for the username:
        usernameLabel = new Label("Username:",Label.RIGHT);
        usernameLabel.setBounds(12,30,100,24);
        add(usernameLabel);

        usernameTextField = new TextField();
        usernameTextField.setBounds(130,30,100,24);
        usernameTextField.setText("");
        add(usernameTextField);
        
        // add label and text entry field for the password:
        passwordLabel = new Label("Password:",Label.RIGHT);
        passwordLabel.setBounds(12,60,100,24);
        add(passwordLabel);

        passwordTextField = new TextField();
        passwordTextField.setBounds(130,60,100,24);
        passwordTextField.setText("");
        passwordTextField.setEchoChar('*');
        add(passwordTextField);

        // add a login button:
        loginButton = new Button();
        loginButton.setLabel("Login");
        loginButton.setBounds(250,60,60,24);
        add(loginButton); 
        
        // add label for the status:
        statusLabel = new Label("",Label.LEFT);
        statusLabel.setBounds(12,150,250,24);
        add(statusLabel);
        
        setTitle("Chat Login");
        setStatus("Enter your username and password.");
        pack();
        setVisible(true);
        
        loginButton.addActionListener(this);
        usernameTextField.addActionListener(this);
        passwordTextField.addActionListener(this);
        
        // allows the window to be closed:
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });  
    }
        
    // "Enter" pressed in password field, or login button clicked:
    public void actionPerformed(ActionEvent event) {
        String username = usernameTextField.getText();
        String password = passwordTextField.getText();
        
        if (username.equals("") || password.equals("")) {
            setStatus("Enter both username & password.");
            return;
        }
        
        Account userAccount = 
            new Account(messenger, username, password);
        try {
            userAccount.validate();
        } catch (InvalidPasswordException e) {
            setStatus("Invalid password.");
            return;
        } catch (UnknownUserException e1) {
            try {
                userAccount.create();
                messenger.showStatus("Created new account for " + 
                    username + "."); 
            } catch (Exception e2) {
                messenger.showStatus(
                    "Could not create account for " + 
                    username + ".");
                return;
            }
        }
        
        Session userSession = new Session(space, username);
        try {
            userSession.start(); 
        } catch (Exception e) {
            messenger.showStatus("Could not create session for " + 
                username + ".");
            return;
        }                
        
        dispose();
        messenger.loginCallback(username, password); 
    }
    
    public void setStatus(String msg) {
        statusLabel.setText(msg);
    }
}
