using System;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using static UnityEngine.Rendering.DebugUI;

public class GameOver : MonoBehaviour
{
    [Header("UI and HUB")]

    [SerializeField] GameObject deathPanel;
    private float duration = 4f;
    [SerializeField] public float timer;
    [SerializeField] TMP_Text deathTime;
    [SerializeField] TMP_Text infoText;
    [SerializeField] TMP_Text costText;
    [SerializeField] int cost;
    //[SerializeField] public bool recentDeath;    //discaraded

    [SerializeField] public bool timerOn;

    public float etwas;
    public float zeit;

    [Header("ScriptPile")]

    [SerializeField] GameObject scriptPile;


    InventoryScript inventory;
    ChangeHealth healthScript;
    HandleEnergy battery;

    private void Awake()
    {
        inventory = scriptPile.GetComponent<InventoryScript>();
        healthScript = scriptPile.GetComponent<ChangeHealth>();
        battery = scriptPile.GetComponent<HandleEnergy>();
    }

    void Start()
    {
        timer = 10f;
        inventory.Gems = 599;
        deathPanel.SetActive(false);
    }

    void Update()
    {

        CheckDeathTime();
        //if (Time.timeScale == 1)
        //{
        //    Debug.Log("The game is running.");
        //}
        //else if(Time.timeScale == 0)
        //{
        //    Debug.Log("The game should be paused now.");
        //}
    }

    public void ShowDeathScreen()
    {
        //Time.timeScale = 0;
        timerOn = true;
        timer = 10f;
        deathPanel.SetActive(true);
        SpeakEnemy();
        costText.text = cost.ToString();      //ZUM DEBUGGEN
        CheckDeathTime();

        //OPTION INS MAINMENUE ZURÜCKZUKEHREN
    }

    public void GiveNewChance()
    {
        if (inventory.Gems >= cost)
        {
            NewChance();
            inventory.Gems -= cost;
            Debug.Log(inventory.Gems);
            timerOn = false;
            timer = 10f;
        }

        else      //EINBAUEN, DASS MAN DORT GEMS KAUFEN KANN
        {
            infoText.text = "You don´t have enough gems to do that.";
        }
    }

    public void StopBothering()
    {
        Time.timeScale = 1;             //UNSICHER OB LADEN VON SCENE DEN WERT AUTO. AUF 1 SETZT
        Debug.Log("Jetzt würdest du wieder in die MainMenueScene geschmissen werden");
        //SceneManager.LoadSceneAsync(0);           //MUSS AM ENDE WIEDER EINGEBAUT WERDEN
    }


    public void NewChance()
    {
        inventory.Health = healthScript.standardHealth;

        if (battery.standardEnergy >= (inventory.Energy + battery.addEnergyValue))
        {
            inventory.Energy += battery.addEnergyValue;
        }

        deathPanel.SetActive(false);
        //Time.timeScale = 1;

        //Logik: bei Retry, soll der Player nochmal neue Energie bekommen
        
    }

    public void CheckDeathTime()
    {
        if(timerOn == true)
        {
            timer += zeit - Time.deltaTime;

            deathTime.text = Convert.ToInt32(timer).ToString();

            if (timer <= 0.0f)
            {
                
                StopBothering();

                timerOn = false;
                timer = 10f;
            }
        }
    }

    public void CalculateCost()
    {
        cost = healthScript.deathCounter * 100;
    }


    public bool fieKill;
    public bool silvKill;
    public bool haiKill;
    public void SpeakEnemy()
    {


        if (fieKill)
        {
            infoText.text = "HAAHAHAHAHAAHAHAHAHAHAHAHAHA";
        }

        if (silvKill)
        {
            infoText.text = "Oh no, I accidently killed someone again. I'm sowy. I just wanted to hug you :(";
        }
        if (haiKill)
        {
            infoText.text = "You sound great...";
        }
        else
        {
            infoText.text = "Try again?";
        }
    }
}
