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.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;

public class ItemStickOfGod extends Item {

    private String textureName;
    private static final int COOLDOWN = 25000; // 25 secondes

    public ItemStickOfGod(String name) {
        this.textureName = name;
        this.setUnlocalizedName(name);
        this.setCreativeTab(VanaMod.vanaTab);
        this.setMaxStackSize(1);
        this.setMaxDamage(8); // 8 utilisations
        this.setFull3D(); // Rendu 3D comme un bâton/épée
    }

    @Override
    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
        if (!stack.hasTagCompound()) {
            stack.setTagCompound(new NBTTagCompound());
        }

        long currentTime = System.currentTimeMillis();
        long lastUse = stack.getTagCompound().getLong("lastUse");

        // Vérifier si le cooldown est terminé
        if (currentTime - lastUse < COOLDOWN) {
            if (world.isRemote) {
                long remaining = (COOLDOWN - (currentTime - lastUse)) / 1000;
                player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "You must wait for the stick to recharge (" + remaining + "s)"));
            }
            return stack;
        }

        // Utiliser le stick
        if (!world.isRemote) {
            // Speed I pendant 3 secondes
            player.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 60, 2)); // Speed III

            // Strength III pendant 4 secondes
            player.addPotionEffect(new PotionEffect(Potion.damageBoost.id, 80, 2));

            // Saturation II pendant 2 secondes (régénère TOUTE la vie rapidement)
            player.addPotionEffect(new PotionEffect(23, 40, 15)); // Saturation niveau très élevé

            // Regeneration V pendant 2 secondes (regen très rapide)
            player.addPotionEffect(new PotionEffect(Potion.regeneration.id, 40, 5));

            // 3 cœurs jaunes temporaires (sans effet Absorption visible)
            player.setAbsorptionAmount(6.0F); // 3 cœurs = 6 HP

            // Sauvegarder le temps d'utilisation
            stack.getTagCompound().setLong("lastUse", currentTime);

            // Endommager l'item
            stack.damageItem(1, player);
        }

        return stack;
    }

    @Override
    public boolean hasEffect(ItemStack stack) {
        // Glow seulement si pas en cooldown
        if (!stack.hasTagCompound()) {
            return true;
        }

        long currentTime = System.currentTimeMillis();
        long lastUse = stack.getTagCompound().getLong("lastUse");

        return (currentTime - lastUse) >= COOLDOWN;
    }

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