using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;

public class MainMenu : MonoBehaviour
{
    [SerializeField] GameObject mainMenu;
    [SerializeField] GameObject skipCreditsText;
    [SerializeField] GameObject skipCreditsIcon;
    [SerializeField] GameObject credits;
    [SerializeField] Button creditsButton;
    [SerializeField] float menuFadeSpeed;
    [SerializeField] float creditsScrollSpeed;
    [SerializeField] float textAlphaFadeSpeed;

    RectMask2D mainMenuMask;
    private bool isCreditScrolling = false;
    private bool skipCuePopped = false;


    private void Start()
    {
        mainMenuMask = mainMenu.GetComponent<RectMask2D>();
    }

    public void AnyKeyPressed(InputAction.CallbackContext context) 
    {
        if (context.performed)
        {
            if (isCreditScrolling)
            {
                StartCoroutine("RevealSkipCue", 0f);
            }
        }
    }

    public void StopCredits(InputAction.CallbackContext context)
    {
        if (!isCreditScrolling) return;
        else
        {
            if (context.performed && skipCuePopped)
            {
                isCreditScrolling = false;
                ResetMenu();
            }
        }
    }

    private void Update()
    {
        //Debug.Log(Input.GetKeyDown(KeyCode.Escape));
        if (isCreditScrolling && Input.GetKeyDown(KeyCode.Escape))
        {
            isCreditScrolling = false;
        }
    }

    public void CreditButtonPress()
    {
        creditsButton.interactable = false;
        StartCoroutine("PlayCredits", 0f);
    }

    public IEnumerator PlayCredits()
    {
        isCreditScrolling = true;
        yield return MainMenuFadeOut();
        yield return new WaitForSeconds(1.5f);
        yield return CreditsScrollUp();
        yield return new WaitForSeconds(.5f);
        yield return MainMenuFadeIn();
    }


    public IEnumerator MainMenuFadeOut()
    {
        float paddingAmount;
        Vector4 padding;

        while (mainMenuMask.padding.y <= 20)
        {
            paddingAmount = mainMenuMask.padding.y + (menuFadeSpeed * Time.deltaTime);
            padding = new Vector4(mainMenuMask.padding.x, paddingAmount, mainMenuMask.padding.z, mainMenuMask.padding.w);
            mainMenuMask.padding = padding;

            yield return null;
        }
    }

    public IEnumerator MainMenuFadeIn()
    {
        float paddingAmount;
        Vector4 padding;

        while (mainMenuMask.padding.y >= -110)
        {
            paddingAmount = mainMenuMask.padding.y - (menuFadeSpeed * Time.deltaTime);
            padding = new Vector4(mainMenuMask.padding.x, paddingAmount, mainMenuMask.padding.z, mainMenuMask.padding.w);
            mainMenuMask.padding = padding;

            yield return null;
        }
        ResetMenu();
    }

    public IEnumerator CreditsScrollUp()
    {
        float startingScrollPos = credits.transform.position.y;
        isCreditScrolling = true;
        
        float scrollAmount;
        Vector3 pos;
        while (credits.transform.position.y < 375 && isCreditScrolling)
        {
            scrollAmount = credits.transform.position.y + (creditsScrollSpeed * Time.deltaTime);
            pos = new Vector3(credits.transform.position.x, scrollAmount, credits.transform.position.z);
            credits.transform.position = pos;
            yield return null;
        }

        credits.transform.position = new Vector3(credits.transform.position.x, startingScrollPos, credits.transform.position.z);

    }

    public IEnumerator RevealSkipCue()
    {
        skipCreditsText.SetActive(true);
        skipCreditsIcon.SetActive(true);

        Color textColor = skipCreditsText.GetComponent<TMP_Text>().color;
        Color iconColor = skipCreditsIcon.GetComponent<SpriteRenderer>().color;
        float fadeAmount;
        while (skipCreditsText.GetComponent<TMP_Text>().color.a<=1 || skipCreditsIcon.GetComponent<SpriteRenderer>().color.a<=1)
        {
            fadeAmount = textColor.a + (textAlphaFadeSpeed * Time.deltaTime);
            textColor = new Color(textColor.r, textColor.g, textColor.b, fadeAmount);
            iconColor = new Color(iconColor.r, iconColor.g, iconColor.b, fadeAmount);
            skipCreditsText.GetComponent<TMP_Text>().color = textColor;
            skipCreditsIcon.GetComponent<SpriteRenderer>().color = iconColor;
            yield return null;
        }

        skipCuePopped = true;
    }

    void ResetMenu()
    {
        skipCuePopped = false;
        creditsButton.interactable = true;
        
        //Reset Skip Credits Cue
        Color textColor = skipCreditsText.GetComponent<TMP_Text>().color;
        Color iconColor = skipCreditsIcon.GetComponent<SpriteRenderer>().color;
        textColor = new Color(textColor.r, textColor.g, textColor.b, 0);
        iconColor = new Color(iconColor.r, iconColor.g, iconColor.b, 0);
        skipCreditsText.GetComponent<TMP_Text>().color = textColor;
        skipCreditsIcon.GetComponent<SpriteRenderer>().color = iconColor;

        skipCreditsText.SetActive(false);
        skipCreditsIcon.SetActive(false);


    }

}
