package com.vanamod.mod.items;

import com.vanamod.mod.VanaMod;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityPotion;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;

public class ItemPotionLauncher extends Item {

    private String textureName;

    public ItemPotionLauncher(String name) {
        this.textureName = name;
        this.setUnlocalizedName(name);
        this.setCreativeTab(VanaMod.vanaTab);
        this.setMaxStackSize(1);
        this.setFull3D();
    }

    @Override
    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
        // Chercher une splash potion dans l'inventaire
        int potionSlot = findSplashPotionSlot(player);

        // Si pas de potion, afficher message côté client seulement
        if (potionSlot == -1) {
            if (world.isRemote) {
                player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "No splash potions in inventory!"));
            }
            return stack;
        }

        // TOUT LE RESTE SE PASSE UNIQUEMENT CÔTÉ SERVEUR
        if (!world.isRemote) {
            ItemStack potionStack = player.inventory.getStackInSlot(potionSlot);

            // Créer et lancer la potion
            EntityPotion entityPotion = new EntityPotion(world, player, potionStack.copy());

            entityPotion.setThrowableHeading(
                    player.getLookVec().xCoord,
                    player.getLookVec().yCoord,
                    player.getLookVec().zCoord,
                    1.0F,
                    1.0F
            );

            world.spawnEntityInWorld(entityPotion);

            // Consommer la potion
            potionStack.stackSize--;
            if (potionStack.stackSize <= 0) {
                player.inventory.setInventorySlotContents(potionSlot, null);
            } else {
                player.inventory.setInventorySlotContents(potionSlot, potionStack);
            }

            // Marquer l'inventaire comme modifié pour forcer la synchro
            player.inventory.markDirty();
            player.openContainer.detectAndSendChanges();
        }

        return stack;
    }

    private int findSplashPotionSlot(EntityPlayer player) {
        for (int i = 0; i < player.inventory.mainInventory.length; i++) {
            if (i == player.inventory.currentItem) continue;

            ItemStack itemStack = player.inventory.mainInventory[i];
            if (itemStack != null && itemStack.getItem() instanceof ItemPotion) {
                if (ItemPotion.isSplash(itemStack.getItemDamage())) {
                    return i;
                }
            }
        }
        return -1;
    }

    @Override
    @SideOnly(Side.CLIENT)
    public boolean isFull3D() {
        return true;
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void registerIcons(IIconRegister iconRegister) {
        this.itemIcon = iconRegister.registerIcon("vanamod:" + this.textureName);
    }
}