﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameController : MonoBehaviour {

	private int robotsLeft = 3;
	private string sceneName;
	public static GameController instance = null;

	// Use this for initialization
	void Start () {
		if (instance == null) {
			instance = this;
		} else if (instance != this) {
			Destroy(gameObject);
		}
		DontDestroyOnLoad(gameObject);
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	public void Break () {
		robotsLeft -= 1;
		Debug.Log(robotsLeft);
		Invoke("RestartLevel", 3f);
	}

	private void RestartLevel () {
		sceneName = SceneManager.GetActiveScene().name;
		if (robotsLeft > 0) {
			SceneManager.LoadScene(sceneName);
		} else {
			SceneManager.LoadScene("GameOver");
		}
	}

	public void LevelEnd () {
		robotsLeft += 1;
		Debug.Log(robotsLeft);
		Invoke("NextLevel", 3f);
	}

	private void NextLevel () {
		int sceneIndex = SceneManager.GetActiveScene().buildIndex;
		SceneManager.LoadScene(sceneIndex+1);
	}
}
