/*
Copyright (C) 2005 David Green <green@couchpotato.net>
All Rights Reserved.

This file is part of Aelfengard.

Aelfengard is proprietary software. You may not redistribute it without
prior written permission from the copyright holder.
*/

package common.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.List;

import javax.swing.Icon;

import common.Face;
import common.FaceModel;
import common.FaceModel.FacePart;

/**
 * A large Icon that is a fully-drawn player-chosen face. 
 * 
 * @author David Green <green@couchpotato.net>
 */
public class FaceIcon implements Icon {
    
    private final Face face;

    /**
     * A "loading" image that is the same size as a face. This can
     * be displayed as a placeholder while you're waiting for the
     * face to load.
     */
    public static final Icon LOADING_IMAGE = new Icon() {
    
        public int getIconHeight() {
            return 250;
        }
    
        public int getIconWidth() {
            return 250;
        }
    
        public void paintIcon(Component c, Graphics g, int x, int y) {
            Graphics2D g2 = (Graphics2D) g;
            g2.translate(x, y);
            g2.setColor(Color.white); // draw diagonal hash lines
            for (int i = 0; i < 500; i += 10) {
                g2.drawLine(i, 0, i - 250, 250);
            }
            // draw a border around the whole image
            g2.drawRect(0, 0, 249, 249);
            // get ready to draw the "Loading..." text
            g2.setFont(g.getFont().deriveFont(Font.BOLD, 20.0f));
            String msg = "Loading...";
            // But first draw a black rectangle with a white border, slightly larger
            // than the text, so that the hash lines don't go through the text.
            Rectangle bounds = g2.getFontMetrics().getStringBounds(msg, g).getBounds();
            bounds.translate(125 - bounds.width / 2, 125);
            // The black rectangle
            g2.setColor(Color.black);
            g2.fillRect(bounds.x - 10, bounds.y - 10, bounds.width + 20, bounds.height + 20);
            // The white border
            g2.setColor(Color.white);
            g2.drawRect(bounds.x - 10, bounds.y - 10, bounds.width + 20, bounds.height + 20);
            // Finally, draw "Loading..." text inside the black rectangle
            g2.drawString(msg, bounds.x, 125);
        }
    
    };
    
    /**
     * Create a new Face Icon depicting the specified Face.
     * @param face the face to depict
     */
    public FaceIcon(Face face) {
        this.face = face;
    }
    
    /**
     * Paints the Face that this FaceIcon is associated with.
     * @param c the component to paint upon
     * @param g the graphics context to use for painting
     * @param x the x coordinate where the painting should be performed
     * @param y the y coordinate where the painting should be performed
     */
    public void paintIcon(Component c, Graphics g, int x, int y) {
        FaceModel model = face.getFaceModel(); // elf_m1, kithian_f1, etc.
        List<Integer> parts = face.getParts(); // user-chosen face parts
        // Draw the actual parts. We do this by forming an ImageIcon for each
        // part, then telling that ImageIcon to paint in our own graphics
        // context. It's really nice that paintIcon() preserves alpha
        // restrictions, and therefore we can just paint all of these
        // parts on top of each other to produce the final image.
        Iterator<Integer> iter = parts.iterator();
        for (FacePart facePart : model.getFaceParts()) {
            String image;
            if (facePart.isStatic()) {
                image = facePart.getImage();
            }
            else {
                image = facePart.getName().toLowerCase() + iter.next() + ".png";
            }
            // create a new icon for that part, and tell it to paint in
            // our own graphics context. :)
            GameClient.getImageIcon("paperdoll/" + model.getDirectory() + "/" + image).paintIcon(c, g, x, y);
        }
    }

    /**
     * Fixed width of 250 pixels.
     * @return 250
     */
    public int getIconWidth() {
        return 250;
    }

    /**
     * Fixed height of 250 pixels.
     * @return 250
     */
    public int getIconHeight() {
        return 250;
    }

}