package common.entity.npc;

import java.util.List;

import common.entity.effect.EntityLightning;
import common.entity.types.EntityLiving;
import common.init.Items;
import common.item.ItemStack;
import common.item.tool.ItemPotion;
import common.potion.Potion;
import common.potion.PotionEffect;
import common.rng.Random;
import common.world.World;

public class EntityMage extends EntityNPC
{
    private int attackTimer;
    private boolean drinking;

    public EntityMage(World worldIn)
    {
        super(worldIn);
    }
    
//    public boolean isRangedWeapon(ItemStack stack) {
//    	return stack != null && stack.getItem() == Items.potion;
//    }
    
    public void onLivingUpdate()
    {
        if (!this.worldObj.client && !this.isPlayer())
        {
            if (this.drinking)
            {
                if (this.attackTimer-- <= 0)
                {
                    this.drinking = false;
                    ItemStack itemstack = this.getHeldItem();
                    this.setItem(0, null);

                    if (itemstack != null && itemstack.getItem() instanceof ItemPotion potion)
                    {
                        List<PotionEffect> list = potion.getEffects();

                        if (list != null)
                        {
                            for (PotionEffect potioneffect : list)
                            {
                            	if(potioneffect.getPotion().isInstant())
                            		potioneffect.getPotion().onImpact(null, null, this, potioneffect.getAmplifier(), 1.0);
                            	else
                            		this.addEffect(new PotionEffect(potioneffect));
                            }
                        }
                    }

                    this.setSpeedMod(1.0f);
                }
            }
            else
            {
                int i = -1;

//                if (this.rand.floatv() < 0.15F && this.isInsideOfLiquid() && !this.hasEffect(Potion.waterBreathing))
//                {
//                    i = 8237;
//                }
//                else
                if (this.rand.floatv() < 0.15F && this.isBurning() && !this.hasEffect(Potion.FIRE_RESISTANCE))
                {
                    i = 16307;
                }
                else if (this.rand.floatv() < 0.05F && this.getHealth() < this.getMaxHealth())
                {
                    i = 16341;
                }
                else if (this.rand.floatv() < 0.25F && this.getAttackTarget() != null && !this.hasEffect(Potion.SPEED) && this.getAttackTarget().getDistanceSqToEntity(this) > 121.0D)
                {
                    i = 16274;
                }
                else if (this.rand.floatv() < 0.25F && this.getAttackTarget() != null && !this.hasEffect(Potion.SPEED) && this.getAttackTarget().getDistanceSqToEntity(this) > 121.0D)
                {
                    i = 16274;
                }

                if (i > -1)
                {
                    this.setItem(0, new ItemStack(ItemPotion.getPotionItem(i)));
                    this.attackTimer = this.getHeldItem().getMaxItemUseDuration();
                    this.drinking = true;
                    this.setSpeedMod(0.165f);
                }
                else if(this.rand.chance(80)) {
                	boolean far = this.getAttackTarget() != null && this.getAttackTarget().getDistanceSqToEntity(this) >= 256.0;
                	this.setItem(0, far || this.rand.chance() ? new ItemStack(Items.splash_potion) : null);
                }
            }
        }

        super.onLivingUpdate();
    }
    
    public void attackEntityWithRangedAttack(EntityLiving target, float range) {
        if(!this.drinking)
        	super.attackEntityWithRangedAttack(target, range);
    }
    
	public int getColor() {
		return 0x9b58c8;
	}
    
	public void onStruckByLightning(EntityLightning lightningBolt) {
	}
    
    public boolean isMagnetic() {
    	return true;
    }

//	public boolean isAggressive() {
//		return true;
//	}
//
//	public boolean isPeaceful() {
//		return false;
//	}

	public boolean isAggressive(Class<? extends EntityNPC> clazz) {
		return clazz == EntityElf.class;
	}

//	public boolean isPeaceful(Class<? extends EntityNPC> clazz) {
//		return false;
//	}

//	public boolean canInfight(Alignment align) {
//		return true;
//	}
//
//	public boolean canMurder(Alignment align) {
//		return false;
//	}

	public boolean canUseMagic() {
		return true;
	}

	public int getBaseHealth(Random rand) {
		return rand.range(26, 66);
	}

//	public int getBaseEnergy(Random rand) {
//		return 0;
//	}

    public float getHeightDeviationMin() {
    	return -0.2f;
    }

    public float getHeightDeviationMax() {
    	return 0.2f;
    }
    
    public float getSpeciesBaseSize() {
    	return 1.85f;
    }

	public Alignment getNaturalAlign() {
		return rand.pick(Alignment.EVIL, Alignment.CHAOTIC_EVIL);
	}
}
