using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using DG.Tweening;

public class MenuMaster : MonoBehaviour
{
	public Transform character1;
	public Transform character2;
	public Transform logoBack;
	public Transform logo;

	public Image play;
	public Image credits;
	public Image loadFill;
	
	private bool[] fingersOut;
	private bool isSelected = false;
	private float timeSelect = 1.5f;

	private void Start()
	{
		AudioMaster.StartMenuSong();

		if (character1 != null)
		{
			character1.DOMoveX(0.2f, 0.4f).SetRelative().SetEase(Ease.Linear).SetLoops(-1, LoopType.Yoyo);
			character1.DOMoveY(0.2f, 0.4f).SetRelative().SetEase(Ease.OutQuart).SetLoops(-1, LoopType.Yoyo);
		}

		if (character2 != null)
		{
			character2.DOMoveX(-0.2f, 0.4f).SetRelative().SetEase(Ease.Linear).SetLoops(-1, LoopType.Yoyo);
			character2.DOMoveY(-0.2f, 0.4f).SetRelative().SetEase(Ease.InQuart).SetLoops(-1, LoopType.Yoyo);
		}

		if (play != null)
			play.rectTransform.DOMoveY(0.25f, 1.0f).SetLoops(-1, LoopType.Yoyo).SetRelative().SetEase(Ease.InOutQuad);
		if (credits != null)
			credits.rectTransform.DOMoveY(0.25f, 1.0f).SetDelay(0.2f).SetLoops(-1, LoopType.Yoyo).SetRelative().SetEase(Ease.InOutQuad);

		Sequence logoShake = DOTween.Sequence();
		logoShake.Append(logo.DOShakePosition(0.8f, 0.2f, 30));
		logoShake.Join(logoBack.DOShakePosition(0.8f, 0.05f, 20).SetDelay(0.2f));
		if (play != null)
			logoShake.Join(play.rectTransform.DOShakeRotation(1.0f, 2.5f, 30).SetDelay(0.1f));
		if (credits != null)
			logoShake.Join(credits.rectTransform.DOShakeRotation(1.0f, 2.5f, 30).SetDelay(0.1f));
		logoShake.AppendInterval(0.6f);
		logoShake.SetLoops(-1);
		
		fingersOut = new bool[5];
	}

	private void Update()
	{
		fingersOut[0] = !Input.GetButton("Thumb");
		fingersOut[1] = !Input.GetButton("Index");
		fingersOut[2] = !Input.GetButton("Middle");
		fingersOut[3] = !Input.GetButton("Ring");
		fingersOut[4] = !Input.GetButton("Pinky");

		if (!isSelected && fingersOut[1] && !fingersOut[0] && !fingersOut[3] && !fingersOut[4])
		{
			if (!fingersOut[2])
			{
				StartCoroutine(HoldPlayButton());
			}
			else if (SceneManager.GetActiveScene().name == "Menu")
			{
				StartCoroutine(HoldCreditsButton());
			}
		}
	}

	private IEnumerator HoldPlayButton()
	{
		isSelected = true;
		Sequence hold = DOTween.Sequence();
		hold.Append(play.rectTransform.DOScale(1.1f, timeSelect));
		hold.Join(play.rectTransform.DOShakePosition(timeSelect, 5.0f, 80, 90, false));
		if (credits != null)
		{
			hold.Join(credits.rectTransform.DOScale(0.8f, timeSelect));
			hold.Join(credits.DOColor(Color.gray, timeSelect));
		}
		hold.Join(loadFill.DOFillAmount(1.0f, timeSelect));
		yield return new WaitForEndOfFrame();
		
		float timer = 0.0f;
		while (!fingersOut[0] && fingersOut[1] && !fingersOut[2] && !fingersOut[3] && !fingersOut[4])
		{
			timer += Time.deltaTime;
			if (timer >= timeSelect)
			{
				SceneManager.LoadScene("Main Scene");
			}
			yield return new WaitForEndOfFrame();
		}

		isSelected = false;
		EventSystem.current.SetSelectedGameObject(null);
		hold.Kill();
		play.rectTransform.localScale = Vector3.one;
		if (credits != null)
		{
			credits.rectTransform.localScale = Vector3.one;
			credits.color = Color.white;
		}
		loadFill.fillAmount = 0.0f;
		yield return null;
	}

	private IEnumerator HoldCreditsButton()
	{
		isSelected = true;
		Sequence hold = DOTween.Sequence();
		hold.Append(credits.rectTransform.DOScale(1.1f, timeSelect));
		hold.Join(credits.rectTransform.DOShakePosition(timeSelect, 5.0f, 80, 90, false));
		hold.Join(play.rectTransform.DOScale(0.8f, timeSelect));
		hold.Join(play.DOColor(Color.gray, timeSelect));
		hold.Join(loadFill.DOFillAmount(1.0f, timeSelect));
		yield return new WaitForEndOfFrame();
		
		float timer = 0.0f;
		while (!fingersOut[0] && fingersOut[1] && fingersOut[2] && !fingersOut[3] && !fingersOut[4])
		{
			timer += Time.deltaTime;
			if (timer >= timeSelect)
			{
				SceneManager.LoadScene("Credits");
				yield break;
			}
			yield return new WaitForEndOfFrame();
		}

		isSelected = false;
		EventSystem.current.SetSelectedGameObject(null);
		hold.Kill();
		credits.rectTransform.localScale = Vector3.one;
		play.rectTransform.localScale = Vector3.one;
		play.color = Color.white;
		loadFill.fillAmount = 0.0f;
		yield return null;
	}
}
