/*
 Eteria IRC Client, an RFC 1459 compliant client program written in Java.
 Copyright (C) 2000  Javier Kohen <jkohen at tough.com>
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
 import java.awt.*;
 import java.awt.event.*;
 import java.util.Vector;
 
 import ar.com.jkohen.awt.ImageButton;
 import ar.com.jkohen.awt.NickInfo;
 import ar.com.jkohen.awt.ImageCanvas;
 import ar.com.jkohen.util.Resources;
 
 public class ASLBox extends NewDialog implements ActionListener, KeyListener
 {
 	private Resources res;
 
 	private TextField nick, pass, age, location;
 	private Checkbox checkM, checkF, checkU;
 
 	public ASLBox(EIRC eirc, String title)
 	{
 		super(eirc);
 		setTitle(title);
 
 		setFont(eirc.getFont());
 
 		GridBagLayout gb = new GridBagLayout();
 		GridBagConstraints gbc = new GridBagConstraints();
 
 		setLayout(gb);
 
 		gbc.insets = new Insets(2, 2, 2, 2);
 
 		ImageButton b;
 		Label l;
 
 		l = new Label(res.getString("eirc.enter_nick") + " :");
 		gbc.anchor = GridBagConstraints.EAST;
 		gbc.fill = GridBagConstraints.NONE;
 		gbc.weightx = 0;
 		gbc.gridwidth = 1;
 		gb.setConstraints(l, gbc);
 		add(l);
 
 		nick = new TextField();
 		gbc.fill = GridBagConstraints.HORIZONTAL;
 		gbc.weightx = 1;
 		gbc.gridwidth = GridBagConstraints.REMAINDER;
 		gb.setConstraints(nick, gbc);
 		add(nick);
 
 		l = new Label(res.getString("passw") + " :");
 		gbc.fill = GridBagConstraints.NONE;
 		gbc.gridwidth = 1;
 		gbc.weightx = 0;
 		gb.setConstraints(l, gbc);
 		add(l);
 
 		pass = new TextField();
 		pass.setEchoChar('*');
 		gbc.fill = GridBagConstraints.HORIZONTAL;
 		gbc.weightx = 1;
 		gbc.gridwidth = GridBagConstraints.REMAINDER;
 		gb.setConstraints(pass, gbc);
 		add(pass);
 
 		l = new Label(res.getString("asl.age") + " :");
 		gbc.fill = GridBagConstraints.NONE;
 		gbc.gridwidth = 1;
 		gbc.weightx = 0;
 		gb.setConstraints(l, gbc);
 		add(l);
 
 		age = new TextField();
 		gbc.fill = GridBagConstraints.HORIZONTAL;
 		gbc.gridwidth = GridBagConstraints.REMAINDER;
 		gbc.weightx = 1;
 		gb.setConstraints(age, gbc);
 		add(age);
 
 		l = new Label(res.getString("asl.sex") + " :");
 		gbc.anchor = GridBagConstraints.SOUTHEAST;
 		gbc.fill = GridBagConstraints.NONE;
 		gbc.gridwidth = 1;
 		gbc.gridheight = 2;
 		gbc.weightx = 0;
 		gb.setConstraints(l, gbc);
 		add(l);
 
 		CheckboxGroup sex = new CheckboxGroup();
 		checkM = new Checkbox(res.getString("asl.male"), sex, false);
 		checkF = new Checkbox(res.getString("asl.female"), sex, false);
 		checkU = new Checkbox(res.getString("asl.unknown"), sex, false);
 		ImageCanvas iconM = new ImageCanvas(res.getImage("asl.male"));
 		ImageCanvas iconF = new ImageCanvas(res.getImage("asl.female"));
 		ImageCanvas iconU = new ImageCanvas(res.getImage("asl.unknown"));
 		gbc.gridheight = 1;
 		gbc.fill = GridBagConstraints.NONE;
 		gbc.weightx = 1;
 		gbc.anchor = GridBagConstraints.CENTER;
 		gbc.gridwidth = 1;
 		gb.setConstraints(iconM, gbc);
 		add(iconM);
 		gbc.weightx = 1;
 		gbc.anchor = GridBagConstraints.CENTER;
 		gbc.gridwidth = 1;
 		gb.setConstraints(iconF, gbc);
 		add(iconF);
 		gbc.weightx = 1;
 		gbc.anchor = GridBagConstraints.CENTER;
 		gbc.gridwidth = GridBagConstraints.REMAINDER;
 		gb.setConstraints(iconU, gbc);
 		add(iconU);
 		gbc.weightx = 1;
 		gbc.anchor = GridBagConstraints.CENTER;
 		gbc.gridwidth = 1;
 		gb.setConstraints(checkM, gbc);
 		add(checkM);
 		gbc.weightx = 1;
 		gbc.anchor = GridBagConstraints.CENTER;
 		gbc.gridwidth = 1;
 		gb.setConstraints(checkF, gbc);
 		add(checkF);
 		gbc.weightx = 1;
 		gbc.anchor = GridBagConstraints.CENTER;
 		gbc.gridwidth = GridBagConstraints.REMAINDER;
 		gb.setConstraints(checkU, gbc);
 		add(checkU);
 
 		l = new Label(res.getString("asl.location") + " :");
 		gbc.fill = GridBagConstraints.NONE;
 		gbc.gridwidth = 1;
 		gbc.weightx = 0;
 		gb.setConstraints(l, gbc);
 		add(l);
 
 		location = new TextField();
 		gbc.fill = GridBagConstraints.HORIZONTAL;
 		gbc.gridwidth = GridBagConstraints.REMAINDER;
 		gbc.weightx = 1;
 		gb.setConstraints(location, gbc);
 		add(location);
 		
 		b = new ImageButton(res.getString("ok"));
 		b.setActionCommand("ok");
 		b.addActionListener(this);
 		gbc.fill = GridBagConstraints.NONE;
 		gbc.anchor = GridBagConstraints.CENTER;
 		gbc.gridwidth = GridBagConstraints.REMAINDER;
 		gb.setConstraints(b, gbc);
 		add(b);
 
 //		updateBackground((Container)this, getBackground());
 		
 		pack();
 		setResizable(false);
 		setVisible(true);
 
 		Component cp[] = getComponents();
 		for (int i = 0; i < cp.length; i++)
 			cp[i].addKeyListener(this);
 		
 		nick.requestFocus();
 	}
 
 	public void init(String n, String p, String r)
 	{
 		nick.setText(n);
 		pass.setText(p);
 
 		Vector v = NickInfo.getASL(r, r);
 		if (v == null)
 			return;
 		int a = ((Integer)v.firstElement()).intValue();
 		int s = ((Integer)v.elementAt(1)).intValue();
 		String l = (String)v.lastElement();
 
 		age.setText(String.valueOf(a));
 		if (s == NickInfo.SEX_MALE)
 			checkM.setState(true);
 		if (s == NickInfo.SEX_FEMALE)
 			checkF.setState(true);
 		if (s == NickInfo.SEX_UNKNOWN)
 			checkU.setState(true);
 		location.setText(l);
 	}
 	
 	public void connect()
 	{
 		String n = nick.getText();
 		String p = pass.getText();
 		String a = age.getText();
 		if (a.equals(""))
 			a = "0";
 		int s = 0;
 		if (checkM.getState())
 			s = NickInfo.SEX_MALE;
 		if (checkF.getState())
 			s = NickInfo.SEX_FEMALE;
 		String l = location.getText();
 		if (!eirc.isNickCorrect(n))
 			return;
 		close();
 		eirc.connect(n, p, NickInfo.fromASL(a, s, l));
 	}
 	
 	public void close()
 	{
 		processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
 	}
 
 	public void actionPerformed(ActionEvent ev)
 	{
 		String action = ev.getActionCommand();
 
 		if (action.equals("ok"))
 			connect();
 
 		if (action.equals("cancel"))
 			close();
 	}
 
 	public void keyTyped(KeyEvent ke)
 	{
 	}
 
 	public void keyReleased(KeyEvent ke)
 	{
 	}
 
 	public void keyPressed(KeyEvent ke)
 	{
 		if (ke.getKeyCode() == KeyEvent.VK_ENTER)
 			connect();
 			
 		if (ke.getKeyCode() == KeyEvent.VK_ESCAPE)
 			close();
 	}
 
 	public String getString()
 	{
 		return(nick.getText());
 	}
 
 	public void updateBackground(Container ct, Color cl)
 	{
 		ct.setBackground(cl);
 		Component cp[] = ct.getComponents();
 		for (int i = 0; i < cp.length; i++)
 		{
 			if (cp[i] instanceof Container)
 				updateBackground((Container)cp[i], cl);
 			else
 				if (!(cp[i] instanceof TextField))
 					cp[i].setBackground(cl);
 		}
 	}
 	
 	public void setTextBackground(Color c)
 	{
 		nick.setBackground(c);
 		pass.setBackground(c);
 		age.setBackground(c);
 		location.setBackground(c);
 	}
 
 	public void setTextForeground(Color c)
 	{
 		nick.setForeground(c);
 		pass.setForeground(c);
 		age.setForeground(c);
 		location.setForeground(c);
 	}
 }