package com.zivilon.cinder_loe.entity.npc.radagast;

import com.zivilon.cinder_loe.CinderLoE;

import lotr.common.LOTRMod;
import lotr.common.entity.animal.LOTREntityBear;
import lotr.common.entity.animal.LOTREntityBear.BearType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

import java.util.List;

public class FangornBear extends FangornAnimal {
    public FangornBear(World world) {
        super(world);
        setSize(1.6F, 1.8F);
    }
    private int hostileTick = 0;
  
    public void entityInit() {
        super.entityInit();
        dataWatcher.addObject(18, Byte.valueOf((byte)0));
        dataWatcher.addObject(20, Byte.valueOf((byte)0));
        setBearType(LOTREntityBear.BearType.forID(rand.nextInt((BearType.values()).length)));
    }
  
    public BearType getBearType() {
        int i = dataWatcher.getWatchableObjectByte(18);
        return BearType.forID(i);
    }
  
    public void setBearType(BearType t) {
        dataWatcher.updateObject(18, Byte.valueOf((byte)t.bearID));
    }

    protected void applyEntityAttributes() {
        super.applyEntityAttributes();
        getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(40.0D);
        getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.2D);
        getAttributeMap().registerAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(8.0D);
    }
    
    public static class BearGroupSpawnData implements IEntityLivingData {
        private BearGroupSpawnData() {}
        
        public int numSpawned = 0;
    }

    public boolean attackEntityAsMob(Entity entity) {
        float f = (float)getEntityAttribute(SharedMonsterAttributes.attackDamage).getAttributeValue();
        return entity.attackEntityFrom(DamageSource.causeMobDamage((EntityLivingBase)this), f);
    }
    public boolean attackEntityFrom(DamageSource damagesource, float f) {
        boolean flag = super.attackEntityFrom(damagesource, f);
        if (flag) {
            Entity attacker = damagesource.getEntity();
            if (attacker instanceof EntityLivingBase)
                if (isChild()) {
                    double range = 12.0D;
                    List list = ((Entity)this).worldObj.getEntitiesWithinAABBExcludingEntity((Entity)this, ((Entity)this).boundingBox.expand(range, range, range));
                    for (Object obj : list) {
                        Entity entity = (Entity)obj;
                        if (entity instanceof FangornBear) {
                            FangornBear bear = (FangornBear)entity;
                            if (!bear.isChild())
                                bear.becomeAngryAt((EntityLivingBase)attacker); 
                        } 
                    } 
                } else {
                    becomeAngryAt((EntityLivingBase)attacker);
                }    
        } 
        return flag;
    }

    private void becomeAngryAt(EntityLivingBase entity) {
        setAttackTarget(entity);
        this.hostileTick = 200;
    }
    
    public void writeEntityToNBT(NBTTagCompound nbt) {
        super.writeEntityToNBT(nbt);
        nbt.setByte("BearType", (byte)(getBearType()).bearID);
        nbt.setInteger("Angry", this.hostileTick);
    }
    
    public void readEntityFromNBT(NBTTagCompound nbt) {
        super.readEntityFromNBT(nbt);
        if (nbt.hasKey("BearType"))
            setBearType(BearType.forID(nbt.getByte("BearType"))); 
        this.hostileTick = nbt.getInteger("Angry");
    }

    public IEntityLivingData onSpawnWithEgg(IEntityLivingData data) {
        data = super.onSpawnWithEgg(data);
        if (data == null) {
            data = new BearGroupSpawnData();
            ((BearGroupSpawnData)data).numSpawned = 1;
        } else if (data instanceof BearGroupSpawnData) {
            BearGroupSpawnData bgsd = (BearGroupSpawnData)data;
            bgsd.numSpawned++;
        } 
        if (rand.nextInt(10000) == 0) setCustomNameTag("Wojtek"); 
        return data;
    }

    protected void dropFewItems(boolean flag, int i) {
        int furs = 1 + rand.nextInt(3) + rand.nextInt(i + 1);
        for (int l = 0; l < furs; l++)
            dropItem(LOTRMod.fur, 1); 
        if (flag) {
            int rugChance = 30 - i * 5;
            rugChance = Math.max(rugChance, 1);
            if (rand.nextInt(rugChance) == 0)
                entityDropItem(new ItemStack(LOTRMod.bearRug, 1, (getBearType()).bearID), 0.0F); 
        } 
    }
    protected String getLivingSound() {
        return "lotr:bear.say";
    }
      
    protected String getHurtSound() {
        return "lotr:bear.hurt";
    }
      
    protected String getDeathSound() {
        return "lotr:bear.death";
    }

    public void attackEntityWithRangedAttack(EntityLivingBase target, float distanceFactor) {
        // No ranged attack implemented
    }

    @Override
    public ItemStack getPickedResult(MovingObjectPosition target) {
        return new ItemStack(CinderLoE.spawnEgg, 1, 27);
    }
}
