using System;
using System.Collections.Generic;
using RothenburgAR.Common;
using UnityEngine;

namespace RothenburgAR.UI
{
    public enum ViewName
    {
        ARView,
        SettingsView,
        TutorialView,
        CreditsView,
        LanguageSelectView,
        ColorSelectView
    }

    public class UIManager : Singleton<UIManager>
    {
        private ARViewBehaviour _ARViewBehaviour;
        private Camera _uiCamera;

        public Boolean IsARViewVisible
        {
            get { return _currentView.ViewName == ViewName.ARView; }
        }

        public ARViewBehaviour ARViewBehaviour
        {
            get
            {
                if (_currentView.ViewName != ViewName.ARView)
                {
                    Debug.Log("ARView is not currently active, access is not allowed. Returning null.");
                    return null;
                }
                if (_ARViewBehaviour == null)
                {
                    GameObject[] x = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
                    foreach (var go in x)
                    {
                        if (go.name == "ARView(Clone)")
                        {
                            _ARViewBehaviour = go.GetComponent<ARViewBehaviour>();
                        }
                    }
                }
                return _ARViewBehaviour;
            }
        }

        private Dictionary<ViewName, View> _availableViews;

        private View _currentView;
        private GameObject _currentViewInstance;

        public bool StartingUp { get; set; }

        public UIManager()
        {
            _availableViews = new Dictionary<ViewName, View>();
            StartingUp = true;
        }

        internal void Initialize()
        {
            _availableViews.Add(ViewName.ARView,
                new View()
                {
                    ViewName = ViewName.ARView,
                    PrefabName = "UI/ARView",
                    OnExit = () => ARViewBehaviour.HidePoiDetails(),
                });

            _availableViews.Add(ViewName.SettingsView,
                new View()
                {
                    ViewName = ViewName.SettingsView,
                    PrefabName = "UI/SettingsView"
                });

            _availableViews.Add(ViewName.TutorialView,
                new View()
                {
                    ViewName = ViewName.TutorialView,
                    PrefabName = "UI/TutorialView"
                });

            _availableViews.Add(ViewName.CreditsView,
                new View()
                {
                    ViewName = ViewName.CreditsView,
                    PrefabName = "UI/CreditsView"
                });

            _availableViews.Add(ViewName.LanguageSelectView,
                new View()
                {
                    ViewName = ViewName.LanguageSelectView,
                    PrefabName = "UI/LanguageSelectView"
                });

            _availableViews.Add(ViewName.ColorSelectView,
                new View()
                {
                    ViewName = ViewName.ColorSelectView,
                    PrefabName = "UI/ColorSelectView"
                });
        }

        public void InitStartView()
        {
            SwitchToView(ViewName.LanguageSelectView);
            StartingUp = true;
        }

        public void SwitchToView(ViewName nextViewName)
        {
            if (_currentView != null && _currentView.ViewName == nextViewName)
            {
                Debug.Log("Tried to switch to view '" + nextViewName + "', but it is already loaded.");
                return;
            }

            View nextView;
            if (!_availableViews.TryGetValue(nextViewName, out nextView))
            {
                Debug.Log("Tried to switch to view '" + nextViewName + "', but it failed.");
                return;
            }

            if (_currentView != null)
            {
                Debug.Log("Switching from view '" + _currentView.ViewName + "' to '" +
                          nextViewName + "'");
            }
            else
            {
                Debug.Log("Switching to view '" + nextViewName + "'");
            }


            if (_currentView != null && _currentView.OnExit != null)
            {
                _currentView.OnExit();
            }

            if (_currentViewInstance != null)
            {
                Destroy(_currentViewInstance);
            }


            _currentView = nextView;
            GameObject prefab = Resources.Load<GameObject>(nextView.PrefabName);
            if (prefab != null)
            {
                _currentViewInstance = Instantiate(prefab);

                Canvas c = _currentViewInstance.GetComponentInChildren<Canvas>();
                if (c != null)
                {
                    c.worldCamera = _uiCamera;
                    c.renderMode = RenderMode.ScreenSpaceCamera;
                    c.planeDistance = 0.5f;
                }
            }
            // Set the StartingUp value
            if (StartingUp && nextViewName == ViewName.ARView)
            {
                StartingUp = false;
            }

            if (nextView.OnEnter != null)
            {
                nextView.OnEnter();
            }
        }

        void Update()
        {
            if (_currentView != null && _currentView.OnUpdate != null)
            {
                _currentView.OnUpdate();
            }
        }

        public void SetUiCamera(Camera uiCamera)
        {
            _uiCamera = uiCamera;
        }
    }
}