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

public class GameManager : MonoBehaviour
{
    public static GameManager instance;
    public bool isgamestarted;
    public GameObject platformspawner;
    [Header("Score")]
    public Text scoreText;
    public Text besttext;
    public Text diamondtext;
    public Text startext;

    int score = 0;
    int bestscore, totaldiamond, totalstar;

    [Header("GameOver")]
    public GameObject gameoverpanel;
    public Text lastscoretext;

    bool Countscore;


    private void Awake()
    {
        if(instance==null)
        {
            instance = this;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        //score and bestscore
        bestscore = PlayerPrefs.GetInt("bestscore");
        besttext.text = bestscore.ToString();

        //total star
        totalstar = PlayerPrefs.GetInt("total star");
        startext.text = totalstar.ToString();

        //total diamond
        totaldiamond = PlayerPrefs.GetInt("total diamond");
        diamondtext.text = totaldiamond.ToString();


        
    }

    // Update is called once per frame
    void Update()
    {
        if (!isgamestarted)
        {
            if (Input.GetMouseButtonDown(0))
            {
                gamestart();

            }
        }
    }
    void gamestart()
    {
        isgamestarted = true;
        Countscore = true;

        StartCoroutine(updatescore());
        platformspawner.SetActive(true);
    }
    public void gameover()
    {
        gameoverpanel.SetActive(true);
        lastscoretext.text = score.ToString();

        Countscore = false;
        
        platformspawner.SetActive(false);
        
        if(score>bestscore)
        {
            PlayerPrefs.SetInt("bestscore", score);
        }
    }
    IEnumerator updatescore()
    {
        while (Countscore)
        {
            yield return new WaitForSeconds(1f);
            score++;
            if (score > bestscore)
            {
                scoreText.text = score.ToString();
                besttext.text = score.ToString();
            }
            else
            {
                besttext.text = score.ToString();
            }
        }
    }
}
