package hud.constructionmenu;

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

import _texture.loader.Textures;
import engine.Game;
import engine.MouseInput;
import interfaces.Colors;
import utils.Utils;

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

public final class ConstructionMenuCategory implements Colors{
	
	public static final int MENUWIDTH = 140, MENUHEIGHT = 386, MENUBOXWIDTH = 40, MENUBOXHEIGHT = 40;
	
	public static ConstructionMenuCategory[] categories;
	public ConstructionItem[] items;
	public int x, y;
	public static int width = 46, height = 45;
	public boolean hovered, selected, unlocked;
	public Rectangle bounds = new Rectangle(0, 0, 46, 45);
	public static final Rectangle menuBounds = new Rectangle(0, 0, MENUWIDTH, MENUHEIGHT);
	
	public ConstructionMenuCategory(){ items = new ConstructionItem[27]; }
	
	public static synchronized final void init(){
		categories = new ConstructionMenuCategory[8];
		categories[0] = new ConstructionMenuCategory();
		categories[1] = new ConstructionMenuCategory();
		categories[2] = new ConstructionMenuCategory();
		categories[3] = new ConstructionMenuCategory();
		categories[4] = new ConstructionMenuCategory();
		categories[5] = new ConstructionMenuCategory();
		categories[6] = new ConstructionMenuCategory();
		categories[7] = new ConstructionMenuCategory();
		categories[0].unlocked = true;
		categories[1].unlocked = true;
		changeSettings();
		
		CONSTRUCTION_ITEMS.createItems(categories);
	}
	
	public static final void changeSettings(){
		for(int i = 0; i < categories.length; i++){
			categories[i].x = ConstructionMenu.x + 15;
			categories[i].y = ConstructionMenu.y + (height * i) + 5*i + 22;
			categories[i].bounds.x = categories[i].x;
			categories[i].bounds.y = categories[i].y;
		}
		categories[7].y = ConstructionMenu.y + 354 + 22; /* last one is off a bit */
		categories[7].bounds.x = categories[7].x; categories[7].bounds.y = categories[7].y;
		
		
		try{
		for(final ConstructionMenuCategory cmg : categories){
			for(int i = 0, count = 0; i < 9; i++)
				for(int j = 0; j < 3; j++, count++){
					try{
						if(cmg.selected){
							cmg.items[count].bounds.x = ConstructionMenu.x + ConstructionMenu.WIDTH + MENUBOXWIDTH * j;
							cmg.items[count].bounds.y = ConstructionMenu.y + 34 + MENUBOXHEIGHT * i;
						} else {
							cmg.items[count].bounds.x = -100; /* if not selected make their bounds not clickable */
							cmg.items[count].bounds.y = -100; // "     "
						}
					} catch (NullPointerException npe){}
				}}
		} catch(ArrayIndexOutOfBoundsException asdf){asdf.printStackTrace();}
		
		
		menuBounds.x = ConstructionMenu.x + ConstructionMenu.WIDTH - 10;
		menuBounds.y = ConstructionMenu.y + 20;
	}
	
	public final void tick(){
		if(ConstructionMenu.hidden) return;
		
		if(!selected) return; else if(!ConstructionMenu.hideBounds.contains(MouseInput.mouseXY)) selected = false;
	}
	
	public final static void onClick(){
		if(!ConstructionMenu.hideBounds.contains(MouseInput.clickPos) || ConstructionMenu.hidden) return;
		for(final ConstructionMenuCategory i : categories) if(i.bounds.contains(MouseInput.clickPos) && i.unlocked) i.selected = true; else i.selected = false;
	
		if(menuBounds.contains(MouseInput.clickPos))
			for(ConstructionMenuCategory cmg : categories)
				for(ConstructionItem ci : cmg.items)
					try{ if(ci.bounds.contains(MouseInput.clickPos)){ ci.createItem(); return; }
					} catch (NullPointerException npe){}
			
		
	}
	
