package hud.constructionmenu;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import javax.swing.JOptionPane;

import _texture.loader.Textures;
import engine.Game;
import engine.Handler;
import engine.MouseInput;
import hud.inventory.Inventory;
import player.MouseMovement;
import structures.Fire;
import utils.Point2f;
import world.Tile;

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

public final class ConstructionItem {
	
	public static boolean PLACING;
	public static ConstructionItem TEMP;

	public int menuCategory, row, col, itemId; /* menuCat is numbered from 0-7 */
	public Cost[] price;
	public boolean unlocked, hovered, selected;
	public Rectangle bounds;
	
	public ConstructionItem(int menuCategory, int col, int row, int itemId, boolean unlocked, Cost... args){
		this.row = row; this.col = col;
		this.itemId = itemId; this.unlocked = unlocked;
		this.menuCategory = menuCategory;
		this.bounds = new Rectangle(0, 0, ConstructionMenuCategory.MENUBOXWIDTH, ConstructionMenuCategory.MENUBOXHEIGHT);
		this.price = new Cost[args.length];
		for (int i = 0; i < args.length; i++) this.price[i] = args[i];
	} public ConstructionItem(int menuCategory, int col, int row, int itemId, Cost... args){ new ConstructionItem(menuCategory, col, row, itemId, true, args); }
	
	public synchronized final void createItem(){
		if(!unlocked) return;
		
		for(int i = 0; i < price.length; i++) if(!Inventory.contains(price[i].itemId, price[i].quantity)) return;
		
		if(!calcIsStructure()){
			for(int i = 0; i < price.length; i++) Inventory.removeItem(price[i].itemId, price[i].quantity);
			Inventory.addItem(itemId, 1, 100);
		} else { TEMP = this; startStructureSequence(); }
		
		ConstructionMenu.changeSettings();
	}
	
	private synchronized final void startStructureSequence(){
		PLACING = Tile.drawHighLighted = true;
		
		//REMOVE ITEMS FOR COST
		for(int i = 0; i < price.length; i++) Inventory.removeItem(price[i].itemId, price[i].quantity);
	}
	
	private static Point POINT;
	public synchronized final static void placeStructure(){
		POINT = Tile.pointToCheck;
		
		switch(TEMP.itemId){
			case 8: Handler.structures.add(new Fire(new Point2f(POINT.x, POINT.y))); break;
			default: JOptionPane.showMessageDialog(null, "Problem with this structure... Yell at the developer");
		}
		
		PLACING = Tile.drawHighLighted = false;
		Tile.resetAllHighlighted();
	}
	
	private static Point p;
	public static final void render(Graphics2D g2d){
		if(!PLACING) return;
		
		if(MouseMovement.PLACING_STRUCTURE) p = Tile.pointToCheck; else p = MouseInput.adjustedMouseXY;
		
		AffineTransform transform = new AffineTransform(); Graphics2D copy = (Graphics2D) g2d.create();
		transform.rotate(-0.78539816 * (Game.rotation - 1), p.x, p.y); copy.transform(transform);
		
		switch(TEMP.itemId){
			case 8: copy.drawImage(Textures.structureSheet[0], p.x - Fire.WIDTH/2, p.y - Fire.HEIGHT /2 , null);
		}
	}
	
	
	private final boolean calcIsStructure(){
		switch(itemId){
			case 8: return true;
			
			default: return false;
		}
	}
}
