package hud.inventory;

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

import _texture.loader.Textures;
import engine.MouseInput;
import fonts.fonts;
import hud.SecondaryHUD;
import hud.SecondaryHUD.TAB;
import interfaces.Colors;
import utils.Utils;

/*  ------------- Nihilne ------------
    * Created by Mitch Weaver - 2016 *
      ~~~~ All Rights Reserved ~~~~ */

public final class InventorySlot implements Colors {

	public Point pos;
	public Rectangle bounds;
	public boolean mouseClicked, equippable;
	public int itemId, quantity, slotNumber, equipSlot, TYPE;
	public float durability;

	public InventorySlot(int slotNumber, int itemId, int quantity, Point pos, Rectangle bounds) {
		this.pos = pos;
		this.bounds = bounds;
		this.itemId = itemId;
		this.quantity = quantity;
		this.mouseClicked = false;
		this.slotNumber = slotNumber;
	}

	// tab icon slots for secondaryHUD
	public InventorySlot(int slotNumber, int TYPE, Point pos, Rectangle bounds) {
		this.pos = pos;
		this.bounds = bounds;
		this.slotNumber = slotNumber;
		this.TYPE = TYPE;
	}

	public void render(Graphics2D g2d) {
		try {
			if (TYPE == -1) {
				if (slotNumber == 5) return;

				// TAB SLOTS
				if (slotNumber == 1 && SecondaryHUD.tab == TAB.BACKPACK) {
					g2d.drawImage(Textures.secondaryHUDSheet[3], pos.x, pos.y, null);
					return;
				} else if (slotNumber == 2 && SecondaryHUD.tab == TAB.EQUIPMENT) {
					g2d.drawImage(Textures.secondaryHUDSheet[3], pos.x, pos.y, null);
					return;
				} else if (slotNumber == 3 && SecondaryHUD.tab == TAB.SKILLS) {
					g2d.drawImage(Textures.secondaryHUDSheet[3], pos.x, pos.y, null);
					return;
				} else if (slotNumber == 4 && SecondaryHUD.tab == TAB.MAGIC) {
					g2d.drawImage(Textures.secondaryHUDSheet[3], pos.x, pos.y, null);
					return;
				}

				// EQUIPMENT SLOTS --- BASE BOXES
				if (bounds.contains(MouseInput.mouseXY)) g2d.drawImage(Textures.secondaryHUDSheet[2], pos.x, pos.y, null);
				else g2d.drawImage(Textures.secondaryHUDSheet[1], pos.x, pos.y, null);
			} else if (TYPE == -3) { // EQUIPMENT

				if (slotNumber == 1) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[11], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[12], pos.x, pos.y, null);
				} else if (slotNumber == 2) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[13], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[14], pos.x, pos.y, null);
				} else if (slotNumber == 3) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[15], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[16], pos.x, pos.y, null);
				} else if (slotNumber == 4) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[17], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[18], pos.x, pos.y, null);
				} else if (slotNumber == 5) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[19], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[20], pos.x, pos.y, null);
				} else if (slotNumber == 6) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[21], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[22], pos.x, pos.y, null);
				} else if (slotNumber == 7) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[23], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[24], pos.x, pos.y, null);
				} else if (slotNumber == 8) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[25], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[26], pos.x, pos.y, null);
				} else if (slotNumber == 9) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[27], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[28], pos.x, pos.y, null);
				} else if (slotNumber == 10) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[29], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[30], pos.x, pos.y, null);
				} else if (slotNumber == 11) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[31], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[32], pos.x, pos.y, null);
				} else if (slotNumber == 12) {
					if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[33], pos.x, pos.y, null);
					else g2d.drawImage(Textures.secondaryHUDSheet[34], pos.x, pos.y, null);
				}

				if (!(itemId == 0)) {
					g2d.drawImage(Textures.itemIconSheet[itemId - 1].getScaledInstance((int) (Inventory.SLOT_WIDTH * 0.6), (int) (Inventory.SLOT_HEIGHT * 0.6),
							Image.SCALE_SMOOTH), pos.x, pos.y, null);

					if (durability < 100) {
						g2d.setColor(Color.white);
						g2d.setFont(fonts.Gregorian10);
						g2d.drawString(Float.toString(durability) + "%", pos.x + 20, pos.y + 38);
					}
				}

									// EQUIPMENT ATTACK STYLE SLOTS
									if (slotNumber == 13) {
										if (mouseClicked) g2d.drawImage(Textures.secondaryHUDSheet[37], pos.x, pos.y, null);
										else if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[35], pos.x, pos.y, null);
										else g2d.drawImage(Textures.secondaryHUDSheet[36], pos.x, pos.y, null);
									} else if (slotNumber == 14) {
										if (mouseClicked) g2d.drawImage(Textures.secondaryHUDSheet[40], pos.x, pos.y, null);
										else if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[38], pos.x, pos.y, null);
										else g2d.drawImage(Textures.secondaryHUDSheet[39], pos.x, pos.y, null);
									} else if (slotNumber == 15) {
										if (mouseClicked) g2d.drawImage(Textures.secondaryHUDSheet[43], pos.x, pos.y, null);
										else if (!(bounds.contains(MouseInput.mouseXY))) g2d.drawImage(Textures.secondaryHUDSheet[41], pos.x, pos.y, null);
										else g2d.drawImage(Textures.secondaryHUDSheet[42], pos.x, pos.y, null);
									}
			}
			
			
			/*** INVENTORY SLOTS ***/
			else if (!(itemId == 0) && TYPE >= 0) {
				g2d.drawImage(Textures.itemIconSheet[itemId - 1], pos.x, pos.y, null);

				// DONT DRAW QUANTITY OR DURABILITY IF CLICKED
				if (!mouseClicked) {

					if (InvenHelper.isStackable(itemId)) {
						g2d.setFont(fonts.Gregorian26);
						
						if(quantity < 10_000) { // UNDER 10k
							g2d.setColor(Color.white);
							g2d.drawString(Integer.toString(quantity), pos.x + 57, pos.y + 25);
						} else if(quantity < 100_000) { // 10k TO 99,999
							g2d.setColor(Color.white);
							g2d.drawString(Integer.toString(quantity / 1000) + "k", pos.x + 57, pos.y + 25);
						} else if(quantity < 1_000_000) { // 100k to 999,999
							g2d.setColor(Color.white);
							g2d.drawString(Integer.toString(quantity / 1000) + "k", pos.x + 57, pos.y + 25);
						} else if(quantity < 10_000_000) { // 1000k to 99_999_999
							g2d.setColor(Color.white);
							g2d.drawString(Integer.toString(quantity / 1000) + "k", pos.x + 57, pos.y + 25);
						} else if(quantity < 100_000_000) { // 10M to 999_999_999
							g2d.setColor(Color.green);
							g2d.drawString(Integer.toString(quantity / 1000000) + "m", pos.x + 57, pos.y + 25);
						} else if(quantity < 1_000_000_000) { // 100M to 1B
							g2d.setColor(Color.green);
							g2d.drawString(Integer.toString(quantity / 1000000) + "m", pos.x + 57, pos.y + 25);
						} else if(quantity < Utils._2147483647) { // 1000M to MAX STACK - 1
							g2d.setColor(Color.cyan);
							g2d.drawString(Integer.toString(quantity / 1000000) + "m", pos.x + 57, pos.y + 25);
						} else if(quantity == Utils._2147483647) { // MAX STACK
							g2d.setColor(pastelRed);
							g2d.drawString(Integer.toString(quantity / 1000000) + "m", pos.x + 57, pos.y + 25);
						} else if(quantity > Utils._2147483647) {
							System.out.println(" WHOA WE HAVE A PROBLEM HERE. INVEN RENDER MAX INT "); System.exit(0);
						}
						
						
					} else if (InvenHelper.isEquippable(itemId) && durability < 100) {
							g2d.setFont(fonts.Gregorian20); g2d.setColor(Color.white);
							g2d.drawString(Float.toString(durability) + "%", (int) (pos.x + Inventory.SLOT_WIDTH - Float.toString(durability).length() * 13.75), pos.y + 61);
					}
				}
			}
		} catch ( Exception e) { e.printStackTrace(); }
	}
}
