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

public class ScorecounterScript : MonoBehaviour
{
    private int score;
    private int hiScore;
    private int lives;
    public Text scoreText;
    public Text hiscoreText;
    public Text gameOverSign;
    public Text livesText;
    bool gameOver;

    // Start is called before the first frame update
    void Start()
    {
        gameOver = false;
        score = 0;
        lives = 3;
        hiScore = PlayerPrefs.GetInt("HiScore");
        updateScoreBoard();
        updateLivesBoard(gameObject);
    }

    public void OnFire(InputAction.CallbackContext context)
    {
        if (gameOver)
        {
            SceneManager.LoadScene(0);
        }
    }

    public void updateScore(int value)
    {
        score += value;
        if (score > hiScore)
        {
            hiScore = score;
        }
        updateScoreBoard();
    }

    public void updateLives(int n,GameObject player)
    {
        lives += n;
        updateLivesBoard(player);
    }

    public void updateScoreBoard()
    {
        if (scoreText != null && hiscoreText!=null)
        {
            scoreText.text = "SCORE: " + score.ToString("D8");
            hiscoreText.text = "HI-SCORE: " + hiScore.ToString("D8");
        }
    }

    void updateLivesBoard(GameObject player)
    {
        if (lives < 1 && gameOverSign != null)
        {
            Destroy(player);
            gameOverSign.gameObject.SetActive(true);
            PlayerPrefs.SetInt("HiScore", hiScore);
            StartCoroutine(waitForNSeconds(3f));
            gameOver = true;
        }
        if (livesText != null)
        {
            livesText.text = "LIVES: " + lives.ToString();
        }

    }
    IEnumerator waitForNSeconds(float n)
    {
        //do something here
        yield return new WaitForSeconds(n);

    }
}
