using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Threading.Tasks;

public class GameManager : MonoBehaviour
{
    public TMP_Text usernameTxt;
    public TMP_Text[] coinsTxt;
    public TMP_Text[] gemsTxt;
    public TMP_Text metalTxt;
    public TMP_Text oxygenTxt;
    public TMP_Text levelTxt;
    public Slider levelSlider;

    public GameObject loadingScreen;
    void Start()
    {
        GameManagerInstance.gameManager = this;
        

        //Go back to login if not logged
        if(!DBmanager.LoggedIn){
            SceneManager.LoadScene(0);
        }else{
            usernameTxt.text = DBmanager.username;
            RefreshData();
        }

        loadingScreen.SetActive(false);

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void RefreshData(){
        foreach(TMP_Text txt in coinsTxt){
            txt.text = DBmanager.Coins.ToString();
        }
        foreach(TMP_Text txt in gemsTxt){
        txt.text = DBmanager.Gems.ToString();
        }
        metalTxt.text = DBmanager.Metal.ToString();
        oxygenTxt.text = DBmanager.Oxygen.ToString();
        levelTxt.text = Mathf.CeilToInt(DBmanager.Level).ToString();

        float levelExcess = DBmanager.Level - Mathf.FloorToInt(DBmanager.Level);
        levelSlider.value = Mathf.Clamp(levelExcess, 0.1f,1);
    }

    public async void LoadMinigame(){
        loadingScreen.SetActive(true);
        await Task.Delay(1000); 
        SceneManager.LoadScene("MinigameMenu");
    }
}

public static class GameManagerInstance{
    public static GameManager gameManager;
}
