package hud;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

import _texture.loader.Textures;
import engine.Game;
import engine.MouseInput;
import fonts.fonts;
import hud.inventory.InventorySlot;
import player.Player;

/**  ------------- Nihilne ------------
    *** @author Mitch Weaver - 2016 ***
      ~~~~ All Rights Reserved ~~~~ */

public final class SecondaryHUD {

	public static int x, y;
	public static final int SECONDARY_HUD_WIDTH = 245, SECONDARY_HUD_HEIGHT = 320, MINIMIZED_WIDTH = 224, MINIMIZED_HEIGHT = 56, TAB_BOX_WIDTH = 50, TAB_BOX_HEIGHT = 42, MINIMIZED_BUTTON_WIDTH = 33,
						    MINIMIZED_BUTTON_HEIGHT = 10, ATTACK_STYLE_CHOICE_WIDTH = 63, ATTACK_STYLE_CHOICE_HEIGHT = 37, MINIMIZE_BUTTON = 5;
	public static TAB tab = TAB.MINIMIZED;
	public static final Rectangle maximizedBounds = new Rectangle(0, 0, SECONDARY_HUD_WIDTH, SECONDARY_HUD_HEIGHT);
	public static final Rectangle minimizedBounds = new Rectangle(0, 0, MINIMIZED_WIDTH, MINIMIZED_HEIGHT);

