using NUnit.Framework;
using UnityEditor;
using LLM.Editor.Analysis;
using System.Collections;
using UnityEngine.TestTools;

namespace LLM.Editor.Tests.Unit
{
    [TestFixture]
    public class EditorStateProviderTests
    {
        private EditorStateProvider _provider;

        [SetUp]
        public void SetUp()
        {
            _provider = new EditorStateProvider();
            // Ensure we are in Edit Mode before each test
            if (EditorApplication.isPlayingOrWillChangePlaymode)
            {
                EditorApplication.ExitPlaymode();   
            }
        }

        [Test]
        public void GetContext_WhenInEditMode_ReturnsEditMode()
        {
            // Act
            var context = _provider.GetContext(null, null);
            Assert.IsInstanceOf<EditorStateProvider.EditorStateResult>(context);
            var contents = (EditorStateProvider.EditorStateResult)context;

            // Assert
            Assert.AreEqual("EditMode", contents.currentState);
        }

        [UnityTest]
        public IEnumerator GetContext_WhenInPlayMode_ReturnsPlayMode()
        {
            // Arrange
            yield return new EnterPlayMode();

            // Act
            var context = _provider.GetContext(null, null);
            Assert.IsInstanceOf<EditorStateProvider.EditorStateResult>(context);
            var contents = (EditorStateProvider.EditorStateResult)context;

            // Assert
            Assert.AreEqual("PlayMode", contents.currentState);
            
            yield return new ExitPlayMode();
        }

        [UnityTest]
        public IEnumerator GetContext_WhenPaused_ReturnsPaused()
        {
            // Arrange
            yield return new EnterPlayMode();
            EditorApplication.isPaused = true;

            // Act
            var context = _provider.GetContext(null, null);
            Assert.IsInstanceOf<EditorStateProvider.EditorStateResult>(context);
            var contents = (EditorStateProvider.EditorStateResult)context;

            // Assert
            Assert.AreEqual("Paused", contents.currentState);

            // Cleanup
            EditorApplication.isPaused = false;
            yield return new ExitPlayMode();
        }
    }
}
