using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;

public class GameManager : MonoBehaviour
{
    [SerializeField] private Player _player;
    
    private float _lastTimeof2Seconds;
    [SerializeField] private TMPro.TextMeshProUGUI _timer;
    
    [SerializeField] private TMPro.TextMeshProUGUI _gameOver;
    
    [SerializeField] private GameObject _potionPrefab; 
    private Vector2 _potionGroundSpawnRange = new Vector2(-48f, 48f);
    private int _healthDecrease = -2;

    [SerializeField] private GameObject _keyPrefab;
    private HiddenObject _keyInstanceScript;
    private GameObject _keyInstance;
    private bool isKeyCollected = false;

    [SerializeField] private GameObject _chestClosedPrefab;
    [SerializeField] private GameObject _chestOpenPrefab;
    private GameObject _chestInstance;

    private void Start()
    {
        Assert.IsNotNull(_player);
        _lastTimeof2Seconds = Time.time;
        _gameOver.enabled = false;
        
        SpawnPotion();    
        
        Assert.IsNotNull(_keyPrefab);
        _keyInstance = Instantiate(_keyPrefab,
            new Vector3(
                Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y),
                0.5f,
                Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y)
                ),
            Quaternion.identity);
        
        _keyInstanceScript = _keyInstance.transform.GetChild(0).gameObject.GetComponent<HiddenObject>();
        Assert.IsNotNull(_keyInstance);
        
        Assert.IsNotNull(_chestClosedPrefab);
        _chestInstance = Instantiate(_chestClosedPrefab,
            new Vector3(
                Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y),
                0.5f,
                Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y)
            ),
            Quaternion.LookRotation(Vector3.up));
    }

    public void CollectKey()
    {
        isKeyCollected = true;
        Debug.Log("Collected Key");
    }

    public void OpenChest()
    {
        if (!isKeyCollected)
        {
            Debug.LogWarning("Key not collected");
            return;
        }

        Instantiate(
            _chestOpenPrefab,
            new Vector3(
                _chestInstance.transform.position.x,
                0.5f,
                _chestInstance.transform.position.z
            ),
            Quaternion.LookRotation(Vector3.up)
        );
        GameOver(true);
    }
    public void SpawnPotion()
    {
        Assert.IsNotNull(_potionPrefab);
        Instantiate(_potionPrefab, 
            new Vector3(
                Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y),
                0.5f,
                Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y)),
            Quaternion.identity);
    }

    private void Update()
    {
        _timer.SetText(((int)(Time.time)).ToString());
        if (Time.time - _lastTimeof2Seconds > 2)
        {
            Debug.Log("Decreasing health");
            _lastTimeof2Seconds = Time.time;
            _player.AddHealth(_healthDecrease);
        }

        if (_player.GetHealth() <= 0)
        {
            GameOver();
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            _keyInstanceScript.ShowObjectForSeconds(3);
        }
    }

    private void GameOver(bool isWon = false)
    {
        _player.SetIsMovementEnabled(false);
        _healthDecrease = 0;
        if (isWon)
        {
            _gameOver.SetText("You won!");
        }
        else
        {
            _gameOver.SetText("You lost!");
        }
        _gameOver.enabled = true; 
    }
}
