using UnityEngine;
using System.Collections;

/// <summary>
/// TeamIndicatorManager - Automatically adds TeamIndicator components to all characters in Cash System mode.
/// Attach to a manager object in the scene or it will be created by CashSystemManager.
/// </summary>
public class TeamIndicatorManager : MonoBehaviour
{
    [Header("Settings")]
    [SerializeField] private bool autoSetupOnStart = true;
    [SerializeField] private float refreshInterval = 2f; // Check for new characters periodically
    
    private static TeamIndicatorManager instance;
    public static TeamIndicatorManager Instance => instance;
    
    private void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(gameObject);
            return;
        }
        instance = this;
    }
    
    private void Start()
    {
        // Only activate in Cash System mode
        if (SelectionOptions.Instance == null || !SelectionOptions.Instance.isCashSystemSelected)
        {
            enabled = false;
            return;
        }
        
        if (autoSetupOnStart)
        {
            StartCoroutine(SetupIndicatorsDelayed());
        }
    }
    
    private IEnumerator SetupIndicatorsDelayed()
    {
        // Wait for characters to spawn
        yield return new WaitForSeconds(1f);
        
        SetupAllIndicators();
        
        // Periodic refresh for newly spawned characters
        while (enabled)
        {
            yield return new WaitForSeconds(refreshInterval);
            RefreshIndicators();
        }
    }
    
    /// <summary>
    /// Add TeamIndicator to all characters that don't have one
    /// </summary>
    public void SetupAllIndicators()
    {
        // Find all player team members
        TeamMember[] allTeamMembers = FindObjectsOfType<TeamMember>();
        foreach (var member in allTeamMembers)
        {
            AddIndicatorIfMissing(member.gameObject);
        }
        
        // Find all characters with HealthNew (covers AI enemies too)
        HealthNew[] allCharacters = FindObjectsOfType<HealthNew>();
        foreach (var character in allCharacters)
        {
            AddIndicatorIfMissing(character.gameObject);
        }
        
        Debug.Log($"[TeamIndicatorManager] Setup indicators on {allTeamMembers.Length + allCharacters.Length} characters");
    }
    
    /// <summary>
    /// Check for new characters and add indicators
    /// </summary>
    public void RefreshIndicators()
    {
        TeamMember[] allTeamMembers = FindObjectsOfType<TeamMember>();
        foreach (var member in allTeamMembers)
        {
            AddIndicatorIfMissing(member.gameObject);
        }
        
        HealthNew[] allCharacters = FindObjectsOfType<HealthNew>();
        foreach (var character in allCharacters)
        {
            AddIndicatorIfMissing(character.gameObject);
        }
        
        // Refresh colors on all indicators
        TeamIndicator.RefreshAllIndicators();
    }
    
    private void AddIndicatorIfMissing(GameObject character)
    {
        if (character.GetComponent<TeamIndicator>() == null)
        {
            character.AddComponent<TeamIndicator>();
            Debug.Log($"[TeamIndicatorManager] Added TeamIndicator to {character.name}");
        }
    }
    
    /// <summary>
    /// Manually add indicator to a specific character
    /// </summary>
    public void AddIndicator(GameObject character)
    {
        AddIndicatorIfMissing(character);
    }
}
