package projectzombie.tiles;

import java.util.Random;

import gl_engine.vec.Vec2i;
import gl_engine.vec.Vec3d;
import projectzombie.Main;
import projectzombie.entity.Entity;
import projectzombie.entity.EntityItem;
import projectzombie.init.Items;
import projectzombie.init.Models;
import projectzombie.items.modifier.ItemModifierMeta;
import projectzombie.model.Model;
import projectzombie.util.ItemStack;
import projectzombie.util.TileState;
import projectzombie.util.math.random.RandomHelpers;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;

public class TileChest extends Tile implements TileBulletBreakable
{
	public static final short CHEST_CAVES = 1;
	public static final short CHEST_LAVA_CAVES = 2;

	public TileChest() {
		
		this.tileSolid = true;
		this.tileHitbox = 0.4;
		
	}
	
	private void spawnItem(Chunk chunk, Vec2i pos, ItemStack stack) {
		chunk.spawnEntity(new EntityItem(pos.xny().toDouble(), new Vec3d(0, 0, 0), stack));
	}
	
	@Override
	public boolean onActivated(Layer layer, Vec2i tpos, Entity entity, TileState state) {
		super.onActivated(layer, tpos, entity, state);
		
		Chunk chunk = layer.getChunk(tpos);
		Random rand = Main.rand;
		
		if(state.meta == CHEST_CAVES)
		{
			// Ammo
			spawnItem(chunk, tpos, new ItemStack(Items.AMMO, RandomHelpers.randrange(rand, 100), new ItemModifierMeta(1)));
			
			// Tnt
			spawnItem(chunk, tpos, new ItemStack(Items.TNT, RandomHelpers.randrange(rand, 2), new ItemModifierMeta(10)));
			
			// Flare
			spawnItem(chunk, tpos, new ItemStack(Items.FLARE, RandomHelpers.randrange(rand, 5)));
			
			// Lantern
			spawnItem(chunk, tpos, new ItemStack(Items.LANTERN, RandomHelpers.randrange(rand, 5)));
			
			// Health potions
			spawnItem(chunk, tpos, new ItemStack(Items.HEALTH_POTION, RandomHelpers.randrange(rand, 4), new ItemModifierMeta(50)));
			
		}
		
		if(state.meta == CHEST_LAVA_CAVES)
		{
			// Ammo
			spawnItem(chunk, tpos, new ItemStack(Items.AMMO, RandomHelpers.randrange(rand, 250), new ItemModifierMeta(1)));
			
			// Tnt
			spawnItem(chunk, tpos, new ItemStack(Items.TNT, RandomHelpers.randrange(rand, 2), new ItemModifierMeta(10)));
			
			// Health potions
			spawnItem(chunk, tpos, new ItemStack(Items.HEALTH_POTION, RandomHelpers.randrange(rand, 4), new ItemModifierMeta(50)));
			
			// Flare
			spawnItem(chunk, tpos, new ItemStack(Items.FLARE, RandomHelpers.randrange(rand, 5)));
			
			// Lantern
			spawnItem(chunk, tpos, new ItemStack(Items.LANTERN, RandomHelpers.randrange(rand, 5)));
			
		}
		
		layer.breakFrontTile(tpos);
		
		return true;
	}
	
	@Override
	public int getBulletBreakChance() {
		return 25;
	}

	@Override
	public Model getModel(byte meta) {
		return Models.TILE_CHEST;
	}

}
