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

public class ButtonMashUI : MonoBehaviour
{
	[SerializeField] private Image rainbow1;
	[SerializeField] private Image rainbow2;
	[SerializeField] private Image portrait1Place;
	[SerializeField] private Image portrait2Place;
	[SerializeField] private Image buttonPlace1;
	[SerializeField] private Image buttonPlace2;
	[SerializeField] private Sprite[] portraits;
	[SerializeField] private RuntimeAnimatorController[] catBars;
	[SerializeField] private Sprite[] catBarSprites;
	[SerializeField] private Sprite buttonUp;
	[SerializeField] private Sprite buttonDown;
	[SerializeField] private bool shakePortrait;
	[SerializeField] private float shakeAmount;
	[SerializeField] private GameObject blackBG;
	[SerializeField] private Image whiteFlash;

	[SerializeField] private RectTransform barCont;

	public static ButtonMashUI Instance { get; private set; }

	private void Awake ()
    {
        //Debug.Log(GetValueInRange(15, 0, 30, 0, 15));


        if (Instance == null)
        {
            Instance = this;
            //DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(this.gameObject);
        }
    }

	private bool runMash;

	private void Update()
	{
		if (runMash)
		{
			if (shakePortrait)
			{
				ShakeObject(portrait1Place.rectTransform);
				ShakeObject(portrait2Place.rectTransform);
			}

			float sign = Mathf.Sign(p1Points - p2Points);

			barCont.anchoredPosition = Vector2.Lerp(
				barCont.anchoredPosition,
				new Vector2(Mathf.Exp((p1Points + p2Points) / 10) * sign, 0),
				Time.deltaTime
				);
			//barCont.anchoredPosition = new Vector2(Mathf.Exp((p1Points + p2Points) / 10) * sign, 0);

			if (barCont.anchoredPosition.x >= 550)
			{
				StartCoroutine(WhiteFlash2(idX1, idX1, idX2));
			}

			if (barCont.anchoredPosition.x <= -550)
			{
				StartCoroutine(WhiteFlash2(idX2, idX1, idX2));
			}
		}
	}

	private void ShakeObject(RectTransform obj)
	{
		obj.anchoredPosition = obj.anchoredPosition + new Vector2(Mathf.Sin(Time.time * shakeAmount), Mathf.Cos(Time.time * shakeAmount));
	}

	private int idX1;
	private int idX2;

	public void InitButtonMash(int id1, int id2)
	{
		Debug.Log("A");
		runMash = true;
		GameStart.Instance.GetPlayerFromID(id1).Input.mashID = 0;
		GameStart.Instance.GetPlayerFromID(id2).Input.mashID = 1;
		idX1 = id1;
		idX2 = id2;
		StartCoroutine(WhiteFlash(id1, id2));
	}

	private IEnumerator WhiteFlash(int id1, int id2)
	{
		whiteFlash.enabled = true;
		yield return new WaitForSeconds(0.1f);
		whiteFlash.enabled = false;
		blackBG.SetActive(true);
		ToggleShake(true);
		SetBars(id1, id2);
	}

	private IEnumerator WhiteFlash2(int winnerID, int id1, int id2)
	{
		runMash = false;
		whiteFlash.enabled = true;
		yield return new WaitForSeconds(0.1f);
		whiteFlash.enabled = false;
		ToggleShake(false);
		blackBG.SetActive(false);
		p1Points = p2Points = 0;
		barCont.anchoredPosition = new Vector2(0, 0);
		GameStart.Instance.EndButtonMash(winnerID, id1, id2);
	}

	public void ToggleShake(bool b)
	{
		shakePortrait = b;
	}

	public Animation anima;

	public void SetBars(int id1, int id2)
	{
		rainbow1.sprite = catBarSprites[id1];
		rainbow2.sprite = catBarSprites[id2];

		// rainbow1.runtimeAnimatorController = catBars[id1];
		// rainbow2.runtimeAnimatorController = catBars[id2];

		// // rainbow1A.AddClip(barClips[id1], "A");
		// // rainbow2A.AddClip(barClips[id2], "B");
		// // rainbow1A.Play(barClips[id1].name);
		// // rainbow2A.Play(barClips[id2].name);

		// rainbow1.SetTrigger("Play");
		// rainbow2.SetTrigger("Play");
		// Debug.Log(rainbow1.runtimeAnimatorController.animationClips[0]);
		// Debug.Log(rainbow2.runtimeAnimatorController.animationClips[0]);
		portrait1Place.sprite = portraits[id1];
		portrait2Place.sprite = portraits[id2];

		//anima.Play(anima.clip.name);
	}

	public void ButtonDown(int i)
	{
		if (i == 0)
		{
			buttonPlace1.sprite = buttonDown;
			p1Points++;
		}
		else if (i == 1)
		{
			buttonPlace2.sprite = buttonDown;
			p2Points++;
		}
	}
	public void ButtonUp(int i)
	{
		if (i == 0)
		{
			buttonPlace1.sprite = buttonUp;
		}
		else if (i == 1)
		{
			buttonPlace2.sprite = buttonUp;
		}
	}

	public float GetValueInRange(float value, float myMinRange, float myMaxRange, float newMinRange, float newMaxRange)
    {
        return (value - myMinRange) * (newMaxRange - newMinRange)/(myMaxRange - myMinRange) + newMinRange;
    }

	public int p1Points;
	public int p2Points;
}
