using GoogleMobileAds.Sample;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;


public class UiManager : MonoBehaviour
{
    public MenuControler menuControler;
    public PlayerMovement playerMovement;
    public PLayerLife playerLife;
    public soundManager soundmanager;
    public GameObject PauseMenu;
    public bool paused;
    public bool gameOver;
    public bool gameOverAnimPlayed;
    public score scr;
    public GameObject playerModels;
    public int adindex;
    public Animation gameOverAnim;

    [SerializeField] InterstitialAdController interstitialAdController;

    [SerializeField] private PlayGamesManager PGM;


    void Start()
    {
        adindex = PlayerPrefs.GetInt("adIndex");
    }

    void Update()
    {
        PauseMenu.SetActive(menuControler.playing && paused);

        if(paused) Time.timeScale = 0;
        else Time.timeScale = 1;

        if (playerLife.Hp <= 0 )
        {
            paused = false;
            gameOver = true;
        }

        if( gameOver && !gameOverAnimPlayed) 
        {
            playerModels.SetActive(false);
            playerMovement.enabled = false;
            gameOverAnim.Play();

            if (adindex >= 5)
            {
                //  SHOW INTERSTITIAL AD
                interstitialAdController.ShowAd();

                PlayerPrefs.SetInt("adIndex", 0);
                adindex = 0;
            }
            else
            {
                PlayerPrefs.SetInt("adIndex", adindex + 1);
                adindex++;
            }

            // Do stuff when player dies;

            // Save data
            PGM.OpenSave(true);

            gameOverAnimPlayed = true;
        }

        scr.enabled = !gameOver;

        if (!gameOver)
            gameOverAnim.gameObject.transform.localScale = Vector3.zero;

    }

    public void Pause(bool state)
    {
        paused = state;
        soundmanager.PlayClick1();
    }

}