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

public class GameManager : MonoBehaviour {

    public int lifeLossFrequency;
    public int goalEnergy;
    public Sonic senseSonic;
    public Sonic attackSonic;
    public playerctrl player;

    private bool isOver;
    private int energy;
    private static GameManager _instance;

    public static GameManager Instance {
        get {
            if (_instance == null) {
                _instance = FindObjectOfType<GameManager>();
            }
            return _instance;
        }
    }

    private void Awake () {
        isOver = false;
        energy = 0;
    }

    private void Start () {
        StartCoroutine(lifeLoss());
    }

    private void Update () {
        if (Input.GetKeyDown(KeyCode.Escape)) {
            SceneManager.LoadScene("Menu");
        }
    }

    public void GetEnergy () {
        energy++;
    }

    public int GetCurrentEnergy () {
        return energy;
    }

    public int GetGoalEnergy () {
        return goalEnergy;
    }

    public float GetNormalizedPower (float value) {
        return 40f + value * 40f;
    }

    public float GetNormalizedDuration (float value) {
        return 2f + value * 3f;
    }

    public Sonic GetSenseSonic () {
        return senseSonic;
    }

    public Sonic GetAttackSonic () {
        return attackSonic;
    }

    public Vector3 GetPlayerPosition () {
        return player.transform.position;
    }

    public void UpdateVictory () {
        if (!isOver && energy >= goalEnergy) {
            player.gameObject.SetActive(false);
            UIManager.Instance.DisplayVictory();
            UIManager.Instance.DisplayMenu();
            isOver = true;
            print("Victory");
        }
    }

    public void UpdateGameOver () {
        if (!isOver && player.health <= 0) {
            player.gameObject.SetActive(false);
            UIManager.Instance.DisplayGameOver();
            UIManager.Instance.DisplayMenu();
            isOver = true;
            print("Game Over");
        }
    }

    public void LoadMenu () {
        SceneManager.LoadScene("Menu");
    }

    private IEnumerator lifeLoss () {
        WaitForSeconds wait = new WaitForSeconds(lifeLossFrequency);
        while (true) {
            player.Damaged(1);
            yield return wait;
        }
    }

}
