using QFSW.QC;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace BNG {
    public class SceneLoader : MonoBehaviour {
        public List<string> AllMapsLevelNames = new List<string>();
        public string ResultSceneName = "ResultScene";

        public static SceneLoader Instance;
        public int TournamentSceneCount = 3;
        private List<string> _randomMapsList = new List<string>();


        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
            }
            else if (Instance != this)
            {
                Destroy(gameObject);
            }
        }


        private string _sceneName;

        private void Start()
        {
            InitStartValues();
        }

        [ContextMenu("Debug test")]
        private void InitStartValues()
        {
            _randomMapsList.Clear();
            _randomMapsList = new List<string>();

            _randomMapsList.AddRange(AllMapsLevelNames.OrderBy(x => Random.Range(0, 100)).Take(TournamentSceneCount));
            _randomMapsList.Add(ResultSceneName);

            GlobalSettings.Instance.AllMaps = false;
        }

        public void ChangeLevel()
        {
            var firstPersonAfterJack = _randomMapsList.SkipWhile(p => p != SceneManager.GetActiveScene().name).ElementAt(1);
            LoadScene(firstPersonAfterJack);
        }

        public void LoadTournament()
        {
            LoadScene(_randomMapsList.First());
        }

        public void LoadScene(string sceneName)
        {
            _sceneName = sceneName;
            StartCoroutine(LoadAsuncScene());
        }

        [ContextMenu("Debug load mon")]
        private void LoadMonument()
        {
            LoadScene(_randomMapsList.First());
        }

        public void LoadStartScene()
        {
            InitStartValues();
            LoadScene("StartScene");
            //var @new = "StartScene";
            //var old = SceneManager.GetActiveScene().name;
            //_sceneName = @new;
            //StartCoroutine(LoadAsuncScene(@new, old));
        }

        private IEnumerator LoadAsuncScene()
        {
            var scene = SceneManager.LoadSceneAsync(_sceneName, LoadSceneMode.Single);
            scene.allowSceneActivation = false;

            while (!scene.isDone)
            {
                scene.allowSceneActivation = true;
                yield return null;
            }
        }

        private IEnumerator LoadAsuncScene(string @new,string @old)
        {
            var scene = SceneManager.LoadSceneAsync(@new, LoadSceneMode.Additive);
            scene.allowSceneActivation = false;

            while (!scene.isDone)
            {
                scene.allowSceneActivation = true;
                yield return null;
            }

            yield return new WaitForEndOfFrame();

            SceneManager.UnloadScene(@old);
        }


        private int _sceneIndex = 0;

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                SceneManager.LoadScene(0);
            }

            if (Input.GetKeyDown(KeyCode.Equals))
            {
                _sceneIndex++;
                if (_sceneIndex >= SceneManager.sceneCountInBuildSettings)
                    _sceneIndex = 0;

                SceneManager.LoadScene(_sceneIndex);
            }
            else if (Input.GetKeyDown(KeyCode.Minus))
            {
                _sceneIndex--;
                if (_sceneIndex < 0)
                    _sceneIndex = SceneManager.sceneCountInBuildSettings - 1;

                SceneManager.LoadScene(_sceneIndex);
            }
        }
    }
}