package jrummikub.view.impl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.util.Collection;
import java.util.Collections;

import javax.swing.ImageIcon;
import javax.swing.border.MatteBorder;

import jrummikub.model.Hand;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.util.Pair;
import jrummikub.view.IHandPanel;

/**
 * Implementation of the board
 */
@SuppressWarnings("serial")
class HandPanel extends AbstractStonePanel implements IHandPanel {
	private final static int HEIGHT = 2;

	private final static BufferedImage BACKGROUND;
	static {
		ImageIcon image = new ImageIcon(
				HandPanel.class.getResource("/jrummikub/resource/wood.png"));
		BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(),
				BufferedImage.TYPE_INT_RGB);

		image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0);
	}

	private BufferedImage scaledBackground = BACKGROUND;

	private PlayerPanel playerPanel;

	private int currentRow = 0;
	private int maxRow = 0;

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

	/**
	 * Creates a new Board instance
	 */
	HandPanel(PlayerPanel playerPanel) {
		this.playerPanel = playerPanel;

		setBorder(new MatteBorder(0, 1, 0, 1, Color.DARK_GRAY));

		addComponentListener(new ComponentAdapter() {

			@Override
			public void componentResized(ComponentEvent e) {
				rescale();
			}
		});
	}

	private BufferedImage getScaledBackground(int size) {
		BufferedImage scaled = new BufferedImage(size, size,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = scaled.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BICUBIC);

		g.drawImage(BACKGROUND, 0, 0, size, size, null);

		return scaled;
	}

	@Override
	protected void paintComponent(Graphics g1) {
		Insets insets = getInsets();
		int x = insets.left, y = insets.top, width = getWidth() - insets.left
				- insets.right, height = getHeight() - insets.top - insets.bottom;
		Graphics2D g = (Graphics2D) g1.create(x, y, width, height);

		int size = height / HEIGHT;

		if (repaintAll) {
			if (scaledBackground.getHeight() != size)
				scaledBackground = getScaledBackground(size);

			for (int i = 0; i < HEIGHT; ++i) {
				for (int xpos = -size * i / 3; xpos < width; xpos += size) {
					g.drawImage(scaledBackground, xpos, size * i, null);
				}
			}
		}

		Pair<Integer, Integer> trans = getTranslation();
		g.translate(trans.getFirst(), trans.getSecond());

		for (Pair<Stone, Position> entry : getStones()) {
			getStonePainter().paintStone(g, entry.getFirst(), entry.getSecond(),
					selectedStones.contains(entry.getFirst()),
					entry.getFirst() == getHoveredStone());
		}
	}

	@Override
	public void setStones(Iterable<Pair<Stone, Position>> stones) {
		super.setStones(stones);

		maxRow = 0;
		for (Pair<Stone, Position> entry : stones) {
			if (entry.getSecond().getY() > maxRow) {
				maxRow = (int) entry.getSecond().getY();
			}
		}

		repaintAll = true;
		repaint();

		playerPanel.updateButtons();
	}

	void rowUp() {
		currentRow--;
		repaintAll = true;
		repaint();

		playerPanel.updateButtons();
	}

	void rowDown() {
		currentRow++;
		repaintAll = true;
		repaint();

		playerPanel.updateButtons();
	}

	@Override
	public void resetCurrentRow() {
		currentRow = Math.max(0, maxRow - 1);

		playerPanel.updateButtons();
	}

	boolean canRowUp() {
		return (currentRow > 0);
	}

	boolean canRowDown() {
		return (currentRow < maxRow);
	}

	@Override
	public Pair<Integer, Integer> getTranslation() {
		return new Pair<Integer, Integer>(0, -getStonePainter().getStoneHeight()
				* currentRow);
	}

	/**
	 * Sets the stones that are to be painted selected
	 * 
	 * @param stones
	 *          the selected stones
	 */
	void setSelectedStones(Collection<Stone> stones) {
		selectedStones = stones;
		repaint();
	}

	private void rescale() {
		Insets insets = getInsets();
		int size = (getHeight() - insets.top - insets.bottom) / HEIGHT;

		getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE);

		setSize(new Dimension(Hand.WIDTH * getStonePainter().getStoneWidth()
				+ insets.left + insets.right, getHeight()));

		repaintAll = true;
	}
}
