package common.tileentity;

import common.entity.types.EntityLiving;
import common.init.EntityRegistry;
import common.inventory.ContainerTile;
import common.item.ItemStack;
import common.item.spawner.ItemMonsterPlacer;
import common.item.spawner.ItemNpcSpawner;
import common.tags.TagObject;
import common.vars.Vars;

public class TileEntityMobSpawner extends TileEntityDevice implements ITickable
{
    public TileEntityMobSpawner() {
		super(1);
	}

	private int spawnDelay = 20;
    private int minSpawnDelay = 200;
    private int maxSpawnDelay = 800;
    private int spawnRange = 4;

	public boolean isItemValidForSlot(int index, ItemStack stack) {
		return index == 0 ? stack.getItem() instanceof ItemMonsterPlacer || stack.getItem() instanceof ItemNpcSpawner : false;
	}

	public String getName() {
		return "Mob-Spawner";
	}

	public String getGuiID() {
		return "mob_spawner";
	}
	
	public String formatDisplay(ContainerTile inv) {
		ItemStack stack = inv.getSlot(0).getStack();
		if(stack == null)
			return "Kein Spawner vorhanden";
		return String.format("Erschaffe: %s", stack.getItem() instanceof ItemMonsterPlacer egg ? EntityRegistry.getEntityName(egg.getSpawnedId()) : ((stack.getItem() instanceof ItemNpcSpawner egg ? egg.getCharName() : "<?>")));
	}
    
    protected boolean executeFunction() {
		if(!Vars.mobs || !Vars.spawners || !this.hasAmount(0, 1))
			return false;
        if (this.spawnDelay == -1)
            this.resetTimer();
        if (this.spawnDelay > 0) {
            --this.spawnDelay;
            return true;
        }
    	ItemStack stack = this.getStackInSlot(0);
        if(stack == null)
            return true;
        double x = (double)this.pos.getX() + (this.worldObj.rand.doublev() - this.worldObj.rand.doublev()) * (double)this.spawnRange + 0.5D;
        double y = (double)(this.pos.getY() + this.worldObj.rand.zrange(3) - 1);
        double z = (double)this.pos.getZ() + (this.worldObj.rand.doublev() - this.worldObj.rand.doublev()) * (double)this.spawnRange + 0.5D;
        EntityLiving entity = null;
    	if(stack.getItem() instanceof ItemMonsterPlacer egg)
    		entity = ItemMonsterPlacer.spawnCreature(this.worldObj, egg.getSpawnedId(), x, y, z, true);
    	else if(stack.getItem() instanceof ItemNpcSpawner egg)
    		entity = ItemNpcSpawner.spawnNpc(this.worldObj, egg.getSpawnedChar(), x, y, z, true);
        if (entity == null)
            return true;
        this.decrStackSize(0, 1);
        entity.spawnExplosionParticle();
        this.resetTimer();
        return true;
    }

    private void resetTimer()
    {
        if (this.maxSpawnDelay <= this.minSpawnDelay)
        {
            this.spawnDelay = this.minSpawnDelay;
        }
        else
        {
            int i = this.maxSpawnDelay - this.minSpawnDelay;
            this.spawnDelay = this.minSpawnDelay + this.worldObj.rand.zrange(i);
        }
    }

    public void readTags(TagObject nbt)
    {
        super.readTags(nbt);
        this.spawnDelay = nbt.getShort("Delay");

        if (nbt.hasShort("MinSpawnDelay"))
        {
            this.minSpawnDelay = nbt.getShort("MinSpawnDelay");
            this.maxSpawnDelay = nbt.getShort("MaxSpawnDelay");
        }

        if (nbt.hasShort("SpawnRange"))
        {
            this.spawnRange = nbt.getShort("SpawnRange");
        }
    }

    public void writeTags(TagObject nbt)
    {
        super.writeTags(nbt);
        nbt.setShort("Delay", (short)this.spawnDelay);
        nbt.setShort("MinSpawnDelay", (short)this.minSpawnDelay);
        nbt.setShort("MaxSpawnDelay", (short)this.maxSpawnDelay);
        nbt.setShort("SpawnRange", (short)this.spawnRange);
    }
}
