/*
 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
 */
 
 package ar.com.jkohen.awt;
 
 import java.awt.*;
 import ar.com.jkohen.irc.User;
 import ar.com.jkohen.irc.Modes;
 import ar.com.jkohen.util.Resources;
 
 public class NickItem extends Component
 {
 	protected boolean selected;
 	protected int modes;
 	protected String nick;
 
 	private Color textbg = SystemColor.text;
 	private Color textfg = SystemColor.textText;
 	private Color selbg = SystemColor.textHighlight;
 	private Color selfg = SystemColor.textHighlightText;
 	
 	private Resources res;
 	
 	private static final int OP = 0;
 	private static final int HALFOP = 1;
 	private static final int VOICE = 2;
 	private static final int ADMIN = 3;
 	private static final int OWNER = 4;
 	
 	private static final int BULLETS = 5;
 
 	private static int BULLET_DIAMETER;
 	private static int BULLET_HEIGHT;
 	private static boolean bullets_loaded;
 	private static final int BULLET_GAP = 4;
 
 	private static Image [] bullets;
 	private Image current_bullet;
 	private boolean item_bullet;
 
 	public NickItem(String nick)
 	{
 		this.modes = User.NORMAL_MASK;
 		setNick(nick);
 		
 		if (!bullets_loaded)
 			loadBullets();
 	}
 
 	private void loadBullets()
 	{
 		bullets_loaded = true;
 
 		bullets = new Image[BULLETS];
 		bullets[OP] = res.getImage("op");
 		bullets[HALFOP] = res.getImage("hop");
 		bullets[VOICE] = res.getImage("voiced");
 		bullets[ADMIN] = res.getImage("admin");
 		bullets[OWNER] = res.getImage("owner");
 	}
 	
 	public void setBullet(boolean b)
 	{
 		this.item_bullet = b;
 		if (b)
 		{
 			if ((modes & User.OWNER_MASK) != 0)
 				this.current_bullet = bullets[OWNER];
 			else if ((modes & User.ADMIN_MASK) != 0)
 				this.current_bullet = bullets[ADMIN];
 			else if ((modes & User.OP_MASK) != 0)
 				this.current_bullet = bullets[OP];
 			else if ((modes & User.HALFOP_MASK) != 0)
 				this.current_bullet = bullets[HALFOP];
 			else if ((modes & User.VOICE_MASK) != 0)
 				this.current_bullet = bullets[VOICE];
 			else
 				this.current_bullet = null;
 		}
 		else
 		{
 			this.current_bullet = null;
 		}
 		
 		int bullet_width;
 		BULLET_DIAMETER = 0;
 		for (int i = 0; i < BULLETS; i++)
 		{
 			while ((bullet_width = bullets[i].getWidth(this)) == -1);
 			BULLET_DIAMETER = Math.max(BULLET_DIAMETER, bullet_width);
 			BULLET_HEIGHT = Math.max(BULLET_HEIGHT, bullets[i].getHeight(this));
 		}
 	}
 
 	public void paint(Graphics g)
 	{
 		Rectangle clip = getBounds();
 		int x = 0;
 		Color fg = getTextForeground();
 		
 		FontMetrics fm = g.getFontMetrics();
 		int y = getLocation().y + fm.getLeading() + fm.getMaxAscent();
 
 		if (isSelected())
 		{
 			// Fill background.
 	  		g.setColor(getSelectedBackground());
 			g.fillRect(clip.x, clip.y, clip.width, clip.height);
 	  		fg = getSelectedForeground();
 		}
 
 		g.setColor(fg);
 		
 		if (current_bullet != null && item_bullet)
 		{
 			int h_bullet = current_bullet.getHeight(this);
 			int h_text = fm.getMaxDescent() + fm.getLeading() + fm.getMaxAscent();
 			int base = y;
 			if (h_bullet < h_text)
 				base = base - h_bullet;
 	   		g.drawImage(current_bullet, BULLET_GAP, base, this);
 		}
 		else
 			g.drawString(Modes.maskToSymbolic(modes), BULLET_GAP, y);
 
 		x += BULLET_DIAMETER + 2 * BULLET_GAP;
 		g.drawString(nick, x, y);
 	}
 	
 	public Dimension getSize(Graphics g)
 	{
 		FontMetrics fm = g.getFontMetrics();
 
 		int h = fm.getLeading() + fm.getMaxAscent() + fm.getMaxDescent();
 		h = Math.max(h, BULLET_HEIGHT);
 		Dimension size = new Dimension(fm.stringWidth(nick), h);
 		size.width += BULLET_DIAMETER + 2 * BULLET_GAP;
 		return size;
 	}
 
 	public boolean isSelected()
 	{
 		return selected;
 	}
 
 	public void setSelected(boolean selected)
 	{
 		this.selected = selected;
 	}
 
 	public String getNick()
 	{
 		return nick;
 	}
 
 	public void setNick(String nick)
 	{
 		this.nick = nick;
 	}
 	
 	public String toString()
 	{
 		return (Modes.maskToSymbolic(modes) + nick);
 	}
 
 	public int getModes()
 	{
 		return modes;
 	}
 
 	public void setModes(int modes)
 	{
 		this.modes = modes;
 		setBullet(this.item_bullet);
 	}
 
 	public void resetMode(int mode)
 	{
 		this.modes &= ~mode;
 		this.current_bullet = null;
 	}
 
 	public void setMode(int mode)
 	{
 		this.modes |= mode;
 		setBullet(this.item_bullet);
 	}
 
 	public Color getTextBackground()
 	{
 		return textbg;
 	}
 
 	public void setTextBackground(Color c)
 	{
 		this.textbg = c;
 	}
 
 	public Color getTextForeground()
 	{
 		return textfg;
 	}
 
 	public void setTextForeground(Color c)
 	{
 		this.textfg = c;
 	}
 
 	public Color getSelectedBackground()
 	{
 		return selbg;
 	}
 
 	public void setSelectedBackground(Color c)
 	{
 		this.selbg = c;
 	}
 
 	public Color getSelectedForeground()
 	{
 		return selfg;
 	}
 
 	public void setSelectedForeground(Color c)
 	{
 		this.selfg = c;
 	}
 }