package model;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

import main.Window;
import model.state.PlayState;
import resources.image.Images;
import resources.image.Images.ImageType;

public class InfoPanel {
	private PlayState play;

	private int OFFSET = 610;
	private List<String> chat;

	// Draw the game hud
	public InfoPanel(PlayState play) {
		this.play = play;
		chat = new ArrayList<String>();
	}

	public void paint(Graphics2D g2d) {
		g2d.setColor(Color.red);
		// TIME
		g2d.fillRect(200, OFFSET, 400 / 100 * play.getTimeleftpercent(), 20);

		g2d.setColor(Color.black);
		g2d.setFont(new Font("Century Schoolbook L", Font.ROMAN_BASELINE, 22));
		g2d.drawRect(200, OFFSET, 400, 20);

		// Player 1
		g2d.drawLine(35, OFFSET, 35, Window.HEIGHT);
		// P1 Name
		g2d.drawString(play.getPlayer1().getName(), 40, OFFSET + 5 + g2d.getFont().getSize());
		// P1 Score
		g2d.drawString(play.getPlayer1().getScore() + " xp", 40, OFFSET + 5 + g2d.getFont().getSize() * 2 + 5);
		// P1 Health
		for (int i = 0; i < play.getPlayer1().getHealth(); i++) {
			g2d.drawImage(Images.getImage(ImageType.HEARTH), null, 5, OFFSET + (30 * i));
		}
		g2d.drawLine(35, OFFSET + g2d.getFont().getSize() * 3, 200, OFFSET + g2d.getFont().getSize() * 3);

		// Chat
		g2d.drawLine(200, OFFSET, 200, Window.HEIGHT);
		// Chat
		for (int i = 0; i < chat.size(); i++) {
			g2d.drawString(chat.get(i), 210, OFFSET + 50 + g2d.getFont().getSize() * i);
		}
		g2d.drawLine(600, OFFSET, 600, Window.HEIGHT);
		g2d.drawLine(200, OFFSET + 60 + g2d.getFont().getSize() * 3, 600, OFFSET + 60 + g2d.getFont().getSize() * 3);
		// Current message being typed
		g2d.drawString(play.chatMessage, 270, Window.HEIGHT - 20 - g2d.getFont().getSize());

		g2d.drawLine(Window.WIDTH - 35, OFFSET, Window.WIDTH - 35, Window.HEIGHT);
		// P2 Name
		g2d.drawString(play.getPlayer2().getName(), Window.WIDTH - 40 - play.getPlayer2().getName().length() * 15, OFFSET + 5 + g2d.getFont().getSize());
		// P2 Score
		g2d.drawString(play.getPlayer2().getScore() + " xp", Window.WIDTH - 40 - ((play.getPlayer1().getScore() + " ").length() + 3) * 15, OFFSET + 5 + g2d.getFont().getSize() * 2 + 5);
		// P2 Health
		for (int i = 0; i < play.getPlayer2().getHealth(); i++) {
			g2d.drawImage(Images.getImage(ImageType.HEARTH), null, Window.WIDTH - 30, OFFSET + (30 * i));
		}
		g2d.drawLine(Window.WIDTH - 35, OFFSET + g2d.getFont().getSize() * 3, Window.WIDTH - 200, OFFSET + g2d.getFont().getSize() * 3);

		if (play.typing)
			g2d.setColor(Color.RED);
		g2d.drawString("Chat: ", 210, Window.HEIGHT - 20 - g2d.getFont().getSize());
	}

	// Add a chat message
	public void addChatMessage(String msg) {
		chat.add(msg);

		if (chat.size() > 4)
			chat.remove(0);
	}

	public void clearChat() {
		chat.clear();
	}

}
