using System.Collections.Generic;
using UnityEngine;
using UnitySceneManager = UnityEngine.SceneManagement.SceneManager;
using UnityScene = UnityEngine.SceneManagement.Scene;
using System.Collections;
namespace FishNet.Managing.Scened
{
    public class DefaultSceneProcessor : SceneProcessorBase
    {
        #region Private.
        /// 
        /// Currently active loading AsyncOperations.
        /// 
        protected List LoadingAsyncOperations = new List();
        /// 
        /// A collection of scenes used both for loading and unloading.
        /// 
        protected List Scenes = new List();
        /// 
        /// Current AsyncOperation being processed.
        /// 
        protected AsyncOperation CurrentAsyncOperation;
        #endregion
        /// 
        /// Called when scene loading has begun.
        /// 
        public override void LoadStart(LoadQueueData queueData)
        {
            base.LoadStart(queueData);
            ResetValues();
        }
        public override void LoadEnd(LoadQueueData queueData)
        {
            base.LoadEnd(queueData);
            ResetValues();
        }
        /// 
        /// Resets values for a fresh load or unload.
        /// 
        private void ResetValues()
        {
            CurrentAsyncOperation = null;
            LoadingAsyncOperations.Clear();
        }
        /// 
        /// Called when scene unloading has begun within an unload operation.
        /// 
        /// 
        public override void UnloadStart(UnloadQueueData queueData)
        {
            base.UnloadStart(queueData);
            Scenes.Clear();
        }
        /// 
        /// Begin loading a scene using an async method.
        /// 
        /// Scene name to load.
        public override void BeginLoadAsync(string sceneName, UnityEngine.SceneManagement.LoadSceneParameters parameters)
        {
            AsyncOperation ao = UnitySceneManager.LoadSceneAsync(sceneName, parameters);
            LoadingAsyncOperations.Add(ao);
            
            CurrentAsyncOperation = ao;
            CurrentAsyncOperation.allowSceneActivation = false;
        }
        /// 
        /// Begin unloading a scene using an async method.
        /// 
        /// Scene name to unload.
        public override void BeginUnloadAsync(UnityScene scene)
        {
            CurrentAsyncOperation = UnitySceneManager.UnloadSceneAsync(scene);
        }
        /// 
        /// Returns if a scene load or unload percent is done.
        /// 
        /// 
        public override bool IsPercentComplete()
        {
            return (GetPercentComplete() >= 0.9f);
        }
        /// 
        /// Returns the progress on the current scene load or unload.
        /// 
        /// 
        public override float GetPercentComplete()
        {
            return (CurrentAsyncOperation == null) ? 1f : CurrentAsyncOperation.progress;
        }
        /// 
        /// Adds a loaded scene.
        /// 
        /// Scene loaded.
        public override void AddLoadedScene(UnityScene scene)
        {
            base.AddLoadedScene(scene);
            Scenes.Add(scene);
        }
        /// 
        /// Returns scenes which were loaded during a load operation.
        /// 
        public override List GetLoadedScenes()
        {
            return Scenes;
        }
        /// 
        /// Activates scenes which were loaded.
        /// 
        public override void ActivateLoadedScenes()
        {
            foreach (AsyncOperation ao in LoadingAsyncOperations)
                ao.allowSceneActivation = true;
        }
        /// 
        /// Returns if all asynchronized tasks are considered IsDone.
        /// 
        /// 
        public override IEnumerator AsyncsIsDone()
        {
            bool notDone;
            do
            {
                notDone = false;
                foreach (AsyncOperation ao in LoadingAsyncOperations)
                {
                    if (!ao.isDone)
                    {
                        notDone = true;
                        break;
                    }
                }
                yield return null;
            } while (notDone);
            yield break;
        }
    }
}