package info.evshiron.ingresscraft.entities;

import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
import info.evshiron.ingresscraft.Constants;
import info.evshiron.ingresscraft.IngressCraft;
import info.evshiron.ingresscraft.items.ScannerItem;
import info.evshiron.ingresscraft.items.XMPBursterItem;
import info.evshiron.ingresscraft.utils.IngressHelper;
import info.evshiron.ingresscraft.utils.IngressNotifier;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.*;
import net.minecraft.world.World;

import java.util.List;

/**
 * Created by evshiron on 5/26/15.
 */
public class PortalEntity extends IngressEntityBase implements IEntityAdditionalSpawnData {

    public static final String NAME = "portal";

    public int Faction = Constants.Faction.NEUTRAL;
    public String Owner = "NIA";
    public EntityPlayer AttackingAgent = null;

    public PortalEntity(World world) {

        super(world);

    }

    public void SetFaction(int faction) {

        Faction = faction;

    }

    public void SetOwner(String owner) {

        Owner = owner;

    }

    public int GetLevel() {

        int levels = 0;

        List<ResonatorEntity> resonators = IngressHelper.GetEntitiesAround(worldObj, ResonatorEntity.class, this, IngressCraft.CONFIG_PORTAL_RANGE);

        for(int i = 0; i < resonators.size(); i++) {

            ResonatorEntity resonator = resonators.get(i);

            levels += resonator.Level;

        }

        return resonators.size() == 0 ? 0 : levels / resonators.size();

    }

    @Override
    public void writeEntityToNBT(NBTTagCompound nbt) {

        nbt.setInteger("faction", Faction);
        nbt.setString("owner", Owner);

        super.writeEntityToNBT(nbt);

    }

    @Override
    public void readEntityFromNBT(NBTTagCompound nbt) {

        super.readEntityFromNBT(nbt);

        Faction = nbt.getInteger("faction");
        Owner = nbt.getString("owner");

    }

    @Override
    public void writeSpawnData(ByteBuf buffer) {

        ByteBufUtils.writeUTF8String(buffer, String.valueOf(Faction));
        ByteBufUtils.writeUTF8String(buffer, Owner);

    }

    @Override
    public void readSpawnData(ByteBuf additionalData) {

        Faction = Integer.parseInt(ByteBufUtils.readUTF8String(additionalData));
        Owner = ByteBufUtils.readUTF8String(additionalData);

    }

    @Override
    public boolean shouldRenderInPass(int pass) {

        // 0 = Opaque, 1 = Translucent.
        return pass == 1;

    }

    @Override
    public void onLivingUpdate() {

        List resonators = IngressHelper.GetEntitiesAround(worldObj, ResonatorEntity.class, this, IngressCraft.CONFIG_PORTAL_RANGE);

        if (Faction != Constants.Faction.NEUTRAL && AttackingAgent != null && resonators.size() == 0) {

            onDeath(new EntityDamageSource(IngressCraft.MODID + ":xmpBurster", AttackingAgent));

        }

    }

    public boolean attackEntityFrom(DamageSource source, float damage) {

        if(source.getEntity() instanceof EntityPlayer) {

            EntityPlayer player = (EntityPlayer) source.getEntity();

            ItemStack scanner = player.getCurrentArmor(3);

            if(!(scanner.getItem() instanceof ScannerItem)) {

                return false;

            }

            if(source.getDamageType().contentEquals(IngressCraft.MODID + ":xmpBurster")) {

                AttackingAgent = player;

                return true;

            }
            else {

                if(worldObj.isRemote) return false;

                NBTTagCompound nbt = scanner.getTagCompound();

                int times = rand.nextInt(4);
                for(int i = 0; i < times; i++) {

                    int type = rand.nextInt(2);

                    int amount;
                    ItemStack itemStack;
                    NBTTagCompound nbt1;

                    switch(type) {
                        case 0: // Resonator.

                            amount = rand.nextInt(3 + GetLevel() / 2 + (nbt.getInteger("faction") == Faction ? 2 : 0));

                            itemStack = new ItemStack(IngressCraft.ResonatorItem, amount, 0);
                            nbt1 = new NBTTagCompound();
                            // Math.max let level >= 1, Math.min etc. let level in [x-2, x+2].
                            nbt1.setInteger("level", Math.max(1, Math.min(nbt.getInteger("level"), GetLevel()) - 2 + rand.nextInt(5)));
                            itemStack.setTagCompound(nbt1);

                            entityDropItem(itemStack, 0);
                            break;

                        case 1: // XMPBurster.

                            amount = rand.nextInt(3 + GetLevel() / 2 + (nbt.getInteger("faction") != Faction ? 2 : 0));

                            itemStack = new ItemStack(IngressCraft.XMPBursterItem, amount, 0);
                            nbt1 = new NBTTagCompound();
                            nbt1.setInteger("level", Math.max(1, Math.min(nbt.getInteger("level"), GetLevel()) - 2 + rand.nextInt(5)));
                            itemStack.setTagCompound(nbt1);

                            entityDropItem(itemStack, 0);
                            break;

                    }

                }

                return true;

            }

        }

        return false;

    }

    @Override
    public void onDeath(DamageSource source) {

        if(source.getEntity() instanceof EntityPlayer) {

            ItemStack scanner = ((EntityPlayer) source.getEntity()).getCurrentArmor(3);

            if(scanner.getItem() instanceof ScannerItem) {

                NBTTagCompound nbt = scanner.getTagCompound();

                if(!worldObj.isRemote) IngressNotifier.BroadcastNeutralizing(nbt);

                // Show effects.

                SetFaction(Constants.Faction.NEUTRAL);
                SetOwner("NIA");

                AttackingAgent = null;

            }

        }
        else if(source.getEntity() == null) {

            SetFaction(Constants.Faction.NEUTRAL);
            SetOwner("NIA");

        }

    }

}

@@ -2837,16 +2837,33 @@
     List
+%3CResonatorEntity%3E
  resonat
@@ -3174,24 +3174,601 @@
 ngAgent));%0A%0A
+        %7D%0A        else if(resonators.size() == 0) %7B%0A%0A            SetFaction(Constants.Faction.NEUTRAL);%0A            SetOwner(%22NIA%22);%0A%0A        %7D%0A        else if(Faction == Constants.Faction.NEUTRAL && resonators.size() %3E 0) %7B%0A%0A            int faction = resonators.get(0).Faction;%0A%0A            for(int i = 1; i %3C resonators.size(); i++) %7B%0A%0A                ResonatorEntity resonator = resonators.get(i);%0A%0A                if(resonator.Faction != faction) %7B%0A%0A                    return;%0A%0A                %7D%0A%0A            %7D%0A%0A            Faction = faction;%0A%0A        %7D%0A        else %7B%0A%0A%0A%0A
         %7D%0A%0A 

Fix Portal