using System;
using UnityEngine;
using UnityEngine.UI;

public class InGameGUIController : MonoBehaviour {

    public Text currentScore;
    public Text remainingTime;

    public AppleProgressGroup progressGroup;

    public Text sessionDoneMessage;
    public Button buttonMainMenu;

    public bool overrideNonTouch = false;
    public Button buttonTurnLeft;
    public Button buttonTurnRight;

    void Awake() {
        ActivateForTouchOnly(buttonTurnLeft.gameObject);
        ActivateForTouchOnly(buttonTurnRight.gameObject);
    }

    private void ActivateForTouchOnly(GameObject obj) {
        obj.SetActive(overrideNonTouch || Input.touchSupported);
    }

    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 UpdateScore(int score) {
        string text
              = Localizer.Instance.GetText(
                             "InGameGUI.CurrentScore", score);
        currentScore.text = text;
    }

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

    public void AppleCollected() {
        UpdateCollectedApples();
    }

    private void UpdateCollectedApples() {
        GameStateManager gsm = GameStateManager.Instance;
        foreach (AppleCategory cat in Enum.GetValues(typeof(AppleCategory))) {
            int collectedCount = gsm.CollectedApplesCount(cat);
            progressGroup.MarkProgress(cat, collectedCount);
        }
    }

    private void UpdateApples(AppleCategory category, Text field) {
        GameStateManager gsm = GameStateManager.Instance;

        string key = string.Format(
            "InGameGUI.CollectedApples.{0}",
            category.ToString());

        if (gsm.ApplesCount(category) > 0) {
            string text = Localizer.Instance.GetText(key,
                gsm.CollectedApplesCount(category),
                gsm.ApplesCount(category));
            field.text = text;
        } else {
            field.gameObject.SetActive(false);
        }

    }

    public void GameStateChanged() {
        switch (GameStateManager.Instance.CurrentGameState) {
            case GameState.Playing:
                InitAppleProgress();
                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;
        }
    }

    private void InitAppleProgress() {
        GameStateManager gsm = GameStateManager.Instance;
        foreach (AppleCategory cat in Enum.GetValues(typeof(AppleCategory))) {
            int count = gsm.ApplesCount(cat);
            progressGroup.InitCategoryProgress(cat, count);
        }
    }


}
