﻿#region Header

// Copyright (c) 2021-2022 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.

#endregion

using BlackBox.Utils;
using UnityEngine;
using System.Windows.Input;

namespace BlackBox
{
    public static class Bootstrap
    {
        private static PluginManager pluginManager_ = null;
        private static LogWriter logWriter_ = null;
        private static ScreenGrabber screenGrabber_ = null;
        private static PerformanceProfiler profiler_;

#if UNITY_STANDALONE
        private static IssueReporter issueReporter_ = null;
#endif
        
        [RuntimeInitializeOnLoadMethod]
        private static void StartBlackBox()
        {
            var config = BlackBoxConfig.Instance;
            
            // Asserts
            config.OnValidate();
            
            if (!config.AutoInitialize)
            {
                return;
            }

            Initialize();
        }

#if DEBUG || DEVELOPMENT_BUILD
        public static void ManualInitialize()
        {
            Initialize();
        }
#else
        public static void ManualInitialize()
        {
            // do nothing
        }        
#endif
        
        private static void Initialize()
        {
            Logger.Log("Initialize");
            
            var cycle  = Cycle.Instance;
            cycle.OnInitialize   += OnInitialize;
            cycle.OnStartSession += OnStartSession;
            cycle.OnEndSession   += OnEndSession;
            cycle.OnUpdate       += OnUpdate;
#if UNITY_ANDROID || UNITY_IOS
            cycle.OnPreInitialize       += OnPreInitialize;
#endif
            cycle.Initialize();
        }

        private static void OnInitialize()
        {
            logWriter_     = new LogWriter();
            screenGrabber_ = new ScreenGrabber();
            profiler_      = PerformanceProfiler.ResolveProfiler();
#if UNITY_STANDALONE
            pluginManager_ = new PluginManager();
#endif
            pluginManager_.OnValidateBlackBox();
            pluginManager_.Initialize();

#if UNITY_STANDALONE
            issueReporter_ = new IssueReporter();
            issueReporter_.Initialize();
#endif
        }
        
        private static void OnStartSession()
        {
            Logger.Log("OnStartSession");
            
            pluginManager_.StartSession();
            logWriter_.Start();
        }
        
        private static void OnEndSession()
        {
            Logger.Log("OnEndSession");
            
            pluginManager_.EndSession();
            logWriter_.End();
        }
        
        private static void OnUpdate()
        {
            profiler_.Capture();
            pluginManager_.Update(Time.deltaTime, profiler_.GetCpuTime(), profiler_.GetGpuTime(), true);
            screenGrabber_.Update();
#if UNITY_STANDALONE_WIN
            issueReporter_.Update();
#endif
        }

#if UNITY_ANDROID || UNITY_IOS
        private static void OnPreInitialize()
        {
            pluginManager_ = new PluginManager();
            pluginManager_.PreInitialize();
        }
#endif
    }
}
