using System.Collections;
using UnityEngine;
[CreateAssetMenu(
    menuName = "Game/EnemyAbilities/Throw",
    fileName = "ThrowAbility")]
public class ThrowAbility : EnemyAbility
{
    [SerializeField] private GameObject projectilePrefab;
    [SerializeField] private int projectileDamage;
    [SerializeField] private float lifeTime = 6f;
    public override float ComputeUtility(EnemyDecisionContext ctx)
    {
        if (ctx.Entity.Brain.Combat.IsAttacking) return 0f;
        
        float baseUtility = 0.2f;
        float dist = ctx.DistanceToPlayer;
        float distUtility = (dist > ctx.Config.idealAttackDistance) ? 0.5f : 0.0f;
        float utility = distUtility * 0.5f + baseUtility; 
        
        return utility;
    }
    
    public override void Use(EnemyDecisionContext ctx)
    {
        
        ctx.Entity.Brain.animationBridge.SetThrowTrigger();
        ctx.Entity.StartCoroutine(ThrowAfterDelay(ctx, 1.25f)); //
        
        
    }
    
    private IEnumerator ThrowAfterDelay(EnemyDecisionContext ctx, float delay)
    {
        yield return new WaitForSeconds(delay);

        Vector2 spawnPos = ctx.Entity.transform.position;
        GameObject projectile = Instantiate(projectilePrefab, spawnPos, Quaternion.identity);
        Projectile projectileScript = projectile.GetComponent<Projectile>();
        projectileScript.Direction = (ctx.PlayerTransform.position - ctx.Entity.transform.position).normalized;
        projectileScript.projectileDamage = projectileDamage;
        Destroy(projectile, lifeTime);
    }
}

