using Nox.EditorCode;
using System;
using Unity.VectorGraphics;
using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.SceneManagement;

namespace Nox.Core {
    public class Boot {
        /// <summary>
        /// The is the first method called when the game starts. It will load the Initializer prefab which will initialize the project
        /// </summary>
        [RuntimeInitializeOnLoadMethod]
        private static void Initialize() {
#if UNITY_EDITOR
            switch(BootMode.BootType) {
                case BootType.UnityDefault:
                    return;
                case BootType.SceneBoot:
                    Addressables.InstantiateAsync("Initializer").WaitForCompletion();
                    break;
                case BootType.FullBoot:
                    var loadOperation = SceneManager.LoadSceneAsync("Startup", LoadSceneMode.Single);
                    if(loadOperation != null) {
                        loadOperation.completed += OnCompleted;
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
#else
            Addressables.InstantiateAsync("Initializer").WaitForCompletion();
#endif
        }

        private static void OnCompleted(AsyncOperation obj) {
            obj.allowSceneActivation = true;
            Addressables.InstantiateAsync("Initializer").WaitForCompletion();
        }
    }
}
