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

public class Levels : MonoBehaviour
{
    public string menuSceneName;
    public string creditSceneName;
    public string[] levelNames;

    public void LoadNextLevel()
    {
        Scene active = SceneManager.GetActiveScene();

        int next = -1;
        for (int i = 0; i < this.levelNames.Length; i++)
        {
            if (active.name == this.levelNames[i])
            {
                next = i + 1;
                break;
            }
        }

        if (next == -1)
        {
            print("Could not find next scene!");
            this.LoadScene(this.menuSceneName);
        } else if (next >= this.levelNames.Length)
        {
            print("No more levels");
            this.LoadScene(this.menuSceneName);
        } else
        {
            this.LoadScene(this.levelNames[next]);
        }
    }

    public void ReloadScene()
    {
        Scene active = SceneManager.GetActiveScene();
        this.LoadScene(active.name);
    }

    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            LoadMenu();
        }
        else if (Input.GetKeyUp(KeyCode.Return))
        {
            ReloadScene();
        }
    }

    public void LoadMenu()
    {
        Scene active = SceneManager.GetActiveScene();
        if (active.name != this.menuSceneName)
        {
            this.LoadScene(this.menuSceneName);
        }
    }

    public void LoadCredits()
    {
        Scene active = SceneManager.GetActiveScene();
        if (active.name != this.creditSceneName)
        {
            this.LoadScene(this.creditSceneName);
        }
    }

    public void CloseGame()
    {
        Application.Quit();
    }

    public void LoadLevelNumber(int n)
    {
        if (n >= 0 && n < this.levelNames.Length)
        {
            this.LoadScene(this.levelNames[n]);
        }
    }

    private void LoadScene(string scene)
    {
        print("Loading scene " + scene);
        SceneManager.LoadScene(scene, new LoadSceneParameters(LoadSceneMode.Single));
    }
}
