package common.tileentity;

import common.init.Blocks;
import common.init.ItemRegistry;
import common.init.Items;
import common.item.ItemStack;
import common.tileentity.MachineResource.Type;

public class TileEntityTianReactor extends TileEntityMachine {
	public TileEntityTianReactor() {
		super(2, new MachineResource(Type.OUTPUT, "output.energy", 1024, 0, 0));
	}

	protected boolean executeFunction() {
		if(!this.hasAmount(0, 2)) // && !this.isCreative)
			return false;
		if(this.rand.rarity(5))
			return true;
//		if(!this.isCreative) {
		this.decrStackSize(0, 2);
		this.decrStackSize(1, 1);
//		}
		this.getResource(0).add(this.rand.range(this.temperature / 200, this.temperature / 50), 20, false);
		return true;
	}

	protected int getTempIncrement() {
		return this.isEmpty(1) ? this.rand.range(20, 50) : (this.temperature >= this.rand.range(4200, 4250) ? 0 : this.rand.range(10, 15));
	}

	protected int getTempDecrement() {
		return this.isEmpty(1) ? this.rand.chance(0, 1, 3) : this.rand.range(1, 3);
	}

	public boolean isItemValidForSlot(int index, ItemStack stack) {
		return index == 0 ? stack.getItem() == Items.aluminium_ingot : (index == 1 ? stack.getItem() == ItemRegistry.getItemFromBlock(Blocks.lead_block) : false);
	}

	protected int getMaxTemp() {
		return 8200;
	}

	public String getName() {
		return "Tianreaktor";
	}

	public String getGuiID() {
		return "tian_reactor";
	}
	
	public String formatDisplay() {
		return String.format("Gespeicherte Energie: %d TF", this.getResource(0).getValue());
	}

	public void detonate() {
		this.worldObj.setBlockToAir(getPos());
		this.worldObj.newExplosion(this.getXPos(), this.getYPos(), this.getZPos(), 120);
	}
}
