using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
using RetroDev.Random;
using UnityEngine.UI;
using System.Collections;

/// <summary>
/// Manages the loading screen behavior and scene transitions.
/// </summary>
public class Loader : MonoBehaviour
{
    [Header("Loading Hint Customization")]
    public TextMeshProUGUI hintText; // Text field for loading hints
    public Color keywordColor; // Color for highlighting keywords in the hint

    [Header("Loading Icon Customization")]
    public SpriteRenderer loadingImage; // Image to display loading icon
    public Sprite icon; // Sprite for the loading icon
    public Color iconColor; // Color of the loading icon
    public bool rotateIcon; // Flag to enable/disable icon rotation
    public float rotationSpeed; // Speed of icon rotation

    public static bool fromMenu; // Flag indicating if the transition is from the menu scene
    public static bool fromGame; // Flag indicating if the transition is from the game scene

    private void Start()
    {
        // Display a random quote on the loading screen
        ShowQuote();

        // Show the loading icon
        ShowLoader();

        // Load the game scene
        LoadGame();
    }

    /// <summary>
    /// Displays a random quote on the loading screen.
    /// </summary>
    public void ShowQuote()
    {
        hintText.text = GenerateQuote();
    }

    /// <summary>
    /// Displays the loading icon.
    /// </summary>
    public void ShowLoader()
    {
        // Display the loading icon with specified settings
        iconColor.a = 1f;
        loadingImage.sprite = icon;
        loadingImage.color = iconColor;
    }

    /// <summary>
    /// Generates and returns a random quote string.
    /// </summary>
    public string GenerateQuote()
    {
        int randomInt = RandomUtil.RandomNumber(1, 10);

        // If a controller is being used, show the controller hints
        if (ControllerDetector.IsControllerConnected())
        {
            // Switch case to select a random quote
            switch (randomInt)
            {
                case 1:
                    return $"don't forget to dash with <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.controllerDash}</color> - it might even save you.";
                case 2:
                    return $"Did you know you can view your ship stats in-game using <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.controllerHUD}</color> ?";
                case 3:
                    return "Each game is unique and different - Have a bad round? The next might be better!";
                case 4:
                    return $"You can move your ship forwards and backwards with the <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.controllerForward}";
                case 5:
                    return $"The key to survival is to keep moving! Remember to rotate your ship quickly with the <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.controllerRotateLeft}";
                case 6:
                    return "Have you noticed the blue blocks yet? They provide you with much needed cover during intense battles.";
                case 7:
                    return "Choosing the right upgrades will greatly improve your time in the field.";
                case 8:
                    return $"Need a break? You can pause your game with <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.controllerPause}</color>.";
                case 9:
                    return $"In the rare case where enemy ships fire at you, you can return fire using <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.controllerShoot}</color>.";
                default:
                    return "I have run out of useful hints for you.";
            }
        }
        // Otherwise if not, show the keyboard hints
        else
        {
            // Switch case to select a random quote
            switch (randomInt)
            {
                case 1:
                    return $"don't forget to dash with <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.keyboardDash}</color> - it might even save you.";
                case 2:
                    return $"Did you know you can view your ship stats in-game using <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.keyboardHUD}</color> ?";
                case 3:
                    return "Each game is unique and different - Have a bad round? The next might be better!";
                case 4:
                    return $"You can move your ship forwards with <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.keyboardForward}</color> and backwards with <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.keyboardBackward}</color>.";
                case 5:
                    return $"The key to survival is to keep moving! Remember to rotate your ship quickly with <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.keyboardRotateLeft}</color> and <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.keyboardRotateRight}</color>.";
                case 6:
                    return "Have you noticed the blue blocks yet? They provide you with much needed cover during intense battles.";
                case 7:
                    return "Choosing the right upgrades will greatly improve your time in the field.";
                case 8:
                    return $"Need a break? You can pause your game with <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.keyboardPause}</color>.";
                case 9:
                    return $"In the rare case where enemy ships fire at you, you can return fire using <color=#{keywordColor.ToRGBHex()}>{ControlsUI.instance.keyboardShoot}</color>.";
                default:
                    return "I have run out of useful hints for you.";
            }
        }
    }

    private void LateUpdate()
    {
        // Rotate the loading icon if enabled
        if (rotateIcon)
        {
            float rotationAmount = rotationSpeed * Time.deltaTime;
            loadingImage.transform.Rotate(Vector3.back * rotationAmount);
        }
    }

    /// <summary>
    /// Loads the game scene based on the transition type.
    /// </summary>
    public void LoadGame()
    {
        if (fromMenu)
        {
            // Load the game scene after a delay
            StartCoroutine(ChangeScenes("Game"));
        }

        if (fromGame)
        {
            // Load the menu scene after a delay
            StartCoroutine(ChangeScenes("Menu"));
        }
    }

    /// <summary>
    /// Coroutine to change scenes after a delay.
    /// </summary>
    private IEnumerator ChangeScenes(string sceneName)
    {
        // Wait for a short delay before changing scenes
        yield return new WaitForSeconds(3f);

        // Change to the specified scene
        SceneManager.LoadScene(sceneName);
    }
}
