/*
 * TITLE SCREEN ERROR TROUBLESHOOTING GUIDE
 * ========================================
 * 
 * If you're getting ArgumentOutOfRangeException errors when running the Title Screen,
 * this is likely due to UI Toolkit stylesheet issues.
 * 
 * SOLUTION STEPS:
 * ==============
 * 
 * 1. REMOVE USS REFERENCE FROM UXML (FIXED)
 *    - The TitleScreen.uxml file now has the problematic Style reference removed
 *    - You'll need to manually assign the USS file in Unity instead
 * 
 * 2. ASSIGN STYLESHEET MANUALLY IN UNITY:
 *    a) Open your TitleScreen scene
 *    b) Select the GameObject with UIDocument component
 *    c) In the UIDocument component, find the "Style Sheets" section
 *    d) Click the "+" button to add a style sheet
 *    e) Assign the TitleScreen.uss file from Assets/UI Toolkit/
 * 
 * 3. ALTERNATIVE SIMPLE SETUP (If USS still causes issues):
 *    - You can remove the USS file entirely and use inline styles
 *    - The buttons will use Unity's default styling
 *    - See the SimpleSetup method below for code-based styling
 * 
 * 4. VERIFY SETUP:
 *    - Make sure the UXML file is assigned to the UIDocument
 *    - Make sure the TitleScreenManager script is on the same GameObject
 *    - Check Console for any "not found" warnings about buttons
 * 
 * COMMON ISSUES:
 * =============
 * 
 * - USS files with unsupported CSS properties cause this error
 * - Hardcoded GUID references in UXML can break when files are moved
 * - Button names in UXML must match exactly what the script expects
 * 
 * TEST WITHOUT STYLING:
 * ====================
 * 
 * If you want to test functionality without any styling issues:
 * 1. Remove the USS file assignment completely
 * 2. The UI will use Unity's default button styling
 * 3. Once functionality works, you can re-add styling gradually
 * 
 */

using UnityEngine;
using UnityEngine.UIElements;

public class TitleScreenTroubleshooter : MonoBehaviour
{
    [Header("Troubleshooting Options")]
    public bool useSimpleSetupWithoutUSS = false;

    [ContextMenu("Apply Simple Setup (No USS)")]
    public void ApplySimpleSetup()
    {
        var uiDocument = GetComponent<UIDocument>();
        if (uiDocument == null)
        {
            Debug.LogError("No UIDocument found on this GameObject");
            return;
        }

        var root = uiDocument.rootVisualElement;

        // Apply basic inline styling to avoid USS issues
        var titleScreen = root.Q<VisualElement>("TitleScreen");
        if (titleScreen != null)
        {
            titleScreen.style.width = Length.Percent(100);
            titleScreen.style.height = Length.Percent(100);
            titleScreen.style.backgroundColor = new Color(0.08f, 0.08f, 0.12f, 1f); // Dark blue background
        }

        var mainContent = root.Q<VisualElement>("MainContent");
        if (mainContent != null)
        {
            mainContent.style.width = Length.Percent(100);
            mainContent.style.height = Length.Percent(100);
            mainContent.style.alignItems = Align.Center;
            mainContent.style.justifyContent = Justify.Center;
            mainContent.style.flexDirection = FlexDirection.Column;
        }

        var gameTitle = root.Q<Label>("GameTitle");
        if (gameTitle != null)
        {
            gameTitle.style.fontSize = 48;
            gameTitle.style.color = new Color(1f, 0.84f, 0.39f, 1f); // Gold color
            gameTitle.style.unityFontStyleAndWeight = FontStyle.Bold;
            gameTitle.style.marginBottom = 20;
        }

        var gameSubtitle = root.Q<Label>("GameSubtitle");
        if (gameSubtitle != null)
        {
            gameSubtitle.style.fontSize = 18;
            gameSubtitle.style.color = new Color(0.8f, 0.8f, 0.8f, 1f); // Light gray
            gameSubtitle.style.marginBottom = 40;
        }

        // Style buttons
        var newGameButton = root.Q<Button>("NewGameButton");
        var loadGameButton = root.Q<Button>("LoadGameButton");
        var quitButton = root.Q<Button>("QuitButton");

        StyleButton(newGameButton, new Color(0.24f, 0.47f, 0.24f, 1f)); // Green
        StyleButton(loadGameButton, new Color(0.24f, 0.24f, 0.47f, 1f)); // Blue
        StyleButton(quitButton, new Color(0.47f, 0.24f, 0.24f, 1f)); // Red

        Debug.Log("Simple setup applied - no USS styling used");
    }

    private void StyleButton(Button button, Color backgroundColor)
    {
        if (button == null) return;

        button.style.width = 200;
        button.style.height = 50;
        button.style.marginTop = 8;
        button.style.marginBottom = 8;
        button.style.backgroundColor = backgroundColor;
        button.style.borderTopLeftRadius = 8;
        button.style.borderTopRightRadius = 8;
        button.style.borderBottomLeftRadius = 8;
        button.style.borderBottomRightRadius = 8;
        button.style.fontSize = 16;
        button.style.color = Color.white;
        button.style.unityFontStyleAndWeight = FontStyle.Bold;
    }

    [ContextMenu("Test Button Finding")]
    public void TestButtonFinding()
    {
        var uiDocument = GetComponent<UIDocument>();
        if (uiDocument == null)
        {
            Debug.LogError("No UIDocument found");
            return;
        }

        var root = uiDocument.rootVisualElement;

        var newGameButton = root.Q<Button>("NewGameButton");
        var loadGameButton = root.Q<Button>("LoadGameButton");
        var quitButton = root.Q<Button>("QuitButton");

        Debug.Log($"NewGameButton found: {newGameButton != null}");
        Debug.Log($"LoadGameButton found: {loadGameButton != null}");
        Debug.Log($"QuitButton found: {quitButton != null}");

        if (newGameButton == null || loadGameButton == null || quitButton == null)
        {
            Debug.LogWarning("Some buttons not found - check UXML button names match script expectations");
        }
        else
        {
            Debug.Log("All buttons found successfully!");
        }
    }
}
