using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.UI;

public class SplashScreenManager : MonoBehaviour
{
    [Header("Splash Screen Elements")]
    public Image splashScreenImage; // Reference to the Image component to display splash screens
    public GameObject objectToEnable; // Object to enable after the splash screens finish
    public Camera mainCamera; // Main camera to access post-processing effects

    [Header("Splash Screen Sprites")]
    public Sprite[] splashScreenSprites; // Array of splash screen sprites

    [Header("Splash Screen Customization")]
    public float fadeInTime = 1f; // Time to fade in each splash screen
    public float fadeOutTime = 1f; // Time to fade out each splash screen
    public float transitionDelay = 1f; // Delay between showing each splash screen
    public float objectRevealDelay = 1f; // Delay between final image and object being enabled

    [Header("Other Settings")]
    public bool enableSplash; // Enable / disable splash screen

    public Action onSplashScreensFinished; // Callback for when all splash screens have been shown

    public static SplashScreenManager instance;

    /// <summary>
    /// Disables splash screen if loaded from game.
    /// </summary>
    private void Awake()
    {
        if (Loader.fromGame)
        {
            enableSplash = false;
            Loader.fromGame = false;
        }
    }

    /// <summary>
    /// Starts the splash screen or skips it based on settings.
    /// </summary>
    public void Start()
    {
        if (enableSplash)
        {
            mainCamera.GetComponent<PostProcessLayer>().enabled = false;
            mainCamera.GetComponent<PostProcessVolume>().enabled = false;
            onSplashScreensFinished += EnableObject;
            StartCoroutine(ShowSplashScreens());
        }
        else
        {
            gameObject.SetActive(false);
            objectToEnable.SetActive(true);
            MainMenu.instance.ShowMenu();
        }
    }

    /// <summary>
    /// Shows splash screens with fading effect.
    /// </summary>
    public IEnumerator ShowSplashScreens()
    {
        foreach (Sprite splashScreenSprite in splashScreenSprites)
        {
            splashScreenImage.sprite = splashScreenSprite;
            splashScreenImage.preserveAspect = true;
            FadeIn();
            yield return new WaitForSeconds(fadeInTime + transitionDelay);
            FadeOut();
            yield return new WaitForSeconds(fadeOutTime + transitionDelay);
        }
        onSplashScreensFinished?.Invoke(); // Invoke callback when all splash screens are done
    }

    /// <summary>
    /// Fades in the splash screen image.
    /// </summary>
    private void FadeIn()
    {
        StartCoroutine(Fade(0f, 1f, fadeInTime));
    }

    /// <summary>
    /// Fades out the splash screen image.
    /// </summary>
    private void FadeOut()
    {
        StartCoroutine(Fade(1f, 0f, fadeOutTime));
    }

    /// <summary>
    /// Performs fade effect on the splash screen image.
    /// </summary>
    private IEnumerator Fade(float startAlpha, float targetAlpha, float duration)
    {
        float startTime = Time.time;
        float endTime = startTime + duration;
        Color color = splashScreenImage.color;
        color.a = startAlpha;

        while (Time.time < endTime)
        {
            float timeRatio = (Time.time - startTime) / duration;
            color.a = Mathf.Lerp(startAlpha, targetAlpha, timeRatio);
            splashScreenImage.color = color;
            yield return null;
        }

        color.a = targetAlpha;
        splashScreenImage.color = color;
    }

    /// <summary>
    /// Enables the object after splash screens finish.
    /// </summary>
    public void EnableObject()
    {
        gameObject.SetActive(false);
        mainCamera.GetComponent<PostProcessLayer>().enabled = true;
        mainCamera.GetComponent<PostProcessVolume>().enabled = true;
        objectToEnable.SetActive(true);
        MainMenu.instance.ShowMenu();
    }
}
