using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour {


	public float timeLimit;
	public float downloadGoal;
	public Image downloadBar;
	public float downloadCounter;
	public Text timeCounterText;
	public GameState state;
	public GameObject player;
	public Text tulisan;
	public Text downloadProgress;

	private float timeCounter;

	public bool pause;

	public GameObject winpanel,gameoverpanel;


	void Start () {
		state = GameState.GAMEPLAY;
		timeCounter = 0;
		downloadCounter = 0;
		downloadBar.fillAmount = 0;
		downloadProgress.text = downloadCounter.ToString("2F")+" b of "+downloadGoal.ToString("2F")+" b downloaded";
	}
	
	// Update is called once per frame
	void Update () {
		switch (state) {
		case GameState.MAINMENU:
			break;
		case GameState.GAMESTART:
			break;
		case GameState.GAMEPLAY:

			UpdateGameplay();

			break;
		case GameState.GAMEWIN:
			break;
		case GameState.GAMEOVER:
			break;
		}
	}

	void UpdateGameplay(){
		if (!pause) {
			if (timeCounter < timeLimit) {
				timeCounter += Time.deltaTime;



				if (downloadCounter > downloadGoal) {
					state = GameState.GAMEWIN;
					player.GetComponent<Animator> ().SetTrigger ("win");
					StartCoroutine(winning());

				} else {
					downloadProgress.text = downloadCounter.ToString("F2")+" b of "+downloadGoal.ToString("F2")+" b downloaded";
					downloadBar.fillAmount = downloadCounter / downloadGoal;
					TimeSpan time = TimeSpan.FromSeconds (timeLimit - timeCounter);
					timeCounterText.text = string.Format ("{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds);
				}
			
			} else {
				state = GameState.GAMEOVER;
				player.GetComponent<Animator> ().SetTrigger ("death");
			}
		}

	}

	void youwin()
	{
		winpanel.SetActive(true);
	}
	
	void youlose()
	{
		gameoverpanel.SetActive(true);
	}

	IEnumerator winning(){
		yield return new WaitForSeconds (3);
		youwin ();
	}
	
	
}
public enum GameState{
	MAINMENU,
	GAMESTART,
	GAMEPLAY,
	GAMEWIN,
	GAMEOVER
}