	public final void render(final Graphics2D g2d){
		if(!unlocked){
			g2d.drawImage(Textures.baseConstructionSheet[3], x, y, null);
			g2d.drawImage(Textures.baseConstructionSheet[4], x, y, null);
			if(calcIcon() != 0) g2d.drawImage(Textures.baseConstructionSheet[calcIcon()], x, y, null);
			g2d.setColor(construction_menu_darken);
			g2d.fillRect(x, y, ConstructionMenu.BOXWIDTH, ConstructionMenu.BOXHEIGHT);
		} else {
			if(selected) g2d.drawImage(Textures.baseConstructionSheet[3], x, y, null);
			else if(bounds.contains(MouseInput.mouseXY)) g2d.drawImage(Textures.baseConstructionSheet[2], x, y, null);
			else g2d.drawImage(Textures.baseConstructionSheet[1], x, y, null);
			if(calcIcon() != 0) g2d.drawImage(Textures.baseConstructionSheet[calcIcon()], x, y, null);
		}
		if(Game.drawInventoryBounds){ g2d.setColor(Color.pink); g2d.drawRect(bounds.x, bounds.y, bounds.width, bounds.height); }
		
		
		if(!selected) return;
			
		g2d.drawImage(Textures.baseConstructionSheet[13], ConstructionMenu.x + ConstructionMenu.WIDTH - 10, ConstructionMenu.y + 20, null);
		
		int count = 0;
		for(int i = 0; i < 9; i++)
			for(int j = 0; j < 3; j++, count++){
				try{
					if(items[count].bounds.contains(MouseInput.mouseXY)) g2d.drawImage(Textures.baseConstructionSheet[15], ConstructionMenu.x + ConstructionMenu.WIDTH + MENUBOXWIDTH * j, ConstructionMenu.y + 34 + MENUBOXHEIGHT * i, null);
					else g2d.drawImage(Textures.baseConstructionSheet[14], ConstructionMenu.x + ConstructionMenu.WIDTH + MENUBOXWIDTH * j, ConstructionMenu.y + 34 + MENUBOXHEIGHT * i, null);
				} catch (Exception e){
					g2d.drawImage(Textures.baseConstructionSheet[14], ConstructionMenu.x + ConstructionMenu.WIDTH + MENUBOXWIDTH * j, ConstructionMenu.y + 34 + MENUBOXHEIGHT * i, null);
					g2d.drawImage(Textures.baseConstructionSheet[4], ConstructionMenu.x + ConstructionMenu.WIDTH + MENUBOXWIDTH * j, ConstructionMenu.y + 34 + MENUBOXHEIGHT * i, null);
					g2d.setColor(construction_menu_darken125);
					g2d.fillRect(ConstructionMenu.x + ConstructionMenu.WIDTH + MENUBOXWIDTH * j, ConstructionMenu.y + 34 + MENUBOXHEIGHT * i, MENUBOXWIDTH, MENUBOXHEIGHT);
				}
			}
		
		if(this == categories[0]) renderTools(g2d);
		else if(this == categories[1]) renderFire(g2d);
		
		
		if(Game.drawInventoryBounds) drawInvenBounds(g2d);
		
		ConstructionMenuHover.render(g2d);
	}
	
	private final void renderTools(final Graphics2D g2d){
		g2d.drawImage(Utils.reverseImage(Textures.itemIconSheet[2]).getScaledInstance(MENUBOXWIDTH, MENUBOXHEIGHT, 2), 80, y + 12, null); // CRUDE AXE
		g2d.drawImage(Utils.reverseImage(Textures.itemIconSheet[4]).getScaledInstance(MENUBOXWIDTH, MENUBOXHEIGHT, 2), 80 + MENUBOXWIDTH, y + 11, null); // CRUDE PICKAXE
		
		int count = 0; ConstructionMenuHover.render = false;
		for(int i = 0; i < 9; i++)
			for(int j = 0; j < 3; j++, count++)
				try{if(categories[0].items[count].bounds.contains(MouseInput.mouseXY)){ ConstructionMenuHover.item = categories[0].items[count]; ConstructionMenuHover.render = true; }
				} catch (Exception e){}
	}
	
	
	private final void renderFire(final Graphics2D g2d){
		g2d.drawImage(Utils.reverseImage(Textures.itemIconSheet[9]).getScaledInstance((int)(MENUBOXWIDTH * 1.3), (int)(MENUBOXHEIGHT * 1.3), 2), 73, y - 43, null); // regular fire
		g2d.drawImage(Utils.reverseImage(Textures.baseConstructionSheet[6]).getScaledInstance(MENUBOXWIDTH, MENUBOXHEIGHT, 2), 81 + MENUBOXWIDTH, y - 38, null); // regular fire
		
		int count = 0; ConstructionMenuHover.render = false;
		for(int i = 0; i < 9; i++)
			for(int j = 0; j < 3; j++, count++)
				try{if(categories[1].items[count].bounds.contains(MouseInput.mouseXY)){ ConstructionMenuHover.item = categories[1].items[count]; ConstructionMenuHover.render = true; }
				} catch (Exception e){}
	}
	
	private final void drawInvenBounds(final Graphics2D g2d){
		g2d.setColor(Color.red); g2d.drawRect(menuBounds.x, menuBounds.y, menuBounds.width, menuBounds.height);
		
		int temp = 0; g2d.setColor(Color.cyan);
		for(int i = 0; i < 9; i++)
			for(int j = 0; j < 3; j++, temp++)
				try{ g2d.drawRect(items[temp].bounds.x, items[temp].bounds.y, items[temp].bounds.width, items[temp].bounds.height); }
				catch (Exception e) {}
	}
	
	private final int calcIcon(){
		if(this == categories[0]) return 5;
		else if(this == categories[1]) return 6;
//		else if(this == categories[2]) return 7;
//		else if(this == categories[3]) return 8;
//		else if(this == categories[4]) return 9;
//		else if(this == categories[5]) return 10;
//		else if(this == categories[6]) return 11;
//		else if(this == categories[7]) return 12;
		 
		return 0;
	}
}
