package totemic_commons.pokefenn.item.equipment.music;

import java.util.List;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.DamageSource;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.ISpecialArmor;
import totemic_commons.pokefenn.item.equipment.EquipmentMaterials;
import totemic_commons.pokefenn.lib.Strings;
import totemic_commons.pokefenn.network.PacketHandler;
import totemic_commons.pokefenn.network.server.PacketJingle;
import totemic_commons.pokefenn.recipe.HandlerInitiation;
import totemic_commons.pokefenn.util.TotemUtil;

/**
 * Created by Pokefenn.
 * Licensed under MIT (If this is one of my Mods)
 */
public class ItemJingleDress extends ItemArmor implements ISpecialArmor
{
    //TODO: This cannot possible work when there's more than one player
    public int time = 0;
    public boolean hasSpeed = false;

    public ItemJingleDress()
    {
        super(EquipmentMaterials.jingleDress, 0, 2);
        setUnlocalizedName(Strings.RESOURCE_PREFIX + Strings.JINGLE_DRESS_NAME);
    }

    @Override
    public ArmorProperties getProperties(EntityLivingBase player, ItemStack armor, DamageSource source, double damage, int slot)
    {
        return new ArmorProperties(1, 1, 0);
    }

    @Override
    public String getArmorTexture(ItemStack stack, Entity entity, int slot, String type)
    {
        return "totemic:textures/armour/jingleDress.png";
    }

    @Override
    public void damageArmor(EntityLivingBase entity, ItemStack stack, DamageSource source, int damage, int slot)
    {
        stack.damageItem(entity.worldObj.rand.nextInt(4), entity);
    }

    @Override
    @SideOnly(Side.CLIENT)
    public EnumRarity getRarity(ItemStack par1ItemStack)
    {
        return EnumRarity.uncommon;
    }

    @Override
    public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack)
    {
        if(world.isRemote)
        {
            if(world.getWorldTime() % 20L == 0)
                if(Math.abs(player.motionX) > 0 || Math.abs(player.motionZ) > 0)
                    PacketHandler.sendToServer(new PacketJingle(player.motionX, player.motionZ));
        } else
        {
            if(world.getWorldTime() % 20L == 0)
            {
                hasSpeed = player.isPotionActive(Potion.moveSpeed);
            }

            if(time > 2 || (hasSpeed && time > 1))
            {
                time = 0;
                playMusic(world, player, itemStack, player.isSneaking());
            }
        }
    }

    public void playMusic(World world, EntityPlayer player, ItemStack itemStack, boolean isSneaking)
    {
        if(!isSneaking)
        {
            TotemUtil.playMusicFromItem(world, (int) player.posX, (int) player.posY, (int) player.posZ, HandlerInitiation.jingleDress, 0, 0);
            particlesAllAround((WorldServer)world, player.posX, player.posY, player.posZ);
        } else
        {
            TotemUtil.playMusicFromItemForCeremonySelector(player, (int) player.posX, (int) player.posY, (int) player.posZ, HandlerInitiation.jingleDress, 0);
            particlesAllAround((WorldServer)world, player.posX, player.posY, player.posZ);
        }
    }

    public void particlesAllAround(WorldServer world, double x, double y, double z)
    {
        world.func_147487_a("note", x + 0.5D, y + 0.4D, z + 0.5D, 2, 0.5D, 0.2D, 0.5D, 0.0D);
        world.func_147487_a("note", x, y + 0.4, z, 2, 0.0D, 0.2D, 0.0D, 0.0D);
        world.func_147487_a("note", x + 0.5D, y + 0.4D, z, 2, 0.0D, 0.2D, 0.0D, 0.0D);
        world.func_147487_a("note", x, y + 0.4D, z + 0.5D, 2, 0.0D, 0.2D, 0.0D, 0.0D);
    }

    @Override
    public int getArmorDisplay(EntityPlayer player, ItemStack armor, int slot)
    {
        return EquipmentMaterials.totemArmour.getDamageReductionAmount(slot);
    }

    /*

    @Override
    public int getMaximumMusic(World world, int x, int y, int z, boolean isFromPlayer, EntityPlayer player)
    {
        return 90;
    }

    @Override
    public int getMusicOutput(World world, int x, int y, int z, boolean isFromPlayer, EntityPlayer player)
    {
        return isFromPlayer && player != null && player.getCurrentArmor(3) != null ? 7 : 0;
    }

    @Override
    public int getRange(World world, int x, int y, int z, boolean isFromPlayer, EntityPlayer player)
    {
        return 8;
    }
    */

    public int getBonusMusic()
    {
        //TODO
        return 0;
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void registerIcons(IIconRegister iconRegister)
    {
        itemIcon = iconRegister.registerIcon(getUnlocalizedName().substring(getUnlocalizedName().indexOf(".") + 1));
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean shift)
    {
        int musicOutput = HandlerInitiation.jingleDress.getBaseOutput() + getBonusMusic();
        if(musicOutput < 5)
            list.add(StatCollector.translateToLocal("totemic.music.lowMelody"));
        else if(musicOutput == 6)
            list.add(StatCollector.translateToLocal("totemic.music.mediumMelody"));
        else if(musicOutput == 7)
            list.add(StatCollector.translateToLocal("totemic.music.highMelody"));
        else if(musicOutput > 7)
            list.add(StatCollector.translateToLocal("totemic.music.veryHighMelody"));
    }
}

@@ -734,24 +734,65 @@
 ecialArmor;%0A
+import totemic_commons.pokefenn.Totemic;%0A
 import totem
@@ -1574,16 +1574,59 @@
 _NAME);%0A
+        setCreativeTab(Totemic.tabsTotem);%0A
     %7D%0A%0A 

Add Jingle Dress to Totemic creative tab