/*
 Eteria IRC Client, an RFC 1459 compliant client program written in Java.
 Copyright (C) 2000-2001  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.io.IOException;
 import java.net.URL;
 import java.util.Observable;
 import java.util.StringTokenizer;
 
 import ar.com.jkohen.awt.*;
 import ar.com.jkohen.irc.Channel;
 import ar.com.jkohen.irc.MircMessage;
 import ar.com.jkohen.util.ConfigurationProperties;
 import ar.com.jkohen.util.Resources;
 
 
 class PrivateWindow extends ChatWindow implements ActionListener, ItemListener
 {
 	private TextFieldHistory entry;
 	private ImageButton whois;
 	private ImageButton close;
 	private ImageButton kill;
 	
 	private Choice color;
 
 	private EIRC eirc;
 	private String user;
 	private String last_attr;
 	private Resources res;
 	private boolean is_bot;
 
 	public PrivateWindow(EIRC eirc, String u, String title)
 	{
 		super(title);
 
 	  	this.eirc = eirc;
 		this.user = u;
 		this.is_bot = eirc.isService(u);
 		
 		text_canvas.setMode(eirc.scrollSpeed());
 
 		GridBagLayout gb = new GridBagLayout();
 		GridBagConstraints gbc = new GridBagConstraints();
 
 		setLayout(gb);
 		gbc.insets = new Insets(2, 2, 2, 2);
 		gbc.gridy = 0;
 
 		/* Component's initialization
 		 */
 
 		gbc.weightx = 1.0;
 		gbc.weighty = 1.0;
 		gbc.gridwidth = 4;
 		gbc.fill = GridBagConstraints.BOTH;
 		gb.setConstraints(text_canvas, gbc);
 		add(text_canvas);
 
 		gbc.weightx = 0.0;
 
 		// From here below, everything fills the space horizontally.

 		gbc.gridwidth = 1;
 	  	gbc.fill = GridBagConstraints.NONE;
 		gbc.weightx = 1.0;
 		gbc.weighty = 0.0;
 
 		/* Next Row */
 		gbc.gridx = 0;
 		gbc.gridy++;
 
 		color = new Choice();
 		StringTokenizer tk = new StringTokenizer(res.getString("conf.write_col_list"), ",");
 		while (tk.hasMoreTokens())
 			color.add(tk.nextToken());
 		gbc.anchor = GridBagConstraints.WEST;
 		gb.setConstraints(color, gbc);
 		add(color);
 		color.setEnabled(!is_bot);
 
 	 	gbc.weightx = 0;
 		gbc.gridx++;
 		
 		whois = new ImageButton(Resources.getLabel("pv.text.whois"), Resources.getImage("pv.icon.whois"));
 		whois.setEnabled(true);
 		gb.setConstraints(whois, gbc);
 		add(whois);
 		
 		gbc.gridx++;
 		
 		kill = new ImageButton(Resources.getLabel("pv.text.kill"), Resources.getImage("pv.icon.kill"));
 		kill.setEnabled(true);
 		gb.setConstraints(kill, gbc);
 		add(kill);
 		
 		gbc.gridx++;
 		
 		close = new ImageButton(Resources.getLabel("pv.text.close"), Resources.getImage("pv.icon.close"));
 		close.setEnabled(true);
 		gbc.anchor = GridBagConstraints.EAST;
 		gb.setConstraints(close, gbc);
 		add(close);
 		
 		gbc.gridx = 0;
 		gbc.gridy++;
 
 		this.entry = new TextFieldHistory(eirc.getFrame());
 		gbc.anchor = GridBagConstraints.CENTER;
 		gbc.fill = GridBagConstraints.HORIZONTAL;
 		gbc.gridwidth = 4;
 		gb.setConstraints(entry, gbc);
 		add(entry);
 
 
 		/* Event Listeners
 		 */
 
 	  	whois.addActionListener(this);
 	  	close.addActionListener(this);
 	  	kill.addActionListener(this);
 		entry.addActionListener(this);
 		color.addItemListener(this);
 	}
 
 	public void requestFocus()
 	{
 		entry.requestFocus();
 	}
 
 	protected String getNick()
 	{
 		return eirc.getNick();
 	}
 
 	protected void visitURL(URL url)
 	{
 		eirc.visitURL(url);
 	}
 
 	protected void joinChannel(String name)
 	{
 		eirc.joinChannel(name);
 	}
 	
 	protected void bell()
 	{
 		// Not used here, see EIRC.java PRIVMSG process.

 	}
 
 	protected void talkTo(String nick)
 	{
    		String text = entry.getText();
    		nick += talkto;
 
    		// Insert nick at caret position.

    		int pos = entry.getCaretPosition();
 
    		text = text.substring(0, pos).concat(nick).concat(text.substring(pos));
 
    		entry.setText(text);
    		entry.setCaretPosition(pos + nick.length());
    		entry.requestFocus();
 	}
 		
 	protected void copyText(String s)
 	{
 		eirc.cutPaste(s);
 	}
 
 	public void update(Observable o, Object arg)
 	{
 		super.update(o, arg);
 
 		ConfigurationProperties props = (ConfigurationProperties) o;
 		
 		if (arg == null || arg.equals("write_color"))
 			color.select(props.getInt("write_color"));
 	}
 	
 	public void setEnabled(boolean b)
 	{
 		entry.setEnabled(b);
 		whois.setEnabled(b);
 		kill.setEnabled(b);
 		close.setEnabled(b);
 	}
 	
 	public void setFont(Font f)
 	{
 		super.setFont(f);
 		entry.setFont(f);
 		whois.setFont(f);
 		kill.setFont(f);
 		close.setFont(f);
 	}
 	
 	public void setBackground(Color c)
 	{
 		super.setBackground(c);
 		whois.repaint();
 		close.repaint();
 		kill.repaint();
 	}
 	
 	public void setTextBackground(Color c)
 	{
 		text_canvas.setBackground(c);
 		entry.setBackground(c);
 	}
 
 	public void setTextForeground(Color c)
 	{
 		text_canvas.setForeground(c);
 		entry.setForeground(c);
 	}
 	
 	public void setSelectedBackground(Color c)
 	{
 		text_canvas.setSelectedBackground(c);
 	}
 
 	public String getUser()
 	{
 		return(user);
 	}
 
 	public void setUser(String s)
 	{
 		this.user = s;
 	}
 
 	public void actionPerformed(ActionEvent ev)
 	{
 		Object comp = ev.getSource();
 
 		if (comp.equals(entry))
 		{
 			String text = entry.getText();
 			if (text.length() <= 0)
 				return;
 
 			// See if it's a command.

 			if (text.charAt(0) == '/')
 			{
 				// Remove slash.

 				text = text.substring(1);
 
 				// Avoid empty (or pure white space) input.

 				if (text.trim().length() > 0)
 					eirc.sendCommand(text, this);
 			}
 			else
 			{
 				String message = entry.getText();
 				if (message.length() > 450)
 					message = message.substring(0, 449);
 				if (color.getSelectedIndex() != 1 && !is_bot)
 					message = MircMessage.COLOR + String.valueOf(color.getSelectedIndex()) + " " + message;
 					
 				printMyPrivmsg(message);
 
 				String [] p = { user, message };
 				eirc.sendMessage("PRIVMSG", p);
 			}
 
 			entry.setText("");
 		} 
 		else if (comp.equals(whois))
 		{
 			String p[] = { user };
 			if (p[0] != null)
 				eirc.sendMessage("WHOIS", p);
 		}
 		else if (comp.equals(close))
 		{
 			this.dispose();
 		}
 		else if (comp.equals(kill))
 		{
 			if (user != null)
 				eirc.sendCommand("IGNORE " + user, this);
 		}
 	}
 	
 	public void itemStateChanged(ItemEvent e) 
 	{
 		if (e.getSource() == color)
 			entry.requestFocus();
 	}
 }