using UnityEngine;

public class EnemyCombat : MonoBehaviour
{
    // --- Configuration (passed from Brain) ---
    private EnemyConfig _config;
    private Transform _attackPoint;
    private LayerMask _playerMask;
    private EnemyAnimation _animationBridge;
    // --- State ---
    private float _lastAttackTime = -999f;
    private float _lastBlockTime = -999f;
    
    public float TimeSinceLastAttack => _lastAttackTime;
    public bool IsBlocking { get; private set; }
    public bool IsAttacking { get; private set; } // Renamed for clarity, handled internally

    public bool IsStunned { get; private set; }

    private BoxCollider2D _hitbox; // The enemy's hurtbox (to disable when blocking)

    public void Initialize(EnemyConfig config, BoxCollider2D selfCollider, Transform attackPoint, LayerMask playerMask, EnemyAnimation animationBridge)
    {
        _config = config;
        _hitbox = selfCollider;
        _attackPoint = attackPoint;
        _playerMask = playerMask;
        _animationBridge = animationBridge;
    }

    public void DisableHitBox()
    {
        _hitbox.enabled = false;
    }
    // --- Queries (Used by Actions to decide utility) ---
    public bool CanAttack()
    {
        return Time.time >= _lastAttackTime + _config.attackCooldown;
    }

    public bool CanBlock()
    {
        return Time.time >= _lastBlockTime + _config.blockCooldown;
    }

    // --- Actions (Called by your Strategy Actions) ---
    
    public void Block()
    {
        if (IsBlocking || !CanBlock()) return;
        _lastBlockTime = Time.time;
        IsBlocking = true;
        _hitbox.enabled = false;
        _animationBridge.SetBlockTrigger();
    }

    public void PerformAbility(EnemyAbility ability, EnemyDecisionContext ctx)
    {
        // This replaces the "Attack()" loop execution part
        _lastAttackTime = Time.time;
        IsAttacking = true;
        
        ability.Use(ctx); // Triggers the specific animation/logic defined in the Ability
    }

    // --- Animation Events (Called by EnemyAnimation.cs) ---

    public void GetStunned()
    {
        if (IsStunned) return;
        
        IsStunned = true;
        
        if (IsAttacking)
        {
            IsAttacking = false;
        }
        
        
        _animationBridge.SetStunTrigger();
    }
    public void EndBlock()
    {
        IsBlocking = false;
        _hitbox.enabled = true;
    }

    public void EndAttack()
    {
        IsAttacking = false;
    }

    public void EndStun()
    {
        IsStunned = false;
    }
    public void DealDamage()
    {
        Collider2D[] hits = Physics2D.OverlapCircleAll(
            _attackPoint.position,
            _config.attackRadius,
            _playerMask
        );

        foreach (var hit in hits)
        {
            if (hit.TryGetComponent(out Health playerHealth))
            {
                playerHealth.TakeDamage(_config.damage);
                SpawnHitVFX(hit);
                return;
            }
            
        }
    }

    private void SpawnHitVFX(Collider2D hit)
    {
        if (HitEffectManager.Instance != null)
        {
            Vector2 hitPos = hit.bounds.center;
            Vector2 dir = (hitPos - (Vector2)_attackPoint.position).normalized;
            
            var type = ReturnRandomHitEffectType(Random.Range(0, 3));
            
            HitEffectManager.Instance.Spawn(
                type,
                hitPos,
                dir
            );
        }
    }

    private HitEffectType ReturnRandomHitEffectType(int randomNumber)
    {
        HitEffectType type = HitEffectType.SmackHit;
        if (randomNumber == 0) type = HitEffectType.ZapHit;
        else if (randomNumber == 1) type = HitEffectType.SmackHit;
        else if (randomNumber == 2) type = HitEffectType.SplashHit;

        return type;
    }
    // Debug Gizmo remains here as it relates to combat range
    private void OnDrawGizmosSelected()
    {
        if (_attackPoint == null) return;

        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(_attackPoint.position, _config.attackRadius);

        // Show which objects would be hit right now
        Collider2D[] hits = Physics2D.OverlapCircleAll(_attackPoint.position, _config.attackRadius, _playerMask);
        Gizmos.color = Color.red;

        foreach (var h in hits)
        {
            Gizmos.DrawSphere(h.transform.position, 0.05f);
        }
    }
}