// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   passwordDialog.java

package boggle.client;

import boggle.codec;
import boggle.server.boggleServerInterface;
import boggle.settings;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.rmi.Naming;
import java.util.EventObject;

// Referenced classes of package boggle.client:
//            playerViewer

public class passwordDialog extends Dialog
{
    class SymAction
        implements ActionListener
    {

        public void actionPerformed(ActionEvent actionevent)
        {
            Object obj = actionevent.getSource();
            if(obj == userTextField)
            {
                passTextField.requestFocus();
                return;
            }
            if(obj == passTextField)
            {
                loginButton_MouseClick();
                return;
            }
            if(obj == loginButton)
            {
                loginButton_MouseClick();
                return;
            }
            if(obj == signupButton)
            {
                signupButton_MouseClick();
                return;
            }
            if(obj == quitButton)
                quitButton_MouseClick();
        }

        SymAction()
        {
        }
    }


    public passwordDialog(playerViewer playerviewer, int i)
    {
        super(playerviewer, true);
        fComponentsAdjusted = false;
        holder = playerviewer;
        language = i;
        setLayout(null);
        setVisible(false);
        setSize(getInsets().left + getInsets().right + 215, getInsets().top + getInsets().bottom + 176);
        nameLabel = new Label("Name:");
        nameLabel.setBounds(getInsets().left + 8, getInsets().top + 93, 75, 15);
        add(nameLabel);
        passwordLabel = new Label("Password:");
        passwordLabel.setBounds(getInsets().left + 8, getInsets().top + 121, 75, 15);
        add(passwordLabel);
        userTextField = new TextField(1);
        userTextField.setBounds(getInsets().left + 91, getInsets().top + 89, 100, 22);
        add(userTextField);
        passTextField = new TextField(1);
        passTextField.setEchoChar('*');
        passTextField.setBounds(getInsets().left + 91, getInsets().top + 118, 100, 22);
        add(passTextField);
        loginButton = new Button();
        loginButton.setActionCommand("OK");
        loginButton.setLabel("Login");
        loginButton.setBounds(getInsets().left + 137, getInsets().top + 147, 54, 20);
        add(loginButton);
        signupButton = new Button();
        signupButton.setActionCommand("OK");
        signupButton.setLabel("Sign Up...");
        signupButton.setBounds(getInsets().left + 120, getInsets().top + 22, 71, 20);
        add(signupButton);
        label1 = new Label("If you don't yet have a username and");
        label1.setBounds(getInsets().left + 8, getInsets().top + 5, 204, 17);
        add(label1);
        label2 = new Label("password, please ");
        label2.setBounds(getInsets().left + 8, getInsets().top + 23, 105, 17);
        add(label2);
        label3 = new Label("Boggle Server Login:", 1);
        label3.setBounds(getInsets().left + 10, getInsets().top + 64, 196, 21);
        label3.setFont(new Font("Dialog", 0, 14));
        label3.setForeground(new Color(255));
        add(label3);
        quitButton = new Button();
        quitButton.setActionCommand("button");
        quitButton.setLabel("Quit");
        quitButton.setBounds(getInsets().left + 8, getInsets().top + 147, 39, 20);
        add(quitButton);
        setTitle("Please log in to server");
        SymAction symaction = new SymAction();
        userTextField.addActionListener(symaction);
        passTextField.addActionListener(symaction);
        loginButton.addActionListener(symaction);
        signupButton.addActionListener(symaction);
        quitButton.addActionListener(symaction);
        try
        {
            server = (boggleServerInterface)Naming.lookup("//" + settings.getHostName() + "/BoggleServer");
            userTextField.requestFocus();
            return;
        }
        catch(Exception exception)
        {
            System.out.println("passWordDialog: Can't find server!!!!!!!");
            System.out.println(exception.getMessage());
            exception.printStackTrace();
            label2.setText("XX " + settings.getHostName() + " XX");
            doBadServer();
            return;
        }
    }

    public void setVisible(boolean flag)
    {
        if(flag)
        {
            Rectangle rectangle = getParent().getBounds();
            Rectangle rectangle1 = getBounds();
            setLocation(rectangle.x + (rectangle.width - rectangle1.width) / 2, rectangle.y + (rectangle.height - rectangle1.height) / 2);
        }
        super.setVisible(flag);
    }

