/*
 * TITLE SCREEN SETUP FIX
 * ======================
 * 
 * ERROR: "UIDocument component not found on TitleScreenManager GameObject"
 * 
 * SOLUTION:
 * ========
 * 
 * The TitleScreenManager script requires a UIDocument component on the same GameObject.
 * 
 * STEP-BY-STEP FIX:
 * ================
 * 
 * Option A - Add UIDocument to existing GameObject:
 * ------------------------------------------------
 * 1. Select the GameObject that has the TitleScreenManager script
 * 2. In Inspector, click "Add Component"
 * 3. Search for "UIDocument" and add it
 * 4. In the UIDocument component:
 *    - Set "Source Asset" to your TitleScreen.uxml file
 *    - In "Style Sheets" section, click "+" and add TitleScreen.uss
 * 
 * Option B - Move script to GameObject with UIDocument:
 * ---------------------------------------------------
 * 1. Find the GameObject that already has UIDocument component
 * 2. Add the TitleScreenManager script to that GameObject
 * 3. Remove TitleScreenManager from the old GameObject
 * 
 * Option C - Create new GameObject with both components:
 * ----------------------------------------------------
 * 1. Create empty GameObject named "TitleScreenManager"
 * 2. Add UIDocument component
 * 3. Add TitleScreenManager script
 * 4. Configure UIDocument as described in Option A
 * 
 * VERIFY SETUP:
 * ============
 * 
 * After fixing, you should have ONE GameObject with:
 * ✓ TitleScreenManager script
 * ✓ UIDocument component (with UXML and USS assigned)
 * 
 * The error should disappear and buttons should work.
 * 
 */

using UnityEngine;
using UnityEngine.UIElements;

public class TitleScreenSetupHelper : MonoBehaviour
{
    [Header("Setup Instructions")]
    [TextArea(8, 12)]
    public string setupSteps = @"1. This GameObject needs BOTH components:
   - TitleScreenManager script ✓
   - UIDocument component (missing)

2. Add UIDocument component to this GameObject

3. In UIDocument inspector:
   - Source Asset: TitleScreen.uxml
   - Style Sheets: Add TitleScreen.uss

4. Remove this helper component when done";

    [Header("Setup Validation")]
    public bool hasUIDocument = false;
    public bool hasManagerScript = false;
    public bool uiDocumentConfigured = false;

    private void Start()
    {
        ValidateSetup();
    }

    [ContextMenu("Validate Setup")]
    public void ValidateSetup()
    {
        Debug.Log("=== TITLE SCREEN SETUP VALIDATION ===");

        // Check for UIDocument
        UIDocument uiDoc = GetComponent<UIDocument>();
        hasUIDocument = uiDoc != null;
        Debug.Log($"UIDocument component: {(hasUIDocument ? "✓ Found" : "❌ Missing")}");

        // Check for TitleScreenManager
        TitleScreenManager manager = GetComponent<TitleScreenManager>();
        hasManagerScript = manager != null;
        Debug.Log($"TitleScreenManager script: {(hasManagerScript ? "✓ Found" : "❌ Missing")}");

        // Check UIDocument configuration
        if (hasUIDocument)
        {
            var sourceAsset = uiDoc.visualTreeAsset;

            uiDocumentConfigured = sourceAsset != null;
            Debug.Log($"UXML file assigned: {(sourceAsset != null ? "✓ Yes" : "❌ No")}");

            if (sourceAsset != null)
            {
                Debug.Log($"UXML file: {sourceAsset.name}");
            }

            // Check for style sheets (they're embedded in the UXML or assigned differently)
            Debug.Log("USS files: Check manually in UIDocument inspector");
        }

        // Final status
        bool setupComplete = hasUIDocument && hasManagerScript && uiDocumentConfigured;
        Debug.Log($"=== SETUP STATUS: {(setupComplete ? "✅ COMPLETE" : "❌ INCOMPLETE")} ===");

        if (!setupComplete)
        {
            Debug.LogWarning("Setup incomplete! Follow the instructions in the setupSteps field.");
        }
    }

    [ContextMenu("Auto-Fix Setup (if possible)")]
    public void AutoFixSetup()
    {
        Debug.Log("Attempting auto-fix...");

        // Add UIDocument if missing
        if (!hasUIDocument)
        {
            UIDocument uiDoc = gameObject.AddComponent<UIDocument>();
            Debug.Log("✓ Added UIDocument component");

            // Try to find and assign UXML file
            var uxml = Resources.Load<VisualTreeAsset>("TitleScreen");
            if (uxml == null)
            {
                // Try different paths
                string[] searchPaths = {
                    "Assets/UI Toolkit/TitleScreen.uxml",
                    "Assets/UI/TitleScreen.uxml",
                    "TitleScreen"
                };

                foreach (string path in searchPaths)
                {
                    uxml = UnityEditor.AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(path);
                    if (uxml != null)
                    {
                        Debug.Log($"✓ Found UXML at: {path}");
                        break;
                    }
                }
            }

            if (uxml != null)
            {
                uiDoc.visualTreeAsset = uxml;
                Debug.Log("✓ UXML file assigned automatically");
            }
            else
            {
                Debug.LogWarning("❌ Could not find TitleScreen.uxml - please assign manually");
            }
        }

        ValidateSetup();
    }

    // Remove this component once setup is complete
    [ContextMenu("Remove Setup Helper")]
    public void RemoveSetupHelper()
    {
        if (Application.isPlaying)
        {
            Destroy(this);
        }
        else
        {
            DestroyImmediate(this);
        }
        Debug.Log("Setup helper removed");
    }
}
