package projectzombie.tiles;

import gl_engine.MathHelpers;
import gl_engine.vec.Vec2i;
import gl_engine.vec.Vec3d;
import projectzombie.entity.Entity;
import projectzombie.entity.tileentity.TileEntityBlastFurnace;
import projectzombie.entity.tileentity.TileEntityCampfire;
import projectzombie.init.Items;
import projectzombie.init.Models;
import projectzombie.init.Tiles;
import projectzombie.inventory.Inventory;
import projectzombie.items.ItemTool;
import projectzombie.items.ItemToolType;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import projectzombie.util.math.TileState;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;

public class TileBlastFurnace extends Tile
{

	public TileBlastFurnace() {
		this.emitsLight = true;
		this.tileSolid = true;
		this.tileHitbox = 0.5;
		this.hardness = 800;
	}
	
	@Override
	public double getLightLevel(TileState state, Vec2i pos) {
		return (1 - MathHelpers.squared(1 - state.meta / 16.0)) / 2.0;
	}
	
	@Override
	public Model getModel(byte meta) {
		return Models.TILE_BLAST_FURNACE;
	}
	
	@Override
	public void onActivated(Layer layer, Vec2i tpos, Entity entity, TileState state) {
		super.onActivated(layer, tpos, entity, state);
		
		TileEntityBlastFurnace te = (TileEntityBlastFurnace)layer.getTileEntity(tpos);
		
		if(!(te instanceof TileEntityBlastFurnace)) {
			te = new TileEntityBlastFurnace(tpos);
			layer.createTileEntity(tpos, te);
		}
		
		te.onOpened();
	}
	
	@Override
	public boolean canTileBreak(TileState state, ItemStack stack, ItemTool tool) {
		return true;
	}
	
	@Override
	public boolean canTileSpeedBreak(TileState state, ItemStack stack, ItemTool tool) {
		return (tool != null && tool.toolType(stack) == ItemToolType.PICK);
	}
	
	@Override
	public ItemStack[] getTileDrops(TileState state) {
		return new ItemStack[] {
				new ItemStack(Items.BLAST_FURNACE, 1),
		};
	}

	@Override
	public void onDestroy(Layer layer, Chunk chunk, TileState oldState, TileState newState, Vec2i pos) {
		super.onDestroy(layer, chunk, oldState, newState, pos);
		
		if(newState.tile == Tiles.BLAST_FURNACE) {
			return;
		}
		
		TileEntityBlastFurnace te = (TileEntityBlastFurnace)chunk.getTileEntity(pos);
		
		if(te != null) {
			te.getInventory().dropInventory(layer, chunk, pos.toDouble().add(0.5).xny(), new Vec3d(0, 0, 0));
		}
		
		chunk.destroyTileEntity(pos);
	}
	
}
