/*
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 UnityEngine;
using UnityEngine.XR;
using UnityEngine.SceneManagement;

namespace EyeSkills
{
    public class Lobby : MonoBehaviour
    {
        public float counter = 10f;

        private void Start()
        {
            Debug.Log("Lobby Start");
            //Screen.orientation = ScreenOrientation.LandscapeLeft;
            StartCoroutine(_go());
        }

        private IEnumerator _go()
        {
            Debug.Log("Starting Lobby Enumerator - next scene to load " +
                      ESSceneManager.instance.sceneToLoad.ToString());
            NetworkManager.instance.RegisterScene("Lobby", "");
            NetworkManager.instance.RegisterButton("start", "Start",
                "This button only has effect once the device is in landscape orientation.");


            var landscapeElement = PowerUI.UI.document.getElementById("landscape");
            var portraitElement = PowerUI.UI.document.getElementById("portrait");
            ScreenOrientation so = ScreenOrientation.Landscape;

            while (true)
            {
                if (so != Screen.orientation)
                {
                    Debug.Log("Lobby displaying screen");
                    bool landscape = (Screen.orientation == ScreenOrientation.LandscapeLeft ||
                                      Screen.orientation == ScreenOrientation.LandscapeRight);
                    landscapeElement.style.display = landscape ? "block" : "none";
                    portraitElement.style.display = landscape ? "none" : "block";
                    NetworkManager.instance.RegisterScene("Lobby",
                        "Device is in " + Screen.orientation.ToString() + " orientation.");
                    so = Screen.orientation;
                }

                if (Screen.orientation == ScreenOrientation.Landscape ||
                    Screen.orientation == ScreenOrientation.LandscapeLeft ||
                    Screen.orientation == ScreenOrientation.LandscapeRight)
                    break;

                yield return 0;
            }

            // hide UI
            GetComponent<PowerUI.Manager>().enabled = false;

            // turn on VR
            XRSettings.LoadDeviceByName("cardboard");
            yield return 0;
            XRSettings.enabled = true;
            yield return 0;

            Debug.Log("Lobby loading scene " + ESSceneManager.instance.sceneToLoad.ToString());

            SceneManager.LoadScene(ESSceneManager.instance.sceneToLoad);
        }
    }
}