package jesse.keeblarcraft.entity;

import jesse.keeblarcraft.entity.MAKE_ENTITY_REGISTER;
import net.minecraft.entity.AnimationState;
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.goal.AnimalMateGoal;
import net.minecraft.entity.ai.goal.FollowParentGoal;
import net.minecraft.entity.ai.goal.LookAroundGoal;
import net.minecraft.entity.ai.goal.LookAtEntityGoal;
import net.minecraft.entity.ai.goal.SwimGoal;
import net.minecraft.entity.ai.goal.TemptGoal;
import net.minecraft.entity.ai.goal.WanderAroundFarGoal;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.entity.passive.PassiveEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.recipe.Ingredient;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.world.World;

public class PorcupineEntity extends AnimalEntity {
    public final AnimationState idlingAnimationState = new AnimationState();
    private int idleAnimationCooldown = 0;

    public PorcupineEntity (EntityType<? extends AnimalEntity> entityType, World world) {
      super(entityType, world);
   }

   private void updateAnimations() {
    if (this.idleAnimationCooldown <= 0) {
        this.idleAnimationCooldown = this.random.nextInt(40) + 80;
        this.idlingAnimationState.start(this.age);
        } else {
        --this.idleAnimationCooldown;
        }
   }

   protected void updateLimbs(float posDelta) {
      float f;
      if (this.getPose() == EntityPose.STANDING) {
         f = Math.min(posDelta * 6.0F, 1.0F);
      } else {
         f = 0.0F;
      }

      this.limbAnimator.updateLimbs(f, 0.2F);
   }

   @Override
   public void tick() {
    super.tick();
    if (this.getWorld().isClient) {
        this.updateAnimations();
    }
   }


   @Override
   protected void initGoals() {
    this.goalSelector.add(0, new SwimGoal(this));

    this.goalSelector.add(1, new AnimalMateGoal(this, 5));
    
    this.goalSelector.add(2, new TemptGoal(this, 2, Ingredient.ofItems(Items.BEETROOT), false));

    this.goalSelector.add(3, new FollowParentGoal(this, 3));

    this.goalSelector.add(4, new WanderAroundFarGoal(this, 2));

    this.goalSelector.add(5, new LookAtEntityGoal(this, PlayerEntity.class, 10));

    this.goalSelector.add(5, new LookAtEntityGoal(this, LivingEntity.class, 10));

    this.goalSelector.add(6, new LookAroundGoal(this));
}

   public static DefaultAttributeContainer.Builder createPorcupineAttributes() {
    return MobEntity.createMobAttributes()
    .add(EntityAttributes.GENERIC_MAX_HEALTH, 10)
    .add(EntityAttributes.GENERIC_ATTACK_DAMAGE, 1)
    .add(EntityAttributes.GENERIC_MOVEMENT_SPEED, .2)
    .add(EntityAttributes.GENERIC_ARMOR, 2);
   }

   @Override
   public boolean isBreedingItem(ItemStack stack) {
       return stack.isOf(Items.OAK_LOG);
   }


    @Override
    public PassiveEntity createChild(ServerWorld world, PassiveEntity entity) {
        return MAKE_ENTITY_REGISTER.PORCUPINE.create(world);
    }

    @Override
    protected SoundEvent getHurtSound(DamageSource source) {
        return SoundEvents.ENTITY_GHAST_HURT;
     }

    protected SoundEvent getDeathSound() {
        return SoundEvents.ENTITY_CREEPER_DEATH;
    }
    
}
