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

public class GameManager : MonoBehaviour
{
    public static GameManager instance;
    public float worldScrollingSpeed = 0.2f;

    // pole w ktorym bedziemy pamietc czy aktualnei trwa gra
    public bool inGame;
    // pole do popdpiecia przycisku rest
    public Button resetButton;

    public Text scoreText;
    public Text coinScoreText;
    public Text highScoreText;
    // Powerup Niesmiertelnosc
    public Immortality immortality;
    public Magnet magnet;
    //Pole na najlepszy wynij
    private int highScoreValue;

    //Pole na wynik
    private float score;
    // Pole na ilosc coinsow
    private int coins;
    // Start is called before the first frame update
    void Start()
    {
        if (instance == null) { instance = this; }

        InitalizeGame();
    }

    public void GameOver()
    { 
        inGame = false;
        resetButton.gameObject.SetActive(true);

        if ((int)score > highScoreValue)
        {
            highScoreValue = (int)score;
            PlayerPrefs.SetInt("HighScoreValue", highScoreValue);
        }
    }

    public void RestartGame()
    {
        //ponowwnie ladujemy scene o indeksie 0(czyli jedyna w naszje grze

        SceneManager.LoadScene("Game");
    }
    void InitalizeGame()
    {
        inGame = true;

        if (PlayerPrefs.HasKey("Coins"))
            coins = PlayerPrefs.GetInt("Coins");
        else 
        { 
            coins = 0;
            PlayerPrefs.SetInt("Coins", 0);
        }
        if (PlayerPrefs.HasKey("HighScoreValue"))
            highScoreValue = PlayerPrefs.GetInt("HighScoreValue");
        else
        { 
            highScoreValue = 0;
            PlayerPrefs.SetInt("HighScoreValue", 0);
        }
    
        UpdateScreenScore();
        //resetButton.onClick.AddListener(RestartGame);
        immortality.isActive = false;
        magnet.isActive = false;
    }

    void FixedUpdate()
    {
        if (!GameManager.instance.inGame) return;
        score += worldScrollingSpeed;

        if ((int)score > highScoreValue)
        {
            highScoreValue = (int)score;
            PlayerPrefs.SetInt("HighScoreValue", highScoreValue);
        }

        UpdateScreenScore();
    }

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

    void UpdateScreenScore()
    {
        scoreText.text = score.ToString("0");
        coinScoreText.text = coins.ToString();
        highScoreText.text = highScoreValue.ToString();
    }
    public void CoinCollected(int value = 1)
    {
        coins += value;
        PlayerPrefs.SetInt("Coins", coins);
        UpdateScreenScore();
    }
    public void ImmortalityCollected()
    {
        if (immortality.isActive)
        {
            CancelInvoke("CancelImmortality");
            CancelImmortality();
        }
        immortality.isActive = true;
        worldScrollingSpeed += immortality.GetSpeedBoost();
        Invoke("CancelImmortality", immortality.GetDuration());
    }
    public void MagnetCollected()
    {
        if (magnet.isActive)
        {
            CancelInvoke("CancelMagnet");
        }
        magnet.isActive = true;
        Invoke("CancelMagnet", magnet.GetDuration());
    }

    private void CancelImmortality()
    {
        immortality.isActive = false;
        worldScrollingSpeed -= immortality.GetSpeedBoost();
    }
    private void CancelMagnet()
    {
        magnet.isActive = false;
    }
}
