package com.zivilon.cinder_loe.entity.ai;

import lotr.common.entity.ai.LOTREntityAIAttackOnCollide;
import lotr.common.entity.npc.LOTREntityNPC;
import lotr.common.entity.projectile.LOTREntitySpear;
import lotr.common.item.LOTRItemSpear;
import lotr.common.item.LOTRWeaponStats;

import net.minecraft.item.ItemStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.EntityLiving;
import net.minecraft.pathfinding.PathEntity;

import java.util.List;

public class LoEPreciseAttackOnCollide extends LOTREntityAIAttackOnCollide {
    public double attack_range;
    public int attack_cooldown;

    public LoEPreciseAttackOnCollide(EntityCreature entity, double speed, double reach, int delay, boolean flag) {
        super(entity, speed, flag);
        attack_range = reach;
        attack_cooldown = delay;
    }

    @Override
    public void updateTask() {
        updateLookAndPathing();
        if (this.attackTick > 0)
            this.attackTick--;
        ItemStack weapon = this.theOwner.getHeldItem();

        // Throw a throwing spear if available
        if (weapon != null && weapon.getItem() instanceof LOTRItemSpear && this.attackTick <= 0 && this.theOwner instanceof LOTREntityNPC) {
            LOTREntityNPC theNPC = (LOTREntityNPC)this.theOwner;
            ItemStack spearBackup = theNPC.npcItemsInv.getSpearBackup();

            if (spearBackup != null) {
                LOTRItemSpear spearItem = (LOTRItemSpear)weapon.getItem();
                double d = this.theOwner.getDistanceToEntity((Entity)this.attackTarget);
                double range = this.theOwner.getNavigator().getPathSearchRange();
                if (d > 5.0D && d < range * 0.75D) {
                    LOTREntitySpear spear = new LOTREntitySpear(this.worldObj, (EntityLivingBase)this.theOwner, this.attackTarget, weapon.copy(), 0.75F + (float)d * 0.025F, 0.5F);
                    this.worldObj.playSoundAtEntity((Entity)this.theOwner, "random.bow", 1.0F, 1.0F / (this.worldObj.rand.nextFloat() * 0.4F + 1.2F) + 0.25F);
                    this.worldObj.spawnEntityInWorld((Entity)spear);
                    this.attackTick = 30 + this.theOwner.getRNG().nextInt(20);
                    if (ItemStack.areItemStacksEqual(theNPC.npcItemsInv.getIdleItem(), theNPC.npcItemsInv.getMeleeWeapon()))
                        theNPC.npcItemsInv.setIdleItem(spearBackup);
                    theNPC.npcItemsInv.setMeleeWeapon(spearBackup);
                    theNPC.npcItemsInv.setSpearBackup(null);
                    return;
                }
            }
        }
        float weaponReach = 1.0F;
        if (this.theOwner.ridingEntity != null)
            weaponReach = LOTREntityNPC.MOUNT_RANGE_BONUS;
        if (weapon == null) {
            weaponReach = (float)attack_range;
        } else {
            weaponReach *= LOTRWeaponStats.getMeleeReachFactor(weapon);
        }

        float meleeRange = (float)this.theOwner.boundingBox.getAverageEdgeLength() + weaponReach;
        double distanceSq = this.theOwner.getDistanceSqToEntity((Entity)this.attackTarget);

        if (distanceSq <= (meleeRange * meleeRange) || this.theOwner.boundingBox.intersectsWith(this.attackTarget.boundingBox)) {
            if (this.attackTick <= 0) {
                this.attackTick = (weapon == null) ? attack_cooldown : LOTRWeaponStats.getAttackTimeMob(weapon); // Apply cooldown here
                this.theOwner.attackEntityAsMob((Entity)this.attackTarget);
                this.theOwner.swingItem();
            }
        }
    }

    @Override
    protected void updateLookAndPathing() {
        this.theOwner.getLookHelper().setLookPositionWithEntity((Entity)this.attackTarget, 30.0F, 30.0F);
        if (this.theOwner.riddenByEntity != null && this.theOwner.riddenByEntity instanceof EntityLiving) {
            ((EntityLiving)this.theOwner.riddenByEntity).rotationYaw = this.theOwner.rotationYaw;
            ((EntityLiving)this.theOwner.riddenByEntity).rotationYawHead = this.theOwner.rotationYawHead;
        }
        if ((this.sightNotRequired || this.theOwner.getEntitySenses().canSee((Entity)this.attackTarget)) && --this.pathCheckTimer <= 0) {
            this.pathCheckTimer = 2;
            PathEntity path = getPathEntity();
            if (path != null)
                this.theOwner.getNavigator().setPath(path, this.moveSpeed);
        }
    }

    public PathEntity getPathEntity() {
        if (this.theOwner.ridingEntity != null)
            return this.worldObj.getPathEntityToEntity((Entity)this.theOwner, (Entity)this.attackTarget, this.theOwner.getNavigator().getPathSearchRange(), true, this.theOwner.getNavigator().getCanBreakDoors(), this.theOwner.getNavigator().getAvoidsWater(), false);
        return this.theOwner.getNavigator().getPathToEntityLiving((Entity)this.attackTarget);
    }
}
