package net.trolans.IRC;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

public class Launch extends Frame
{
    IRC irc;
    // IMPORTANT: Source code between BEGIN/END comment pair will be regenerated
    // every time the form is saved. All manual changes will be overwritten.
    // BEGIN GENERATED CODE
    // member declarations
    javax.swing.JCheckBox identCheckbox = new javax.swing.JCheckBox();
    javax.swing.JLabel serverLabel = new javax.swing.JLabel();
    javax.swing.JLabel portLabel = new javax.swing.JLabel();
    javax.swing.JLabel nickLabel = new javax.swing.JLabel();
    javax.swing.JLabel identLabel = new javax.swing.JLabel();
    javax.swing.JLabel realNameLabel = new javax.swing.JLabel();
    javax.swing.JTextField serverField = new javax.swing.JTextField();
    javax.swing.JTextField portField = new javax.swing.JTextField();
    javax.swing.JTextField nickField = new javax.swing.JTextField();
    javax.swing.JTextField identField = new javax.swing.JTextField();
    javax.swing.JTextField realNameField = new javax.swing.JTextField();
    javax.swing.JButton saveButton = new javax.swing.JButton();
    javax.swing.JButton connectButton = new javax.swing.JButton();
    javax.swing.JButton cancelButton = new javax.swing.JButton();
    javax.swing.JLabel logLabel = new javax.swing.JLabel();
    javax.swing.JTextField logfileField = new javax.swing.JTextField();
    javax.swing.JButton logfileBrowseButton = new javax.swing.JButton();
    
    // END GENERATED CODE
    
    public Launch(IRC irc)
    {
        this.irc = irc;
    }
    
