using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    [SerializeField] GameObject[] roadPrefab;
    bool isGameActive = true;
    GameObject player;
    int collectedCoins = 0;
    [SerializeField] TextMeshProUGUI collectedCoinsText;
    [SerializeField] TextMeshProUGUI gameOverText;
    [SerializeField] Button restartBtn;

    public void OnRestartBtnPressed()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void UpdateCollectedCoins()
    {
        collectedCoins++;
        collectedCoinsText.SetText("Coins: " + collectedCoins.ToString());
    }

    public bool GetGameState()
    {
        return isGameActive;
    }

    public void SetGameState(bool gameState)
    {
        isGameActive = gameState;
    }

    void GenerateNewSegment()
    {
        if (isGameActive) {
            int randIndex = Random.Range(0, roadPrefab.Length);
            Instantiate(roadPrefab[randIndex],
                new Vector3(2.5f, -1.761234f, 37.7f),
                roadPrefab[randIndex].transform.rotation);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        restartBtn.gameObject.SetActive(false);
        gameOverText.gameObject.SetActive(false);
        InvokeRepeating("GenerateNewSegment", 5.0f, 4.0f);
        player = GameObject.Find("Player");
    }

    // Update is called once per frame
    void Update()
    {
        if(!isGameActive)
        {
            player.GetComponent<PlayerController>().enabled = false;
            //gameOverText.gameObject.SetActive(true);
            //restartBtn.gameObject.SetActive(true);
        }
    }
}
