package client.gui.container;

import org.lwjgl.opengl.GL11;

import client.gui.element.ActButton;
import client.gui.element.ButtonCallback;
import client.gui.element.PressType;
import client.renderer.GlState;
import client.renderer.ItemRenderer;
import common.entity.Entity;
import common.inventory.ContainerMerchant;
import common.entity.npc.EntityNPC;
import common.item.ItemStack;
import common.packet.CPacketAction;
import common.village.MerchantRecipe;
import common.village.MerchantRecipeList;
import common.world.World;

public class GuiMerchant extends GuiContainer implements ButtonCallback {
	private final String title;

	private int recipe;
	private ActButton prevBtn;
	private ActButton nextBtn;

	public GuiMerchant(EntityNPC inv, Entity entity, World world) {
		super(new ContainerMerchant(inv, null, world), inv);
		this.title = entity.getName();
	}

	public void addButtons() {
		super.addButtons();
		this.nextBtn = this.button(120 + 27, 24, 12, 16, this, ">");
		this.prevBtn = this.button(36 - 19, 24, 12, 16, this, "<");
		this.slot(36 - 1, 24 - 1, 18, 18);
		this.slot(62 - 1, 24 - 1, 18, 18);
		this.slot(120 - 1, 24 - 1, 18, 18);
		this.nextBtn.enabled = false;
		this.prevBtn.enabled = false;
	}

	public void addElements() {
		this.label(this.title, 8, 16);
		this.label("Inventar", 8, 90 - 8);
	}

	public void drawOverlays() {
		super.drawOverlays();
		MerchantRecipeList merchantrecipelist = this.getRecipes();
		if(merchantrecipelist != null && !merchantrecipelist.isEmpty()) {
			int k = this.recipe;
			MerchantRecipe merchantrecipe = (MerchantRecipe)merchantrecipelist.get(k);
			ItemStack itemstack = merchantrecipe.first();
			ItemStack itemstack1 = merchantrecipe.second();
			ItemStack itemstack2 = merchantrecipe.result();
			this.renderItemOverlayIntoGUI(itemstack, 36, 24, null);
			if(itemstack1 != null)
				this.renderItemOverlayIntoGUI(itemstack1, 62, 24, null);
			this.renderItemOverlayIntoGUI(itemstack2, 120, 24, null);
		}
	}

	public void use(ActButton btn, PressType type) {
		boolean flag = false;
		MerchantRecipeList list = this.getRecipes();
		list = list != null && list.size() == 0 ? null : list;

		if(btn == this.nextBtn) {
			if(list != null && this.recipe < list.size() - 1) {
				++this.recipe;
				flag = true;
			}
		}
		else if(btn == this.prevBtn) {
			if(list != null && this.recipe > 0) {
				--this.recipe;
				flag = true;
			}
		}

		if(flag) {
			this.prevBtn.enabled = list != null && list.size() > 1 && this.recipe > 0;
			this.nextBtn.enabled = list != null && list.size() > 1 && this.recipe < list.size() - 1;
			((ContainerMerchant)this.inventorySlots).setCurrentRecipeIndex(this.recipe);
			this.gm.getNetHandler().addToSendQueue(new CPacketAction(CPacketAction.Action.SELECT_TRADE, this.recipe));
		}
	}

	public void drawScreen(int mouseX, int mouseY) {
		super.drawScreen(mouseX, mouseY);
		MerchantRecipeList merchantrecipelist = this.getRecipes();

		if(merchantrecipelist != null && !merchantrecipelist.isEmpty()) {
			int k = this.recipe;
			MerchantRecipe merchantrecipe = (MerchantRecipe)merchantrecipelist.get(k);
			ItemStack itemstack = merchantrecipe.first();
			ItemStack itemstack1 = merchantrecipe.second();
			ItemStack itemstack2 = merchantrecipe.result();
			GL11.glPushMatrix();
			ItemRenderer.enableGUIStandardItemLighting();
			GlState.disableLighting();
			GlState.enableRescaleNormal();
			GlState.enableColorMaterial();
			GlState.enableLighting();
			this.itemRender.zLevel = 100.0F;
			this.itemRender.renderItemAndEffectIntoGUI(itemstack, 36, 24);

			if(itemstack1 != null) {
				this.itemRender.renderItemAndEffectIntoGUI(itemstack1, 62, 24);
			}

			this.itemRender.renderItemAndEffectIntoGUI(itemstack2, 120, 24);
			this.itemRender.zLevel = 0.0F;
			GlState.disableLighting();

			if(this.isPointInRegion(36, 24, 16, 16, mouseX, mouseY) && itemstack != null) {
				this.renderToolTip(itemstack, mouseX, mouseY);
			}
			else if(itemstack1 != null && this.isPointInRegion(62, 24, 16, 16, mouseX, mouseY) && itemstack1 != null) {
				this.renderToolTip(itemstack1, mouseX, mouseY);
			}
			else if(itemstack2 != null && this.isPointInRegion(120, 24, 16, 16, mouseX, mouseY) && itemstack2 != null) {
				this.renderToolTip(itemstack2, mouseX, mouseY);
			}

			GL11.glPopMatrix();
			GlState.enableLighting();
			GlState.enableDepth();
			ItemRenderer.enableStandardItemLighting();
		}
	}

	public void setRecipes(MerchantRecipeList recipes) {
		((ContainerMerchant)this.inventorySlots).getMerchantInventory().setRecipes(recipes);
		this.recipe = 0;
		this.prevBtn.enabled = false;
		this.nextBtn.enabled = recipes != null && recipes.size() > 1;
	}

	private MerchantRecipeList getRecipes() {
		return ((ContainerMerchant)this.inventorySlots).getMerchantInventory().getRecipes();
	}
}
