package com.zivilon.cinder_loe.entity;

import java.util.UUID;

import lotr.common.LOTRAchievement;
import lotr.common.enchant.LOTREnchantment;
import lotr.common.enchant.LOTREnchantmentHelper;
import lotr.common.entity.ai.LOTREntityAIAttackOnCollide;
import lotr.common.entity.ai.LOTREntityAIDrink;
import lotr.common.entity.ai.LOTREntityAIEat;
import lotr.common.entity.ai.LOTREntityAIFollowHiringPlayer;
import lotr.common.entity.ai.LOTREntityAIHiredRemainStill;
import lotr.common.entity.npc.LOTREntityNPC;
import lotr.common.entity.projectile.LOTREntityMarshWraithBall;
import lotr.common.fac.LOTRFaction;
import lotr.common.world.structure.LOTRChestContents;



import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.EnumCreatureAttribute;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

import com.zivilon.cinder_loe.CinderLoE;

public class Wraith extends LOTREntityNPC {

    public Wraith(World world) {
        super(world);
        setSize(0.8F, 2.5F);
        ((EntityLiving) this).tasks.addTask(1, (EntityAIBase) new LOTREntityAIHiredRemainStill(this));
        ((EntityLiving) this).tasks.addTask(2, (EntityAIBase) new LOTREntityAIAttackOnCollide(this, 1.3D, false));
        ((EntityLiving) this).tasks.addTask(3, (EntityAIBase) new LOTREntityAIFollowHiringPlayer(this));
        ((EntityLiving) this).tasks.addTask(5, (EntityAIBase) new EntityAIWander(this, 1.0D));
        ((EntityLiving) this).tasks.addTask(9, (EntityAIBase) new EntityAILookIdle((EntityLiving) this));
        addTargetTasks(true);
    }
    
    protected void entityInit() {
        super.entityInit();
        this.dataWatcher.addObject(17, Integer.valueOf(0));
    }
    
    protected void applyEntityAttributes() {
        super.applyEntityAttributes();
        getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(50.0D);
        getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.2D);
    }
    
    public int getDeathFadeTime() {
        return this.dataWatcher.getWatchableObjectInt(17);
    }
    
    public void setDeathFadeTime(int i) {
        this.dataWatcher.updateObject(17, Integer.valueOf(i));
    }
    
    public LOTRFaction getFaction() {
        return LOTRFaction.HOSTILE;
    }
    
    public void writeEntityToNBT(NBTTagCompound nbt) {
        super.writeEntityToNBT(nbt);
        nbt.setInteger("DeathFadeTime", getDeathFadeTime());
    }
    
    public void readEntityFromNBT(NBTTagCompound nbt) {
        super.readEntityFromNBT(nbt);
        setDeathFadeTime(nbt.getInteger("DeathFadeTime"));
    }
    
    @Override
    public void moveEntityWithHeading(float strafe, float forward) {
        // First, call the super method to preserve normal movement behavior
        super.moveEntityWithHeading(strafe, forward);

        // Check for water at the entity's feet
        int x = MathHelper.floor_double(this.posX);
        int y = MathHelper.floor_double(this.posY);
        int z = MathHelper.floor_double(this.posZ);

        boolean isWater = this.worldObj.getBlock(x, y, z).getMaterial() == Material.water ||
                          this.worldObj.getBlock(x, y - 1, z).getMaterial() == Material.water; // Check below the entity too

        if (isWater) {
            this.onGround = true; // Treat water as solid ground
            this.motionY = 0.0; // Stop falling/sinking
            this.setPosition(this.posX, y + 1, this.posZ); // Adjust position to stay on top of the water
        }
    }


    public void setInWeb() {}
    
    public void onLivingUpdate() {
        super.onLivingUpdate();
        if (rand.nextBoolean())
            ((Entity)this).worldObj.spawnParticle("smoke", ((Entity)this).posX + (rand.nextDouble() - 0.5D) * ((Entity)this).width, ((Entity)this).posY + rand.nextDouble() * ((Entity)this).height, ((Entity)this).posZ + (rand.nextDouble() - 0.5D) * ((Entity)this).width, 0.0D, 0.0D, 0.0D); 
        if (!((Entity)this).worldObj.isRemote) {
            if (getDeathFadeTime() > 0)
                setDeathFadeTime(getDeathFadeTime() - 1); 
            if (getDeathFadeTime() == 1)
                setDead(); 
        } 
    }
    
    public boolean attackEntityFrom(DamageSource damagesource, float f) {
        boolean vulnerable = false;
        Entity entity = damagesource.getEntity();
        if (entity instanceof EntityLivingBase && entity == damagesource.getSourceOfDamage()) {
            ItemStack itemstack = ((EntityLivingBase)entity).getHeldItem();
            if (itemstack != null && LOTREnchantmentHelper.hasEnchant(itemstack, LOTREnchantment.baneWraith))
                vulnerable = true; 
        } 
        if (vulnerable && getDeathFadeTime() == 0) {
            boolean flag = super.attackEntityFrom(damagesource, f);
            return flag;
        } 
        return false;
    }
    
    public void onDeath(DamageSource damagesource) {
        super.onDeath(damagesource);
        if (!((Entity)this).worldObj.isRemote)
            setDeathFadeTime(30); 
    }
    
    protected void dropFewItems(boolean flag, int i) {
        super.dropFewItems(flag, i);
        int flesh = 1 + rand.nextInt(3) + rand.nextInt(i + 1);
        for (int l = 0; l < flesh; l++)
            dropItem(Items.rotten_flesh, 1); 
        dropChestContents(LOTRChestContents.MARSH_REMAINS, 1, 3 + i);
    }
    
    public EnumCreatureAttribute getCreatureAttribute() {
        return EnumCreatureAttribute.UNDEAD;
    }
    
    protected String getHurtSound() {
        return "lotr:wight.hurt";
    }
    
    protected String getDeathSound() {
        return "lotr:wight.death";
    }
    
    public boolean handleWaterMovement() {
        return false;
    }
    
    protected void func_145780_a(int i, int j, int k, Block block) {}

    public void attackEntityWithRangedAttack(EntityLivingBase target, float distanceFactor) {
        // Do nothing
    }

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