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

public class LevelButton : MonoBehaviour {
    public static int CurrentLevelIndex{
        get{
            return currentLevelIndex;
        }
    }

    private static int currentLevelIndex = -1;
    private int levelIndex;

    private float progress;
    private bool appeared;

    public void SetLevel(int index)
    {
        GetComponent<Button>().interactable = false;
        progress = index + 4;
        string tmp = index < 9 ? "0" : "";
        GetComponentInChildren<Text>().text = "LV " + tmp + (index + 1).ToString();

        transform.localScale = Vector3.one * 0.2f;
        GetComponentInChildren<Text>().color = new Color(0, 0, 0, 0);
        appeared = false;
        levelIndex = index;
    }

    void Update()
    {
        if (appeared)
        {
            return;
        }
        progress -= Time.deltaTime * 12;
        if (progress < 3.5f)
        {
            if (progress < 0)
            {
                progress = 0;
                appeared = true;
                GetComponent<Button>().interactable = true;
            }
            GetComponentInChildren<Text>().color = new Color(0, 0, 0, 1 - progress * 0.5f);
            transform.localScale = Vector3.one * (1 - (progress * progress * 0.5f - progress) * 0.25f);
        }
    }

    public void LoadLevel()
    {
        currentLevelIndex = levelIndex;
        SceneManager.LoadScene("Level");
    }

    public static void LoadLevel(int index)
    {
        currentLevelIndex = index;
        SceneManager.LoadScene("Level");
    }

}