    public void addNotify()
    {
        Dimension dimension = getSize();
        super.addNotify();
        if(fComponentsAdjusted)
            return;
        setSize(getInsets().left + getInsets().right + dimension.width, getInsets().top + getInsets().bottom + dimension.height);
        Component acomponent[] = getComponents();
        for(int i = 0; i < acomponent.length; i++)
        {
            Point point = acomponent[i].getLocation();
            point.translate(getInsets().left, getInsets().top);
            acomponent[i].setLocation(point);
        }

        fComponentsAdjusted = true;
    }

    void loginButton_MouseClick()
    {
        if(loginButton.getLabel().equals("Login"))
        {
            doLogin();
            return;
        }
        if(loginButton.getLabel().equals("Request"))
        {
            doStartSignup();
            return;
        } else
        {
            doConfirmSignup();
            return;
        }
    }

    void doLogin()
    {
        int i = -3;
        try
        {
            i = server.login(userTextField.getText(), codec.encode(userTextField.getText(), passTextField.getText()), language);
        }
        catch(Exception exception)
        {
            System.out.println("passWordDialog Error: " + exception.getMessage());
            exception.printStackTrace();
            doBadServer();
            return;
        }
        if(i >= 0)
        {
            holder.setPlayer(i);
            holder.setServer(server);
            setVisible(false);
            dispose();
            return;
        } else
        {
            label1.setText("Login denied, try again");
            userTextField.setText("");
            passTextField.setText("");
            userTextField.requestFocus();
            return;
        }
    }

    void doStartSignup()
    {
        nameLabel.setVisible(false);
        userTextField.setVisible(false);
        password = passTextField.getText();
        passTextField.setText("");
        loginButton.setLabel("Confirm");
        label1.setText("Please confirm your password");
        passTextField.requestFocus();
    }

    void doConfirmSignup()
    {
        if(!passTextField.getText().equals(password))
        {
            label1.setText("Passwords don't match, try again");
            loginButton.setLabel("Request");
            nameLabel.setVisible(true);
            userTextField.setText("");
            userTextField.setVisible(true);
            passTextField.setText("");
            userTextField.requestFocus();
            return;
        }
        int i;
        try
        {
            i = server.Signup(userTextField.getText(), password, language);
        }
        catch(Exception exception)
        {
            System.out.println("passWordDialog Error: " + exception.getMessage());
            exception.printStackTrace();
            doBadServer();
            return;
        }
        if(i > 0)
        {
            label1.setText("Log in with your new Credentials");
            label3.setText("Boggle Server Login:");
            label2.setVisible(false);
            loginButton.setLabel("Login");
        } else
        if(i == -1)
        {
            label1.setText("Choose another Username");
            loginButton.setLabel("Request");
        } else
        if(i == -2)
        {
            doBadServer();
            label1.setText("Too many players registered.");
            return;
        }
        nameLabel.setVisible(true);
        userTextField.setText("");
        userTextField.setVisible(true);
        passTextField.setText("");
        userTextField.requestFocus();
    }

    void doBadServer()
    {
        label1.setText("Server Not Found, please Exit.");
        loginButton.setEnabled(false);
        signupButton.setEnabled(false);
        userTextField.setEnabled(false);
        passTextField.setEnabled(false);
        label3.setText("!!Boggle Server NOT FOUND!!");
    }

    void signupButton_MouseClick()
    {
        label3.setText("Boggle Server Signup:");
        label1.setText("Choose a name and password");
        label2.setText("8 Characters Max.");
        signupButton.setVisible(false);
        loginButton.setLabel("Request");
        userTextField.setText("");
        passTextField.setText("");
        userTextField.requestFocus();
    }

    void quitButton_MouseClick()
    {
        setVisible(false);
        holder.setVisible(false);
        holder.dispose();
    }

    boolean fComponentsAdjusted;
    Label nameLabel;
    Label passwordLabel;
    TextField userTextField;
    TextField passTextField;
    Button loginButton;
    Button signupButton;
    Label label1;
    Label label2;
    Label label3;
    Button quitButton;
    private playerViewer holder;
    private boggleServerInterface server;
    private String password;
    private int language;
}
