package com.zivilon.cinder_loe.items.specials;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import java.util.List;

import lotr.common.LOTRLevelData;
import lotr.common.LOTRMod;
import lotr.common.fac.LOTRFaction;
import lotr.common.network.LOTRPacketHandler;
import lotr.common.network.LOTRPacketWeaponFX;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.Potion;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;

import com.zivilon.cinder_loe.items.WizardStaff;

public class AlatarStaff extends WizardStaff {
    public AlatarStaff() {
        super();
    }

    public ItemStack onEaten(ItemStack itemstack, World world, EntityPlayer entityplayer) {
        entityplayer.swingItem();
        itemstack.damageItem(2, (EntityLivingBase)entityplayer);
        world.playSoundAtEntity((Entity)entityplayer, "lotr:elf.woodElf_teleport", 2.0F, (Item.itemRand.nextFloat() - Item.itemRand.nextFloat()) * 0.2F + 1.0F);
        LOTRFaction faction = getFaction(entityplayer);
        if (!world.isRemote) {
            List<EntityLivingBase> entities = world.getEntitiesWithinAABB(EntityLivingBase.class, ((Entity)entityplayer).boundingBox.expand(12.0D, 8.0D, 12.0D));
            if (!entities.isEmpty())
                for (int i = 0; i < entities.size(); i++) {
                    EntityLivingBase entity = entities.get(i);
                    if (entity != entityplayer) {
                        if (entity instanceof EntityLiving) {
                            EntityLiving entityliving = (EntityLiving)entity;
                            if (!faction.isGoodRelation(LOTRMod.getNPCFaction((Entity)entityliving)))
                                continue; 
                        } 
                        if (entity instanceof EntityPlayer)
                            if (!MinecraftServer.getServer().isPVPEnabled() || LOTRLevelData.getData((EntityPlayer)entity).getAlignment(faction) < 0.0F)
                                continue; 

                        entity.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 1200, 0));
                        entity.addPotionEffect(new PotionEffect(Potion.damageBoost.id, 1200, 0));
                    }
                }
            LOTRPacketWeaponFX packet = new LOTRPacketWeaponFX(LOTRPacketWeaponFX.Type.MACE_SAURON, (Entity)entityplayer);
            LOTRPacketHandler.networkWrapper.sendToAllAround((IMessage)packet, LOTRPacketHandler.nearEntity((Entity)entityplayer, 64.0D));
        } 
        return itemstack;
    }

    private LOTRFaction getFaction(EntityPlayer player) {
        LOTRFaction faction = LOTRLevelData.getData(player).getPledgeFaction();
        if (faction == null) {
            faction = LOTRFaction.DORWINION;
        }
        return faction;
    }
}