package projectzombie.tiles;

import gl_engine.vec.Vec2i;
import gl_engine.vec.Vec3d;
import projectzombie.entity.Entity;
import projectzombie.entity.EntityItem;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.entity.tileentity.TileEntityClayPot;
import projectzombie.init.Items;
import projectzombie.init.Models;
import projectzombie.init.Tiles;
import projectzombie.items.ItemTool;
import projectzombie.items.ItemToolType;
import projectzombie.items.modifier.ItemModifierFluidStorage;
import projectzombie.items.modifier.ItemModifierWater;
import projectzombie.model.Model;
import projectzombie.util.ItemStack;
import projectzombie.util.TileState;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;

public class TileClayPot extends Tile
{
	public TileClayPot() {
		tileSolid = true;
		tileHitbox = 0.3;
		hardness = 100;
	}

	@Override
	public Model getModel(byte meta)
	{
		switch(meta)
		{
		case 0: return Models.TILE_CLAY_POT_EMPTY;
		case 1: return Models.TILE_CLAY_POT_WATER_1;
		case 2: return Models.TILE_CLAY_POT_WATER_2;
		case 3: return Models.TILE_CLAY_POT_WATER_3;
		default: return Models.TILE_MISSING;
		}
	}
	
	@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 boolean onActivated(Layer layer, Vec2i tpos, Entity entity, TileState state) {
		super.onActivated(layer, tpos, entity, state);
		
		TileEntityClayPot te = (TileEntityClayPot)layer.getTileEntity(tpos);
		
		if(te == null) {
			te = new TileEntityClayPot(0);
			layer.createTileEntity(tpos, te);
		}
		
		double volume = te.getVolume();
		
		if(volume <= 0) {
			return true;
		}
		
		if(entity instanceof EntityPlayer)
		{
			EntityPlayer player = (EntityPlayer)entity;
			
			System.out.println(player.getHydration());
			
			if(player.getHydration() < 1)
			{
				player.rehydrate();
				
				volume -= 0.0005;
				
				if(volume < 0) {
					volume = 0;
				}
				
				te.setVolume(volume);
			}
		}
		
		return true;
	}
	
	@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.CLAY_POT) {
			return;
		}
		
		TileEntityClayPot te = (TileEntityClayPot)chunk.getTileEntity(pos);
		
		if(te == null) {
			return;
		}
		
		chunk.spawnEntity(new EntityItem(pos.toDouble().add(0.5).xny(),
				new Vec3d(0, 0, 0), new ItemStack(Items.CLAY_POT, 1,
				new ItemModifierWater(te.getVolume()))));
		
		te.kill();
	}

}
