package client.gui.container;

import client.gui.element.Bar;
import client.gui.element.DisplayLabel;
import client.gui.element.Label;
import common.inventory.ContainerTile;
import common.inventory.IInventory;
import common.entity.npc.EntityNPC;
import common.tileentity.Device;
import common.tileentity.Device.Status;

public class GuiDevice extends GuiContainer {
	private final IInventory playerInv;
	private final IInventory tileInv;
	private final Device tile;

	private Label header;
	private DisplayLabel desc;
	private Bar progress;
	private Bar temperature;
	private Bar[] resources;

	public GuiDevice(EntityNPC player, IInventory inv, Device tile) {
		super(new ContainerTile(player, tile, inv));
		this.playerInv = player;
		this.tileInv = tile;
		this.ySize = 118 + 18 * ((player.getInventoryCapacity() + 11) / 12);
		this.tile = tile;
	}

	public void updateScreen() {
		super.updateScreen();
		this.header.setText(this.tile.getStatus().color + this.tile.getBlock().getDisplay());
		this.desc.setText(this.tile.formatDisplay((ContainerTile)this.inventorySlots));
		if(this.progress != null) {
			this.progress.setText(this.tile.getTotal() <= 0 || this.tile.getProgress() < 0 ? "" : String.format("%d/%d (%.1f %%)", 
				this.tile.getProgress(), this.tile.getTotal(), ((float)this.tile.getProgress() / (float)this.tile.getTotal()) * 100.0f));
			this.progress.setProgress(this.tile.getTotal(), this.tile.getProgress());
		}
		if(this.temperature != null) {
			this.temperature.setText(String.format("t: %d °C" + (this.tile.getMaxTemp() == Integer.MAX_VALUE ? "" : " (max. %d °C)"), this.tile.getTemperature(), this.tile.getMaxTemp()));
			this.temperature.setProgress(this.tile.getMaxTemp() == Integer.MAX_VALUE ? (this.tile.getStatus() == Status.OFF || this.tile.getStatus() == Status.BROKEN ? 0 : this.tile.getTemperature()) : this.tile.getMaxTemp(), this.tile.getTemperature());
		}
		for(int z = 0; z < this.resources.length; z++) {
			this.resources[z].setText(this.tile.getResource(z).format());
			this.resources[z].setProgress(this.tile.getResource(z).getCapacity(), this.tile.getResource(z).getValue());
		}
	}

	public void addElements() {
		this.header = this.label("", 8, 16);
		this.label("Inventar", 8, 118 - 8);
		this.desc = this.display("", 8, 18, 160, 4);
		this.progress = this.tile.hasProgress() ? this.bar(7, 70, 162, 9) : null;
		this.temperature = this.tile.hasTemperature() ? this.bar(7, this.progress == null ? 70 : 60, 162, 9) : null;
		this.resources = new Bar[this.tile.getNumResources()];
		for(int z = 0; z < this.resources.length; z++) {
			this.resources[z] = this.bar(7, 80 - (this.resources.length + (this.progress == null ? 0 : 1) + (this.temperature == null ? 0 : 1)) * 10 + z * 10, 162, 9);
		}
	}
}
