package jrummikub.view.impl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.border.LineBorder;

import jrummikub.model.PlayerSettings;
import jrummikub.model.Score;
import jrummikub.view.IScorePanel;

@SuppressWarnings("serial")
class ScorePanel extends JPanel implements IScorePanel {
	private final static ImageIcon STAR_ICON = new ImageIcon(
			ScorePanel.class.getResource("/jrummikub/resource/star.png"));
	private final static ImageIcon EMPTY_ICON = new ImageIcon(new BufferedImage(
			16, 16, BufferedImage.TYPE_INT_ARGB));

	private Iterable<PlayerSettings> players;
	private Iterable<Score> scores;
	private Score accumulatedScore;

	private JScrollPane scrollPane;
	private JPanel innerPanel;

	public ScorePanel() {
		setBorder(new LineBorder(Color.BLACK));
		setLayout(new BorderLayout(0, 5));

		JLabel titleLabel = new JLabel("Auswertung");
		titleLabel.setHorizontalAlignment(JLabel.CENTER);
		titleLabel.setFont(titleLabel.getFont().deriveFont(20.0f));
		add(titleLabel, BorderLayout.NORTH);

		innerPanel = new JPanel();
		innerPanel.setLayout(new GridBagLayout());

		scrollPane = new JScrollPane(innerPanel,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

		add(scrollPane);
	}

	@Override
	public void setPlayers(Iterable<PlayerSettings> players) {
		this.players = players;
	}

	@Override
	public void setScores(Iterable<Score> scores) {
		this.scores = scores;
	}

	@Override
	public void setAccumulatedScore(Score accumulatedScore) {
		this.accumulatedScore = accumulatedScore;
	}

	@Override
	public void update() {
		innerPanel.removeAll();

		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 0.0;
		c.gridwidth = GridBagConstraints.REMAINDER;

		addPlayerNames();

		innerPanel.add(new JSeparator(), c);

		int n = 0;
		for (Iterator<Score> it = scores.iterator(); it.hasNext();) {
			addScoreRow(it.next(), ++n);
		}

		innerPanel.add(new JSeparator(), c);

		addAccumulatedScore();

		c.weighty = 1;
		innerPanel.add(Box.createVerticalGlue(), c);
	}

	private void addPlayerNames() {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.3;
		c.weighty = 0.0;
		c.insets = new Insets(0, 7, 0, 7);

		JLabel roundHead = new JLabel("Runde");
		roundHead.setHorizontalAlignment(JLabel.CENTER);
		innerPanel.add(roundHead, c);

		c.weightx = 1.0;

		for (Iterator<PlayerSettings> it = players.iterator(); it.hasNext();) {
			PlayerSettings player = it.next();

			if (!it.hasNext()) {
				c.gridwidth = GridBagConstraints.REMAINDER;
			}

			JLabel playerNameLabel = new JLabel(player.getName());
			playerNameLabel.setIcon(ImageUtil.createColorIcon(player.getColor(), 12,
					1));
			playerNameLabel.setHorizontalAlignment(JLabel.CENTER);

			innerPanel.add(playerNameLabel, c);
		}
	}

	private void addScoreRow(Score score, int n) {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.3;
		c.weighty = 0.0;
		c.gridwidth = 1;
		c.insets = new Insets(0, 7, 0, 7);

		JLabel roundLabel = new JLabel(Integer.toString(n));
		roundLabel.setHorizontalAlignment(JLabel.CENTER);
		innerPanel.add(roundLabel, c);

		c.weightx = 1.0;

		for (int i = 0; i < score.getPoints().size(); ++i) {
			if (i == score.getPoints().size() - 1) {
				c.gridwidth = GridBagConstraints.REMAINDER;
			}

			boolean won = score.getWinners().get(i);
			JLabel scoreLabel = new JLabel(Integer.toString(score.getPoints().get(i)));
			scoreLabel.setIcon(won ? STAR_ICON : EMPTY_ICON);
			scoreLabel.setFont(scoreLabel.getFont().deriveFont(won ? Font.BOLD : 0));
			scoreLabel.setHorizontalAlignment(JLabel.CENTER);
			innerPanel.add(scoreLabel, c);
		}
	}

	private void addAccumulatedScore() {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.3;
		c.weighty = 0.0;
		c.insets = new Insets(0, 7, 0, 7);

		JLabel accumLabel = new JLabel("Gesamt");
		accumLabel.setHorizontalAlignment(JLabel.CENTER);
		innerPanel.add(accumLabel, c);

		c.weightx = 1.0;

		for (int i = 0; i < accumulatedScore.getPoints().size(); ++i) {
			if (i == accumulatedScore.getPoints().size() - 1) {
				c.gridwidth = GridBagConstraints.REMAINDER;
			}

			boolean won = accumulatedScore.getWinners().get(i);
			JLabel scoreLabel = new JLabel(Integer.toString(accumulatedScore
					.getPoints().get(i)));
			scoreLabel.setIcon(won ? STAR_ICON : EMPTY_ICON);
			scoreLabel.setFont(scoreLabel.getFont().deriveFont(won ? Font.BOLD : 0));
			scoreLabel.setHorizontalAlignment(JLabel.CENTER);
			innerPanel.add(scoreLabel, c);
		}
	}
}
