package client.gui.container;

import common.inventory.ContainerFurnace;
import common.inventory.IInventory;
import common.inventory.InventoryPlayer;
import common.tileentity.TileEntityFurnace;

public class GuiFurnace extends GuiContainer {
	private final InventoryPlayer playerInv;
	private final IInventory furnace;

	public GuiFurnace(InventoryPlayer playerInv, IInventory furnaceInv) {
		super(new ContainerFurnace(playerInv, furnaceInv));
		this.playerInv = playerInv;
		this.furnace = furnaceInv;
	}

	public void draw() {
		super.draw();
		this.rect(58, 36, 12, 14, 0x202020);
		if(TileEntityFurnace.isBurning(this.furnace)) {
			int burn = this.getBurnLeftScaled(13);
			burn = Math.min(burn, 13);
			this.rect(58, 36 + 13 - burn, 12, burn + 1, 0xff7f00);
		}

		int progress = this.getCookProgressScaled(24);
		this.rect(79, 39, 24, 8, 0x606060);
		if(progress > 0)
			this.rect(79, 39, progress + 1, 8, 0xffaf00);
	}

	public void addElements() {
		this.label(this.furnace.getCommandName(), 8, 6);
		this.label(this.playerInv.getCommandName(), 8, this.ySize - 96 + 2);
	}

	private int getCookProgressScaled(int pixels) {
		int progress = this.furnace.getField(2);
		int total = this.furnace.getField(3);
		return total != 0 && progress != 0 ? progress * pixels / total : 0;
	}

	private int getBurnLeftScaled(int pixels) {
		int total = this.furnace.getField(1);
		return this.furnace.getField(0) * pixels / (total == 0 ? 200 : total);
	}
}
