package projectzombie.tiles;

import gl_engine.vec.Vec2i;
import gl_engine.vec.Vec3d;
import projectzombie.entity.Entity;
import projectzombie.entity.EntityItem;
import projectzombie.init.Items;
import projectzombie.init.Models;
import projectzombie.init.Tiles;
import projectzombie.items.ItemTool;
import projectzombie.model.Model;
import projectzombie.util.ItemStack;
import projectzombie.util.TileState;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;

public class TileHemp extends Tile
{
	public TileHemp() {
		this.hardness = 5000;
	}
	
	@Override
	public void tickRandomly(Layer layer, Chunk chunk, TileState state, Vec2i pos) {
		super.tickRandomly(layer, chunk, state, pos);
		
		if(Math.random() > 0.9) {
			if(state.meta < 7) {
				layer.setFrontTile(new TileState(this, state.meta + 1), pos);
			}
		}
	}

	@Override
	public Model getModel(byte meta)
	{
		if(meta >= Models.TILE_HEMP.length) {
			return Models.TILE_HEMP[7];
		}
		
		return Models.TILE_HEMP[meta];
	}
	
	@Override
	public boolean onActivated(Layer layer, Vec2i tpos, Entity entity, TileState state) {
		super.onActivated(layer, tpos, entity, state);
		
		if(state.meta > 3)
		{
			int seeds = (int)(Math.random() * (state.meta - 4)) + (state.meta - 4);
			int fibre = (int)(Math.random() * (state.meta - 3) * 2) + (state.meta - 3) * 2;
			
			layer.spawnEntity(new EntityItem(tpos.toDouble().xny(), new Vec3d(0, 0, 0), new ItemStack(
					Items.HEMP_SEED, seeds)));
			
			layer.spawnEntity(new EntityItem(tpos.toDouble().xny(), new Vec3d(0, 0, 0), new ItemStack(
					Items.PLANT_FIBRE, fibre)));
			
			layer.setFrontTile(new TileState(Tiles.HEMP, (byte)3), tpos);
			
			return true;
		}
		
		return false;
	}
	
	@Override
	public boolean canTileBreak(TileState state, ItemStack stack, ItemTool tool) {
		return true;
	}
}
