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

public class WinningPanel : TutorialPanel
{
    [HideInInspector] public int playerWinner;
    private bool initialized;
    public Image panelImage;
    public Text text;

    protected override void OnEnable()
    {
        base.OnEnable();
        initialized = false;
        SetAlpha(panelImage, 0);
        SetAlpha(text, 0);
        pressAnyKey.gameObject.SetActive(false);

        panelImage.CrossFadeAlpha(1, 3f, false);
        FadeInText();
        Debug.Log("WinningPanel.OnEnable()");
    }

    private IEnumerator FadeInText()
    {
        yield return new WaitForSeconds(3f);

        float t = 0;
        while (t < 1)
        {
            SetAlpha(text, Mathf.Lerp(0, 1, t));
            t += Time.deltaTime;
            yield return null;
        }

        SetAlpha(pressAnyKey, 1);
        pressAnyKey.gameObject.SetActive(true);
    }

    private void Update()
    {
        if (!initialized)
        {
            initialized = true;

            if (playerWinner == 1)
            {
                panelImage.color = new Color(0, 0, 0.5f, panelImage.color.a);

                text.text = "Player 1, " + text.text;
            }
            else
            {
                panelImage.color = new Color(0.5f, 0, 0, panelImage.color.a);

                text.text = "Player 2, " + text.text;
            }
        }
    }

    public override void Dismiss()
    {
        UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
    }
}
