package jrummikub.view.impl;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.swing.ImageIcon;

import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.util.Event1;
import jrummikub.util.IEvent1;
import jrummikub.util.Pair;
import jrummikub.view.IStoneCollectionPanel;
import jrummikub.view.impl.StonePainter.StoneState;

/**
 * Implementation of the stone collection (selection)
 */
@SuppressWarnings("serial")
class StoneCollectionPanel extends AbstractStonePanel implements
		IStoneCollectionPanel {
	private final static ImageIcon BACKGROUND = new ImageIcon(
			HandPanel.class.getResource("/jrummikub/resource/felt.png"));
	private final static ImageIcon DARK_BACKGROUND = new ImageIcon(
			HandPanel.class.getResource("/jrummikub/resource/dark_felt.png"));
	private final static float INSET_RATIO = 0.1f;

	private Collection<Stone> selectedStones = Collections.emptyList();

	private Event1<Point> otherClickEvent = new Event1<Point>();

	private boolean hidden = false;

	/**
	 * Creates a new StoneCollection instance
	 */
	StoneCollectionPanel() {
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				rescale();
			}
		});
	}

	private void rescale() {
		int height = getHeight() - 2 * (int) (getHeight() * INSET_RATIO);

		getStonePainter().setScale(height * StonePainter.HEIGHT_SCALE);
		repaint();
	}

	/**
	 * Sets the stones to be shown in the collection
	 * 
	 * @param selectedStones
	 *          the selected stones
	 */
	void setSelectedStones(Collection<Stone> selectedStones) {
		this.selectedStones = selectedStones;

		List<Pair<Stone, Position>> stones = new ArrayList<Pair<Stone, Position>>();
		float x = 0;

		for (Stone stone : selectedStones) {
			stones.add(new Pair<Stone, Position>(stone, new Position(x, 0)));
			x++;
		}

		setStones(stones);
		repaint();
	}

	@Override
	protected Pair<Integer, Integer> getTranslation() {
		int inset = (int) (getHeight() * INSET_RATIO);
		int width = getStonePainter().getStoneWidth() * selectedStones.size() + 2
				* inset;
		int x = (getWidth() - width) / 2;

		return new Pair<Integer, Integer>(x + inset, inset);
	}

	/**
	 * The other click event is emitted by the stone collection when the player
	 * has clicked on it, but hasn't hit a stone. This is rather probable as the
	 * stone collection panel is filling the whole table width while being mostly
	 * invisible.
	 * 
	 * @return the event
	 */
	IEvent1<Point> getOtherClickEvent() {
		return otherClickEvent;
	}

	@Override
	protected boolean handleOtherClickEvent(Position pos) {
		Insets insets = getInsets();
		Pair<Integer, Integer> trans = getTranslation();
		int x = (int) (pos.getX() * getStonePainter().getStoneWidth())
				+ insets.left + trans.getFirst();
		int y = (int) (pos.getY() * getStonePainter().getStoneHeight())
				+ insets.top + trans.getSecond();

		otherClickEvent.emit(new Point(x, y));

		return true;
	}

	@Override
	public void paintComponent(Graphics g1) {
		for (int xpos = 0; xpos < getWidth(); xpos += BACKGROUND.getIconWidth()) {
			for (int ypos = 0; ypos < getHeight(); ypos += BACKGROUND.getIconHeight()) {
				BACKGROUND.paintIcon(this, g1, xpos, ypos);
			}
		}

		if (hidden) {
			return;
		}

		int inset = (int) (getHeight() * INSET_RATIO);
		int width = getStonePainter().getStoneWidth() * selectedStones.size() + 2
				* inset, height = getHeight();
		int x = (getWidth() - width) / 2;
		Graphics2D g = (Graphics2D) g1.create(x, 0, width, height);
		g.setClip(new RoundRectangle2D.Float(0, 0, width, height, inset * 1.5f,
				inset * 1.5f));

		if (!selectedStones.isEmpty()) {
			for (int xpos = 0; xpos < width; xpos += DARK_BACKGROUND.getIconWidth()) {
				for (int ypos = 0; ypos < height; ypos += DARK_BACKGROUND
						.getIconHeight()) {
					DARK_BACKGROUND.paintIcon(this, g, xpos, ypos);
				}
			}

			g.translate(inset, inset);

			float xpos = 0;

			for (Stone stone : selectedStones) {
				getStonePainter().paintStone(g, stone, new Position(xpos, 0), StoneState.NORMAL,
						stone == getHoveredStone());
				xpos++;
			}
		}
	}

	@Override
	public void setHidden(boolean enable) {
		hidden = enable;
		repaint();
	}
}
