using System;
#if UNITY_EDITOR
using UnityEditorInternal;
#endif
using UnityEngine;
using UnityEngine.SceneManagement;

public class BlackBoard : MonoSingleton<BlackBoard>
{

    public int itemsFixed = 0;
    public int totalItemsToFix = 0;
    public event Action onPlayerHide;
    public event Action onPlayerVisible;
    [SerializeField]private bool m_playerHidding = false;
    private GameObject m_player;
    public Vector3 playerPos { get { return m_player.transform.position; } }
    public  Transform player { get { return m_player.transform; } }

    public GameObject winScreen;
    private Timer m_timer;
    private bool win = false;
    public AudioSource WinSound;

    // Start is called before the first frame update
    void Start()
    {
        m_player = GameObject.FindGameObjectWithTag("Player");
    }

    public void PlayerHide(bool _hiding)
    {
        m_playerHidding = _hiding;
        if(m_playerHidding)
            onPlayerHide?.Invoke();
        else
            onPlayerVisible?.Invoke();
    }

    public void FixItems()
    {
        itemsFixed++;
        if (itemsFixed >= totalItemsToFix)
        {
            m_timer = new Timer(3f, Time.time);
            winScreen.SetActive(true);
            win = true;
            WinSound.Play();
        }
    }

    private void Update()
    {
        if(!win)
            return;
        
        if(m_timer.IsFinished())
            SceneManager.LoadScene("MainMenu");
    }

    public void EndGame()
    {
        SceneManager.LoadScene("Town");
    }
}
