/*
Copyright 2019 Dr. Thomas Benjamin Senior, Michael Zoeller 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.SceneManagement;

namespace EyeSkills
{
    public class ESSceneManager : MonoBehaviour
    {
        // scene to be loaded next
        public string sceneToLoad = "";

        // singleton like access pattern
        public static ESSceneManager instance;

        void Awake()
        {
            if (instance == null)
                instance = this;
            else if (instance != this)
                Destroy(gameObject);

            DontDestroyOnLoad(this);
        }

        void OnEnable()
        {
            SceneManager.sceneLoaded += OnSceneLoaded;
        }

        void OnSceneLoaded(Scene scene, LoadSceneMode mode)
        {
            Screen.sleepTimeout = SleepTimeout.NeverSleep;
        }

        void OnDisable()
        {
            SceneManager.sceneLoaded -= OnSceneLoaded;
        }

        private void Update()
        {

            //Constantly checking to see if the user is hitting the back button in any non-menu scene (which will result in the menu being loaded).
            if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name != "Menu" && (Input.GetKeyDown(KeyCode.Escape) || Input.GetButton("EyeSkills Cancel")))
                SceneManager.LoadScene("Menu");
        }

        public void VRSwitch(bool enableVR)
        {
            if (Application.isEditor)
                return;

            if (!enableVR && XRSettings.enabled)
            {
                Debug.Log("SceneManager: Loading scene directly");
                // disable VR
                XRSettings.enabled = false;
                //Screen.orientation = ScreenOrientation.Portrait;
                string s = SceneManager.GetActiveScene().name;
                SceneManager.LoadScene(s);
            }
            else if (enableVR && !XRSettings.enabled)
            {
                Debug.Log("SceneManager: Loading lobby");
                // enable VR
                sceneToLoad = SceneManager.GetActiveScene().name;
                SceneManager.LoadScene("Lobby");
            }
        }
    }
}
