using UnityEngine;

public enum GameMode {
    Playing,
    GameOver,
    LevelUp
}

/*
 * Zum "Einstellen" der Positionen kann man das ExecuteInEditMode()
 * an dieser Stelle "einkommentieren" (also die "//" löschen und das
 * somit zu im Editor ausgeführtem Code machen. Das ist einfacher,
 * als das im Play-Mode zu versuchen. Im Editor kann man auch den
 * "Current Game Mode" leicht umstellen.
 */
//[ExecuteInEditMode()]
public class GameGUI : MonoBehaviour {

    public Rect applesPosition = new Rect(40, 100, 300, 40);
    public string applesText = "Nach {0:00} Sekunden {1} von {2} Aepfeln verspeist";

    public Rect levelUpPosition = new Rect(40, 100, 300, 40);
    public string levelUpText = "Yeah - du kommst in den naechsten Level!!!";

    public Rect gameOverPosition = new Rect(40, 40, 300, 40);
    public string gameOverText = "Game Over!!!";

    public GameMode currentGameMode = GameMode.Playing;

    public GUISkin skin = null;

    private int appleCount = 0;
    private Transform apples = null;

    public void Start() {
        currentGameMode = GameMode.Playing;

        apples = GameObject.Find("Apples").transform;
        if (apples == null) {
            Debug.LogWarning("Konnte keine Aepfel finden!");
        } else {
            appleCount = apples.childCount;
        }
    }

    public void OnGUI() {
        GUI.skin = skin;

        switch (currentGameMode) {
            case GameMode.Playing:
                RenderPlayingHUD();
                break;
            case GameMode.LevelUp:
                RenderLevelUp();
                break;
            case GameMode.GameOver:
                RenderGameOver();
                break;
        }
    }

    private string playingHUDCache = null;
    private float timeStamp = 0F;

    private void RenderPlayingHUD() {
        if (playingHUDCache == null || timeStamp + 1F > Time.time) {
            playingHUDCache = string.Format(applesText,
                Time.timeSinceLevelLoad,
                appleCount - apples.childCount,
                appleCount);
            timeStamp = Time.time;
        }
        GUI.Label(applesPosition, playingHUDCache);
    }

    private void RenderLevelUp() {
        GUI.Label(levelUpPosition, levelUpText);
        // add a button and key-code to immediately jump to next level
    }

    private void RenderGameOver() {
        GUI.Label(gameOverPosition, gameOverText);
        // add a button and key-code to immediately jump to main menu
    }
}
