package com.zivilon.cinder_loe.items;

import com.zivilon.cinder_loe.CinderAchievement;
import com.zivilon.cinder_loe.CinderLoE;
import com.zivilon.cinder_loe.LoECreativeTabs;
import com.zivilon.cinder_loe.potion.LoEPotions;

import lotr.common.LOTRAchievement;
import lotr.common.LOTRLevelData;
import lotr.common.LOTRReflection;
import lotr.common.item.LOTRItemMug;
import lotr.common.item.LOTRItemMug.Vessel;
import lotr.client.render.LOTRDrinkIcons;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class LoEItemMug extends LOTRItemMug {
    public boolean is_toxic = false;

    @SideOnly(Side.CLIENT)
    public IIcon[] drinkIcons;

    @SideOnly(Side.CLIENT)
    public IIcon liquidIcon;

    public LoEItemMug(boolean full, boolean food) {
        super(full, food, false, 0.0F);
    }

    public LoEItemMug(float alc) {
        this(true, false, true, alc);
    }

    public LoEItemMug(boolean full, boolean food, boolean brew, float alc) {
        super(full, food, brew, alc);
        setCreativeTab(LoECreativeTabs.tabFoodLoE);
    }

    public void setTextureNameFromUnlocalizedName() {
        String textureName = getUnlocalizedName().substring("item.".length()); // Strip the "item." prefix
        this.setTextureName(textureName);
    }

    public LoEItemMug addPotionEffect(int effect_id, int seconds) {
        return (LoEItemMug)super.addPotionEffect(effect_id, seconds);
    }
    public LoEItemMug addPotionEffect(int effect_id, int seconds, int amplifier) {
        this.potionEffects.add(new PotionEffect(effect_id, seconds * 20, amplifier));
        return this;
    }

    public LoEItemMug setDrinkStats(int i, float f) {
        return (LoEItemMug)super.setDrinkStats(i, f);
    }
    public void registerIcons(IIconRegister iconregister) {
        if (this.isFullMug) {
            this.drinkIcons = new IIcon[(Vessel.values()).length];
            for (int i = 0; i < (Vessel.values()).length; i++) {
                this.drinkIcons[i] = LOTRDrinkIcons.registerDrinkIcon(iconregister, this, getIconString(), (Vessel.values()[i]).name);
            }
            this.liquidIcon = LOTRDrinkIcons.registerLiquidIcon(iconregister, this, getIconString());
            barrelGui_emptyBucketSlotIcon = iconregister.registerIcon("lotr:barrel_emptyBucketSlot");
            barrelGui_emptyMugSlotIcon = iconregister.registerIcon("lotr:barrel_emptyMugSlot");
        } else {
            super.registerIcons(iconregister);
        }
    }

    public IIcon getIconFromDamage(int i) {
        if (this.isFullMug) {
            if (i == -1)
                return this.liquidIcon;
            int vessel = (getVessel(i)).id;
            return this.drinkIcons[vessel];
        }
        return super.getIconFromDamage(i);
    }

    public static Vessel getVessel(int damage) {
        int i = damage / vesselMeta;
        return Vessel.forMeta(i);
    }
    public LoEItemMug toxic() {
        is_toxic = true;
        return this;
    }
    public ItemStack onEaten(ItemStack itemstack, World world, EntityPlayer entityplayer) {
        if (is_toxic) {
            increment_toxin(entityplayer);

        }
        if (this == CinderLoE.mugHumanBrew) {
            LOTRLevelData.getData(entityplayer).addAchievement(CinderAchievement.spiceHumanBrew);
            for (int i = 0; i < Potion.potionTypes.length; ++i) {
                Potion potion = Potion.potionTypes[i];
                if (potion == null || !LOTRReflection.isBadEffect(potion)) continue;
                entityplayer.removePotionEffect(potion.id);
            }
        }
        if (this == CinderLoE.mugDwarfBrew)
            LOTRLevelData.getData(entityplayer).addAchievement(CinderAchievement.spiceDwarfBrew);
        if (this == CinderLoE.mugOrcBrew)
            LOTRLevelData.getData(entityplayer).addAchievement(CinderAchievement.spiceOrcBrew);
        if (this == CinderLoE.mugElfBrew)
            LOTRLevelData.getData(entityplayer).addAchievement(CinderAchievement.spiceElfBrew);

        return super.onEaten(itemstack, world, entityplayer);
    }
    public void increment_toxin(EntityPlayer player) {
        int effect_duration = 7200; // 360 seconds * 20 ticks
        int effect_potency = 0; // Default to level 1 effect
        PotionEffect potion = player.getActivePotionEffect(LoEPotions.overdose);
        if (potion != null) effect_potency = potion.getAmplifier() + 1;
        player.addPotionEffect(new PotionEffect(LoEPotions.overdose.id, effect_duration, effect_potency));
        if (effect_potency == 2)
            LOTRLevelData.getData(player).addAchievement(CinderAchievement.Overdose);
    }
}
