using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// GAME 1303 Tier 2 — Player walks in, the boss wakes up, the doors close.
///
/// SETUP
///  1. Empty GameObject with a BoxCollider2D (Is Trigger) covering the arena entrance.
///  2. Drag the BossController in. Set its Start Automatically OFF.
///  3. OnArenaEntered: enable a wall/door object behind the player, swap music
///     (AudioManager.PlayMusic), show the boss health bar.
///  4. Wire BossController.OnBossDefeated back to this trigger's ReleaseArena (or
///     directly to the door) to open up again.
/// </summary>
[RequireComponent(typeof(Collider2D))]
public class BossArenaTrigger : MonoBehaviour
{
    public BossController boss;
    [Tooltip("Objects to turn ON when the fight starts (doors/walls) and OFF when it ends.")]
    public GameObject[] barriers;
    public UnityEvent OnArenaEntered;
    public UnityEvent OnArenaReleased;

    private bool triggered;

    void Reset() { GetComponent<Collider2D>().isTrigger = true; }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (triggered || !other.CompareTag("Player")) return;
        triggered = true;

        foreach (var b in barriers) if (b) b.SetActive(true);
        if (boss != null)
        {
            boss.OnBossDefeated.AddListener(ReleaseArena);
            boss.StartBoss();
        }
        OnArenaEntered?.Invoke();
    }

    public void ReleaseArena()
    {
        foreach (var b in barriers) if (b) b.SetActive(false);
        OnArenaReleased?.Invoke();
    }
}