package projectzombie.items;

import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec2i;
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.modifier.ItemModifierFluidStorage;
import projectzombie.items.modifier.ItemModifierMeta;
import projectzombie.items.modifier.ItemModifierWater;
import projectzombie.model.Model;
import projectzombie.model.ModelItem;
import projectzombie.util.ItemStack;
import projectzombie.util.TileState;
import projectzombie.world.layer.Layer;

public class ItemClayPot extends Item
{
	public ItemClayPot() {
		max = 1;
	}
	
	@Override
	public ModelItem getModel(ItemStack stack)
	{
		ItemModifierWater fluid = (ItemModifierWater)stack.getModifier(ItemModifierWater.class);
		
		if(fluid == null) {
			fluid = new ItemModifierWater(0);
		}
		
		switch((int)(fluid.getVolume() * 4))
		{
		case 0: return Models.ITEM_CLAY_POT_EMPTY;
		case 1: return Models.ITEM_CLAY_POT_WATER_1;
		case 2: return Models.ITEM_CLAY_POT_WATER_2;
		default: return Models.ITEM_CLAY_POT_WATER_3;
		}
	}
	
	@Override
	public Model getPlaceModel(ItemStack stack)
	{
		ItemModifierWater fluid = (ItemModifierWater)stack.getModifier(ItemModifierWater.class);
		
		if(fluid == null) {
			fluid = new ItemModifierWater(0);
		}
		
		switch((int)Math.ceil(fluid.getVolume() * 3))
		{
		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;
		default: return Models.TILE_CLAY_POT_WATER_3;
		}
	}

	@Override
	public String getName(ItemStack stack) {
		return "Clay Pot";
	}
	
	@Override
	public double getDamage(ItemStack stack)
	{
		ItemModifierWater fluid = (ItemModifierWater)stack.getModifier(ItemModifierWater.class);
		
		if(fluid == null) {
			fluid = new ItemModifierWater(0);
		}
		
		return 1 - fluid.getVolume();
	}
	
	@Override
	public boolean onPlayerRightClick(ItemStack stack, Layer layer, EntityPlayer player, Vec2d place_pos)
	{
		Vec2i tpos = place_pos.toInt();
		TileState ts = layer.getFrontTile(tpos);
		ItemModifierWater fluid = (ItemModifierWater)stack.getModifier(ItemModifierWater.class);
		
		if(fluid == null) {
			fluid = new ItemModifierWater(0);
			stack.addModifier(fluid);
		}
		
		// Increase the fluid storage if the affecting tile is water
		if(ts.tile == Tiles.WATER)
		{
			double volume = fluid.getVolume();
			
			volume += 0.001;
			
			if(volume > 1) {
				volume = 1;
			}
			
			fluid.setVolume(volume);
			
			return true;
		}
		
		// Only place if the front tile is void
		if(ts.tile != Tiles.VOID) {
			return false;
		}
		
		layer.setFrontTile(new TileState(Tiles.CLAY_POT, (byte)Math.ceil(fluid.getVolume() * 3)), tpos);
		layer.createTileEntity(tpos, new TileEntityClayPot(fluid.getVolume()));
		stack.count -= 1;
		
		return true;
	}

}