    public void initComponents() throws Exception
    {
        // IMPORTANT: Source code between BEGIN/END comment pair will be regenerated
        // every time the form is saved. All manual changes will be overwritten.
        // BEGIN GENERATED CODE
        // the following code sets the frame's initial state
        identCheckbox.setSize(new java.awt.Dimension(70, 21));
        identCheckbox.setLocation(new java.awt.Point(290, 110));
        identCheckbox.setVisible(true);
        identCheckbox.setText("Use?");
        
        serverLabel.setSize(new java.awt.Dimension(80, 20));
        serverLabel.setLocation(new java.awt.Point(10, 20));
        serverLabel.setVisible(true);
        serverLabel.setText("Server:");
        
        portLabel.setSize(new java.awt.Dimension(80, 20));
        portLabel.setLocation(new java.awt.Point(10, 50));
        portLabel.setVisible(true);
        portLabel.setText("Port:");
        
        nickLabel.setSize(new java.awt.Dimension(80, 20));
        nickLabel.setLocation(new java.awt.Point(10, 80));
        nickLabel.setVisible(true);
        nickLabel.setText("Nick:");
        
        identLabel.setSize(new java.awt.Dimension(80, 20));
        identLabel.setLocation(new java.awt.Point(10, 110));
        identLabel.setVisible(true);
        identLabel.setText("Ident:");
        
        realNameLabel.setSize(new java.awt.Dimension(80, 20));
        realNameLabel.setLocation(new java.awt.Point(10, 140));
        realNameLabel.setVisible(true);
        realNameLabel.setText("Real name:");
        
        serverField.setSize(new java.awt.Dimension(260, 20));
        serverField.setLocation(new java.awt.Point(100, 20));
        serverField.setVisible(true);
        
        portField.setSize(new java.awt.Dimension(260, 20));
        portField.setLocation(new java.awt.Point(100, 50));
        portField.setVisible(true);
        
        nickField.setSize(new java.awt.Dimension(260, 20));
        nickField.setLocation(new java.awt.Point(100, 80));
        nickField.setVisible(true);
        
        identField.setSize(new java.awt.Dimension(180, 20));
        identField.setLocation(new java.awt.Point(100, 110));
        identField.setVisible(true);
        
        realNameField.setSize(new java.awt.Dimension(260, 20));
        realNameField.setLocation(new java.awt.Point(100, 140));
        realNameField.setVisible(true);
        
        saveButton.setSize(new java.awt.Dimension(110, 20));
        saveButton.setLocation(new java.awt.Point(10, 210));
        saveButton.setVisible(true);
        saveButton.setText("Save");
        
        connectButton.setSize(new java.awt.Dimension(110, 20));
        connectButton.setLocation(new java.awt.Point(130, 210));
        connectButton.setVisible(true);
        connectButton.setText("Connect");
        
        cancelButton.setSize(new java.awt.Dimension(110, 20));
        cancelButton.setLocation(new java.awt.Point(250, 210));
        cancelButton.setVisible(true);
        cancelButton.setText("Cancel");
        
        logLabel.setSize(new java.awt.Dimension(80, 20));
        logLabel.setLocation(new java.awt.Point(10, 170));
        logLabel.setVisible(true);
        logLabel.setText("Logfile:");
        
        logfileField.setSize(new java.awt.Dimension(160, 20));
        logfileField.setLocation(new java.awt.Point(100, 170));
        logfileField.setVisible(true);
        
        logfileBrowseButton.setSize(new java.awt.Dimension(90, 20));
        logfileBrowseButton.setLocation(new java.awt.Point(270, 170));
        logfileBrowseButton.setVisible(true);
        logfileBrowseButton.setText("Browse...");
        
        setLocation(new java.awt.Point(0, 0));
        setSize(new java.awt.Dimension(376, 266));
        setBackground(java.awt.SystemColor.control);
        setLayout(null);
        setTitle("Setup");
        add(identCheckbox);
        add(serverLabel);
        add(portLabel);
        add(nickLabel);
        add(identLabel);
        add(realNameLabel);
        add(serverField);
        add(portField);
        add(nickField);
        add(identField);
        add(realNameField);
        add(saveButton);
        add(connectButton);
        add(cancelButton);
        add(logLabel);
        add(logfileField);
        add(logfileBrowseButton);
        
        identCheckbox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent e) {
                identCheckboxItemStateChanged(e);
            }
        });
        
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                saveButtonActionPerformed(e);
            }
        });
        
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                connectButtonActionPerformed(e);
            }
        });
        
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                cancelButtonActionPerformed(e);
            }
        });
        
        logfileBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                logfileBrowseButtonActionPerformed(e);
            }
        });
        
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {
                thisWindowClosing(e);
            }
        });
        // END GENERATED CODE
        serverField.setText(irc.prefs.getProperty("irc.default.server_name", "irc.exodusirc.net"));
        portField.setText(irc.prefs.getProperty("irc.default.server_port", "6667"));
        nickField.setText(irc.prefs.getProperty("irc.default.nick", System.getProperty("user.name", "")));
        identField.setText(irc.prefs.getProperty("irc.default.ident", System.getProperty("user.name", "")));
        realNameField.setText(irc.prefs.getProperty("irc.default.real_name", System.getProperty("user.name", "")));
        identCheckbox.setSelected(irc.prefs.getProperty("irc.default.use_ident", "true").equalsIgnoreCase("true") ? true:false);
        logfileField.setText(irc.prefs.getProperty("irc.default.logfile", ""));
    }
    
    private boolean mShown = false;
    
    public void addNotify()
    {
        super.addNotify();
        
        if (mShown)
            return;
        
        // move components to account for insets
        Insets insets = getInsets();
        Component[] components = getComponents();
        for (int i = 0; i < components.length; i++) {
            Point location = components[i].getLocation();
            location.move(location.x, location.y + insets.top);
            components[i].setLocation(location);
        }
        
        mShown = true;
    }
    
    // Close the window when the close box is clicked
    void thisWindowClosing(java.awt.event.WindowEvent e)
    {
        setVisible(false);
        dispose();
        System.exit(0);
    }
    
    public void connectButtonActionPerformed(java.awt.event.ActionEvent e)
    {
        irc.inConfigure = false;
        setVisible(false);
        IRCUser me = new IRCUser(nickField.getText() + "!" + identField.getText() + "@localhost");
        me.setRealName(realNameField.getText());
        int port;
        try
        {
            port = Integer.parseInt(portField.getText());
        }
        catch (Exception x) { port = 6667; };
        IRCServer server = new IRCServer(irc, serverField.getText(), port);
        irc.me = me;
        irc.server = server;
        dispose();
    }
    
    public void saveButtonActionPerformed(java.awt.event.ActionEvent e)
    {
        if(irc.prefs == null)
        {
            irc.prefs = new Properties();
        }
        if(irc.prefs != null)
        {
            irc.prefs.put("irc.default.server_name", serverField.getText());
            irc.prefs.put("irc.default.server_port", portField.getText());
            irc.prefs.put("irc.default.nick", nickField.getText());
            irc.prefs.put("irc.default.ident", identField.getText());
            irc.prefs.put("irc.default.real_name", realNameField.getText());
            irc.prefs.put("irc.default.use_ident", irc.useIdent ? "true":"false");
            irc.prefs.put("irc.default.logfile", logfileField.getText());
            try
            {
                irc.prefs.store(new BufferedOutputStream(new FileOutputStream("irc.properties")), "IRC Prefs");
            }
            catch (Exception x) { System.out.println("Error writing prefs file: " + x); }
        }
    }
    
    public void cancelButtonActionPerformed(java.awt.event.ActionEvent e)
    {
        this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
    }
    
    public void identCheckboxItemStateChanged(java.awt.event.ItemEvent e)
    {
        irc.useIdent = (e.getStateChange() != ItemEvent.DESELECTED) ? true:false;
    }
    
    public void logfileBrowseButtonActionPerformed(java.awt.event.ActionEvent e)
    {
        JFileChooser jfc = new JFileChooser(".");
        int returnVal = jfc.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
            logfileField.setText(jfc.getSelectedFile().getName());
        }
    }
}

