using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace Streamingle.Editor.Tests
{
    public sealed class AICameraGeneratorWindowUiTests
    {
        [Test]
        public void UiAssetsLoadAndExposeTheCoreWorkflowContract()
        {
            var uxmlPath =
                AICameraGeneratorWindow.ResolveUiAssetPathForTests(false);
            var ussPath =
                AICameraGeneratorWindow.ResolveUiAssetPathForTests(true);

            Assert.That(uxmlPath, Is.Not.Empty);
            Assert.That(ussPath, Is.Not.Empty);
            var visualTree =
                AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxmlPath);
            var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(ussPath);
            Assert.That(visualTree, Is.Not.Null);
            Assert.That(styleSheet, Is.Not.Null);

            var root = visualTree.CloneTree();
            Assert.That(
                root.Q<DropdownField>("source-director-dropdown"),
                Is.Not.Null);
            Assert.That(root.Q<Button>("generate-full-button"), Is.Not.Null);
            Assert.That(root.Q<VisualElement>("refinement-card"), Is.Not.Null);
            Assert.That(
                root.Q<DropdownField>("selected-shot-dropdown"),
                Is.Not.Null);
            Assert.That(root.Q<Button>("regenerate-shot-button"), Is.Not.Null);
            Assert.That(root.Q<VisualElement>("final-card"), Is.Not.Null);
            Assert.That(root.Q<Button>("save-final-scene-button"), Is.Not.Null);
            Assert.That(root.Q<Foldout>("advanced-foldout"), Is.Not.Null);
            Assert.That(root.Q<ProgressBar>("generation-progress"), Is.Not.Null);
            Assert.That(root.Q<Button>("status-log-button"), Is.Not.Null);
            Assert.That(root.Q<Button>("cancel-generation-button"), Is.Not.Null);
        }

        [Test]
        public void CreateGuiCanRunTwiceWithoutDuplicatingTheWindowRoot()
        {
            var window = ScriptableObject.CreateInstance<AICameraGeneratorWindow>();
            try
            {
                window.CreateGUI();
                window.CreateGUI();

                var roots = window.rootVisualElement
                    .Query<VisualElement>("camera-generator-root")
                    .ToList();
                Assert.That(roots, Has.Count.EqualTo(1));
                Assert.That(
                    window.rootVisualElement.Q<Button>("generate-full-button"),
                    Is.Not.Null);
            }
            finally
            {
                Object.DestroyImmediate(window);
            }
        }
    }
}
