using System.IO;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;

#if pHUMANOID4
using NUnit.Framework;
namespace Passer.Humanoid {

    public class Sites_Test {
        [UnitySetUp]
        public IEnumerator Setup() {
            if (File.Exists(Application.dataPath + "/Passer/HumanoidControlFree/Tests/Runtime/Sites/[Test]HumanoidVisitor Desktop.unity"))
                UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode("Assets/Passer/HumanoidControlFree/Tests/Runtime/Sites/[Test]HumanoidVisitor Desktop.unity", new LoadSceneParameters(LoadSceneMode.Single));
            else
                UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode("Packages/HumanoidControl4_Free/Tests/Runtime/Sites/[Test]HumanoidVisitor Desktop.unity", new LoadSceneParameters(LoadSceneMode.Single));
            yield return new WaitForSeconds(0.1F);
        }

        [UnityTest]
        [Category("Sites")]
        public IEnumerator VisitShootingRange() {
            HumanoidControl humanoid = Object.FindObjectOfType<HumanoidControl>();
            Assert.IsFalse(humanoid == null);

            SiteNavigator navigator = humanoid.GetComponentInChildren<SiteNavigator>();
            Assert.IsFalse(navigator == null);

            navigator.LoadSiteFromURL("serrarens.nl/sites/shootingrange");
            yield return new WaitForSeconds(2);

            int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount;
            Assert.AreEqual(1, sceneCount);

            bool found = false;
            for (int i = 0; i < sceneCount; i++) {
                Scene scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
                if (scene.name == "ShootingRange")
                    found = true;
            }
            Assert.IsTrue(found);

            yield return new WaitForSeconds(1);
            // Destroy explicitly, because DontDestroyOnLoad is enabled
            Object.Destroy(humanoid.gameObject);
            yield return new WaitForSeconds(0.1F);
        }

        [UnityTest]
        [Category("Sites")]
        public IEnumerator VisitSocialSpace() {
            HumanoidControl humanoid = Object.FindObjectOfType<HumanoidControl>();
            Assert.IsFalse(humanoid == null);

            SiteNavigator navigator = humanoid.GetComponentInChildren<SiteNavigator>();
            Assert.IsFalse(navigator == null);

            navigator.LoadSiteFromURL("serrarens.nl/sites/socialspace");
            yield return new WaitForSeconds(2);

            int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount;
            Assert.AreEqual(1, sceneCount);

            bool found = false;
            for (int i = 0; i < sceneCount; i++) {
                Scene scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
                if (scene.name == "SocialSpace")
                    found = true;
            }
            Assert.IsTrue(found);

            yield return new WaitForSeconds(1);
            // Destroy explicitly, because DontDestroyOnLoad is enabled
            Object.Destroy(humanoid.gameObject);
            yield return new WaitForSeconds(0.1F);
        }

        [UnityTest]
        [Category("Sites")]
        public IEnumerator VisitAvatarShop() {
            HumanoidControl humanoid = Object.FindObjectOfType<HumanoidControl>();
            Assert.IsFalse(humanoid == null);

            SiteNavigator navigator = humanoid.GetComponentInChildren<SiteNavigator>();
            Assert.IsFalse(navigator == null);

            navigator.LoadSiteFromURL("serrarens.nl/sites/avatarshop");
            yield return new WaitForSeconds(2);

            int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount;
            Assert.AreEqual(1, sceneCount);

            bool found = false;
            for (int i = 0; i < sceneCount; i++) {
                Scene scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
                if (scene.name == "AvatarShop")
                    found = true;
            }
            Assert.IsTrue(found);

            yield return new WaitForSeconds(1);
            // Destroy explicitly, because DontDestroyOnLoad is enabled
            Debug.Log("Destroying humanoid gameObject");
            Object.Destroy(humanoid.gameObject);
            yield return new WaitForSeconds(0.1F);
        }

        [UnityTest]
        [Category("Sites")]
        public IEnumerator VisitGroceryStore() {
            HumanoidControl humanoid = Object.FindObjectOfType<HumanoidControl>();
            Assert.IsFalse(humanoid == null);

            SiteNavigator navigator = humanoid.GetComponentInChildren<SiteNavigator>();
            Assert.IsFalse(navigator == null);

            navigator.LoadSiteFromURL("serrarens.nl/sites/grocerystore");
            yield return new WaitForSeconds(2);

            int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount;
            Assert.AreEqual(1, sceneCount);

            bool found = false;
            for (int i = 0; i < sceneCount; i++) {
                Scene scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
                if (scene.name == "GroceryStore")
                    found = true;
            }
            Assert.IsTrue(found);

            yield return new WaitForSeconds(1);
            // Destroy explicitly, because DontDestroyOnLoad is enabled
            Object.Destroy(humanoid.gameObject);
            yield return new WaitForSeconds(0.1F);
        }

        [UnityTest]
        [Category("Sites")]
        public IEnumerator VisitTour() {
            HumanoidControl humanoid = Object.FindObjectOfType<HumanoidControl>();
            Assert.IsFalse(humanoid == null);

            SiteNavigator navigator = humanoid.GetComponentInChildren<SiteNavigator>();
            Assert.IsFalse(navigator == null);

            navigator.LoadSiteFromURL("serrarens.nl/sites/shootingrange");
            yield return new WaitForSeconds(2);

            int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount;
            Assert.AreEqual(1, sceneCount);

            bool found = false;
            for (int i = 0; i < sceneCount; i++) {
                Scene scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
                if (scene.name == "ShootingRange")
                    found = true;
            }
            Assert.IsTrue(found);

            navigator.LoadSiteFromURL("serrarens.nl/sites/socialspace");
            yield return new WaitForSeconds(2);

            sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount;
            Assert.AreEqual(1, sceneCount);

            found = false;
            for (int i = 0; i < sceneCount; i++) {
                Scene scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
                if (scene.name == "SocialSpace")
                    found = true;
            }
            Assert.IsTrue(found);

            navigator.LoadSiteFromURL("serrarens.nl/sites/avatarshop");
            yield return new WaitForSeconds(2);

            sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount;
            Assert.AreEqual(1, sceneCount);

            found = false;
            for (int i = 0; i < sceneCount; i++) {
                Scene scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
                if (scene.name == "AvatarShop")
                    found = true;
            }
            Assert.IsTrue(found);

            navigator.LoadSiteFromURL("serrarens.nl/sites/grocerystore");
            yield return new WaitForSeconds(2);

            sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCount;
            Assert.AreEqual(1, sceneCount);

            found = false;
            for (int i = 0; i < sceneCount; i++) {
                Scene scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt(i);
                if (scene.name == "GroceryStore")
                    found = true;
            }
            Assert.IsTrue(found);

            yield return new WaitForSeconds(1);
            // Destroy explicitly, because DontDestroyOnLoad is enabled
            Object.Destroy(humanoid.gameObject);
            yield return new WaitForSeconds(0.1F);
        }

    }
}
#endif