	public static final void init() {
		final int NUMBER_OF_TABS_PLUS_MINIMIZE_BUTTON = 5;
		int count = 0;
		Player.tabSlots = new InventorySlot[NUMBER_OF_TABS_PLUS_MINIMIZE_BUTTON];
		Player.tabSlots[count++] = (new InventorySlot(1, -1, new Point(25, 23), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.tabSlots[count++] = (new InventorySlot(2, -1, new Point(75, 23), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.tabSlots[count++] = (new InventorySlot(3, -1, new Point(125, 23), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.tabSlots[count++] = (new InventorySlot(4, -1, new Point(175, 23), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.tabSlots[count++] = (new InventorySlot(5, -1, new Point(150, 18), new Rectangle(0, 0, MINIMIZED_BUTTON_WIDTH, MINIMIZED_BUTTON_HEIGHT)));

		final int NUMBER_OF_EQUIPMENT_SLOTS_PLUS_ATTACK_STYLE_CHOICES = 15;
		count = 0;
		Player.equipmentSlots = new InventorySlot[NUMBER_OF_EQUIPMENT_SLOTS_PLUS_ATTACK_STYLE_CHOICES];
		Player.equipmentSlots[count++] = (new InventorySlot(1, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(2, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(3, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(4, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(5, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(6, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(7, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(8, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(9, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(10, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(11, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(12, -3, new Point(0, 0), new Rectangle(0, 0, TAB_BOX_WIDTH, TAB_BOX_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(13, -3, new Point(0, 0), new Rectangle(0, 0, ATTACK_STYLE_CHOICE_WIDTH, ATTACK_STYLE_CHOICE_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(14, -3, new Point(0, 0), new Rectangle(0, 0, ATTACK_STYLE_CHOICE_WIDTH, ATTACK_STYLE_CHOICE_HEIGHT)));
		Player.equipmentSlots[count++] = (new InventorySlot(15, -3, new Point(0, 0), new Rectangle(0, 0, ATTACK_STYLE_CHOICE_WIDTH, ATTACK_STYLE_CHOICE_HEIGHT)));
		Player.equipmentSlots[12].mouseClicked = true; // set accurate to default attack style
	}

	public static enum TAB {
		MINIMIZED, BACKPACK, EQUIPMENT, SKILLS, MAGIC
	}

	public static final void changeSettings() {

		maximizedBounds.x = (int) (Game.game.getWidth() - SECONDARY_HUD_WIDTH - Game.game.getWidth() * 0.005f);
		maximizedBounds.y = (int) (Game.game.getHeight() - SECONDARY_HUD_HEIGHT - Game.game.getHeight() * 0.005f);
		minimizedBounds.x = (int) (Game.game.getWidth() - SECONDARY_HUD_WIDTH - Game.game.getWidth() * 0.005f);
		minimizedBounds.y = (int) (Game.game.getHeight() - MINIMIZED_HEIGHT - Game.game.getHeight() * 0.005f);

		if (!(tab == TAB.MINIMIZED)) {
			for (final InventorySlot i : Player.tabSlots) {
				if (i.slotNumber == MINIMIZE_BUTTON) {
					i.pos.x = 192;
					i.pos.y = -255;
					i.bounds.x = (int) (i.pos.x + Game.game.getWidth() - SECONDARY_HUD_WIDTH - Game.game.getWidth() * 0.005f);
					i.bounds.y = (int) (i.pos.y + Game.game.getHeight() - MINIMIZED_HEIGHT - Game.game.getHeight() * 0.005f);
					i.bounds.width = MINIMIZED_BUTTON_WIDTH;
					i.bounds.height = MINIMIZED_BUTTON_HEIGHT;
				} else {
					i.pos.x = 25 + (i.slotNumber - 1) * TAB_BOX_WIDTH;
					i.pos.y = 23;
					i.bounds.x = (int) (i.pos.x + Game.game.getWidth() - SECONDARY_HUD_WIDTH - Game.game.getWidth() * 0.005f);
					i.bounds.y = (int) (i.pos.y + Game.game.getHeight() - SECONDARY_HUD_HEIGHT - Game.game.getHeight() * 0.005f);
				}

				for (final InventorySlot e : Player.equipmentSlots) {
					e.bounds.x = (int) (Game.game.getWidth() - Game.game.getWidth() * 0.005f - SECONDARY_HUD_WIDTH + e.pos.x);
					e.bounds.y = (int) (Game.game.getHeight() - Game.game.getHeight() * 0.005f - SECONDARY_HUD_HEIGHT + e.pos.y);
					if (e.slotNumber < 4) {
						e.pos.x = 26 + (e.slotNumber - 1) * TAB_BOX_WIDTH;
						e.pos.y = 76;
					} else if (e.slotNumber > 3 && e.slotNumber < 7) {
						e.pos.x = 26 + (e.slotNumber - 4) * TAB_BOX_WIDTH;
						e.pos.y = 118;
					} else if (e.slotNumber > 6 && e.slotNumber < 10) {
						e.pos.x = 26 + (e.slotNumber - 7) * TAB_BOX_WIDTH;
						e.pos.y = 160;
					} else if (e.slotNumber > 9 && e.slotNumber < 13) {
						e.pos.x = 26 + (e.slotNumber - 10) * TAB_BOX_WIDTH;
						e.pos.y = 202;
					} else if (e.slotNumber == 13) {
						e.pos.x = 28; e.pos.y = 257;
					} else if (e.slotNumber == 14) {
						e.pos.x = 93; e.pos.y = 257;
					} else if (e.slotNumber == 15) {
						e.pos.x = 158; e.pos.y = 257;
					}
				}
			}
		} else {
			for (final InventorySlot i : Player.tabSlots) {
				if (i.slotNumber == 5)  i.bounds.width = i.bounds.height = 0;
				else {
					i.pos.x = 25 + (i.slotNumber - 1) * TAB_BOX_WIDTH - 12;
					i.pos.y = 23 - 17;
					i.bounds.x = (int) (i.pos.x + Game.game.getWidth() - SECONDARY_HUD_WIDTH - Game.game.getWidth() * 0.005f);
					i.bounds.y = (int) (i.pos.y + Game.game.getHeight() - MINIMIZED_HEIGHT - Game.game.getHeight() * 0.005f);
				}
			}
		}
	}

	private static long equipmentTimer, magicTimer, backPackTimer, skillsTimer;
	private static final int TAB_DELAY = 75;
	public static final void onLeftClick() {
		for (final InventorySlot i : Player.tabSlots) {
			if (i.bounds.contains(MouseInput.mouseXY)) {
				resetAllMouseClicked();
				
				i.mouseClicked = true;
				switch (i.slotNumber) {
					case 1: tab = TAB.BACKPACK; backPackTimer = System.currentTimeMillis(); break;
					case 2: tab = TAB.EQUIPMENT; equipmentTimer = System.currentTimeMillis(); break;
					case 3: tab = TAB.SKILLS; skillsTimer = System.currentTimeMillis(); break;
					case 4: tab = TAB.MAGIC; magicTimer = System.currentTimeMillis(); break;
					case 5: tab = TAB.MINIMIZED;
				}
			}
		}

		if (tab == TAB.EQUIPMENT) {
			if (System.currentTimeMillis() - equipmentTimer > TAB_DELAY) {
				// Attack style clicks:
				for (int i = 12; i < 15; i++) 
					if (Player.equipmentSlots[i].bounds.contains(MouseInput.mouseXY)) {
						for (int e = 12; e < 15; e++) Player.equipmentSlots[e].mouseClicked = false;
						Player.equipmentSlots[i].mouseClicked = true;
					}
			}
		}
	}

	public static final void render(Graphics2D g2d) {
		if (tab == TAB.MINIMIZED) {
			g2d.translate((int) (Game.game.getWidth() - SECONDARY_HUD_WIDTH - Game.game.getWidth() * 0.005),
					(int) (Game.game.getHeight() - MINIMIZED_HEIGHT - Game.game.getHeight() * 0.005));
			g2d.drawImage(Textures.secondaryHUDSheet[8], 0, 0, null);

			for (final InventorySlot i : Player.tabSlots) i.render(g2d);

			g2d.drawImage(Textures.secondaryHUDSheet[4], 25 - 12, 23 - 17, null);
			g2d.drawImage(Textures.secondaryHUDSheet[5], 75 - 12, 23 - 17, null);
			g2d.drawImage(Textures.secondaryHUDSheet[6], 125 - 12, 23 - 17, null);
			g2d.drawImage(Textures.secondaryHUDSheet[7], 175 - 12, 23 - 17, null);

		} else {
			// DRAW MAIN
			g2d.translate((int) (Game.game.getWidth() - SECONDARY_HUD_WIDTH - Game.game.getWidth() * 0.005),
					(int) (Game.game.getHeight() - SECONDARY_HUD_HEIGHT - Game.game.getHeight() * 0.005));

			if (tab == TAB.BACKPACK) g2d.drawImage(Textures.secondaryHUDSheet[0], 0, 0, null);
			else if (tab == TAB.EQUIPMENT) g2d.drawImage(Textures.secondaryHUDSheet[44], 0, 0, null);
			else g2d.drawImage(Textures.secondaryHUDSheet[0], 0, 0, null); //TODO: fix this for skills/magic

			// Render secondaryHUD tab icon boxes
			for (InventorySlot i : Player.tabSlots)
				i.render(g2d);

			// draw the four tab icons
			g2d.drawImage(Textures.secondaryHUDSheet[4], 25, 23, null);
			g2d.drawImage(Textures.secondaryHUDSheet[5], 75, 23, null);
			g2d.drawImage(Textures.secondaryHUDSheet[6], 125, 23, null);
			g2d.drawImage(Textures.secondaryHUDSheet[7], 175, 23, null);

			// EQUIPMENT:
			if (tab == TAB.EQUIPMENT) {
				for (InventorySlot i : Player.equipmentSlots) {
					i.render(g2d);
				}

				int adjustedColor = 240, red = 240;
				if (Player.weight >= 20) {
					if (Player.weight >= 140) red = 255;
					adjustedColor = (int) (240 - (Player.weight - 20) * 2.15);
					if (adjustedColor <= 0) adjustedColor = 0;
				}
				g2d.setColor(new Color(red, adjustedColor, adjustedColor));
				g2d.setFont(fonts.Gregorian10);
				g2d.drawString(Integer.toString((int) Player.weight) + " kg", 200, 311);
			}

			if (tab == TAB.BACKPACK) {

			}
		}
	}
	
	private static final void resetAllMouseClicked() { for (InventorySlot x : Player.tabSlots) x.mouseClicked = false; }

	public static final void cleanUp() {
		Player.tabSlots = null;
		Player.equipmentSlots = null;
	}
}
