using System;
using UnityEngine;

namespace Yumon.Template.Lite.Internal
{
    public static class YumonGameManager
    {
        /// <summary>
        ///     Invoked when the gameplay starts.
        ///     Subscribe to this event to start your gameplay logic.
        ///     You can also use <see cref="IYumonGameplay" /> interface or <see cref="YumonGameplay" /> abstract class.
        /// </summary>
        public static event Action OnLevelStart;

        /// <summary>
        ///     Invoked when the player returns to the home screen.
        ///     Subscribe to this event to reset your gameplay logic to its initial state.
        ///     You can also use <see cref="IYumonGameplay" /> interface or <see cref="YumonGameplay" /> abstract class.
        /// </summary>
        public static event Action OnReturnToHome;

        /// <summary>
        ///     Invoked when the gameplay ends.
        ///     Subscribe to this event to end your gameplay logic.
        ///     You can also use <see cref="IYumonGameplay" /> interface or <see cref="YumonGameplay" /> abstract class.
        /// </summary>
        public static event Action OnLevelEnd;

        /// <summary>
        ///     You must call this method to end the gameplay.
        ///     This method will invoke the <see cref="OnLevelEnd" /> event.
        /// </summary>
        /// <param name="score"> The level score that will be posted to the leaderboard. </param>
        public static void EndLevel(int score)
        {
            Debug.Log($"Gameplay ended with score: {score}");
            GameState.SetState(GameState.EndGame);

            OnLevelEnd?.Invoke();
            OnLevelEndWithScore?.Invoke(score);
        }

        internal static event Action<int> OnLevelEndWithScore;

        internal static void StartLevel()
        {
            Debug.Log("Gameplay started");
            GameState.SetState(GameState.Gameplay);
            OnLevelStart?.Invoke();
        }

        internal static void ReturnToHome()
        {
            Debug.Log("Returned to home");
            GameState.SetState(GameState.Home);
            OnReturnToHome?.Invoke();
        }
    }
}
