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

public class CanvasManager : MonoBehaviour {
    public static CanvasManager Instance;

    public Text levelText;
    public Text scoreText;
    public Text oneScore;
    public Text sliceText;
    public Text minimumScoreText;
    public Image minimumContainer;
    public Text outcomeText;
    public Image outcomeContainer;
    public Text highScoreText;
    public Image sliceType;
    public Sprite[] sliceTypeSprites;

    public Transform resultsScreen;

    private bool showEnding;
    private bool victory;

    void Awake()
    {
        Instance = this;
        showEnding = false;
    }

    void Update()
    {
        float tmp = oneScore.transform.localScale.x;
        tmp = tmp * 0.9f + 0.05f;
        oneScore.transform.localScale = Vector3.one * tmp;

        if (showEnding)
        {
            tmp = minimumContainer.rectTransform.localPosition.y;
            minimumContainer.rectTransform.localPosition = new Vector3(0, tmp * 0.95f, 0);
            tmp = outcomeContainer.rectTransform.localPosition.y;
            outcomeContainer.rectTransform.localPosition = new Vector3(0, tmp * 0.95f, 0);
        }
    }

    public void ShowOneScore(int score)
    {
        oneScore.text = "+" + score.ToString();
        oneScore.transform.localScale = Vector3.one;
    }

    public void ShowTotalScore(int score)
    {
        scoreText.text = "TOTAL:\n" + score.ToString();
    }

    public void ShowSlice(int slice)
    {
        sliceText.text = "SLICES: " + slice.ToString();
    }

    public void Restart()
    {
        GameManager.Instance.Restart();
        resultsScreen.gameObject.SetActive(false);
        showEnding = false;
    }

    public void SetSliceType(SliceType type)
    {
        sliceType.sprite = sliceTypeSprites[(int)type];
    }

    public void ShowLevel(LevelConfig config)
    {
        minimumScoreText.text = "MINIMUM:\n" + config.MinScore().ToString();
        levelText.text = "LEVEL " + (LevelButton.CurrentLevelIndex + 1).ToString();
        //highScoreText.text = "HIGH SCORE: " + config.highScore.ToString();
    }

    public void ToMenu()
    {
        SceneManager.LoadScene("Menu");
    }

    public void ShowResults()
    {
        resultsScreen.gameObject.SetActive(true);
        minimumContainer.rectTransform.localPosition = new Vector3(0, 100, 0);
        outcomeContainer.rectTransform.localPosition = new Vector3(0, -100, 0);
    }

    public void ShowMinimum()
    {
        showEnding = true;
    }

    public void Win(bool win)
    {
        outcomeText.text = win ? "PASSED!\n<NEXT LEVEL>" : "FAILED!\n<RESTART LEVEL>";
        victory = win;
    }

    public void Outcome()
    {
        if (victory)
        {
            LevelButton.LoadLevel(LevelButton.CurrentLevelIndex + 1);
        } else
        {
            Restart();
        }
    }
}
