using UnityEngine;
using UnityEngine.UI;

public class InGameGUIController : MonoBehaviour {

    public Text remainingTime;
    public Text collectedApples;
    public Text sessionDoneMessage;
    public Button buttonMainMenu;

    public void Start() {
        ToggleSessionOverVisibility(false);
    }

    public void ToggleSessionOverVisibility(bool visible) {
        sessionDoneMessage.gameObject.SetActive(visible);
        buttonMainMenu.gameObject.SetActive(visible);
    }

    public void OpenMainMenu() {
        Application.LoadLevel("AStartmenu");
    }

    public void UpdateTime() {
        string text
          = Localizer.Instance.GetText("InGameGUI.RemainingTime",
                GameStateManager.Instance.RemainingTimeString);
        remainingTime.text = text;
    }

    public void AppleCollected() {
        UpdateCollectedApples();
    }

    private void UpdateCollectedApples() {
        string text
          = Localizer.Instance.GetText("InGameGUI.CollectedApples",
                GameStateManager.Instance.CollectedApplesCount,
                GameStateManager.Instance.AllApplesCount);
        collectedApples.text = text;
    }

    public void GameStateChanged() {
        switch (GameStateManager.Instance.CurrentGameState) {
            case GameState.Playing:
                UpdateCollectedApples();
                break;
            case GameState.PlayerCrashed:
            case GameState.TimedOut: 
                sessionDoneMessage.text
                  = Localizer.Instance.GetText("InGameGUI.GameOver");
                ToggleSessionOverVisibility(true);
                break;
            case GameState.AllApplesCollected:
                sessionDoneMessage.text
                  = Localizer.Instance.GetText("InGameGUI.ApplesDone");
                ToggleSessionOverVisibility(true);
                break;
        }
    }
}
