
using UnityEngine;
using UnityEngine.UI;
using TMPro;

namespace NeonTea.Quakeball.Interface {
    public class DeadScreen : MonoBehaviour {

        public Image Crosshair;
        public GameObject Root;
        public TMP_Text KilledText;
        public TMP_Text RespawnText;

        public bool Open = false;

        public float LastUpdate;
        public int seconds;

        public void Start() {
            Terminal.Singleton.RegisterCommand("dead", args => {
                if (args.Length == 1) {
                    StartCountdown(args[0]);
                    return true;
                }
                Terminal.Singleton.Println("Need 1 argument for killer's name!");
                return false;
            }, "dead <name> - Starts the DeadScreen with given name as the killer's name.");
        }

        public void Update() {
            Root.gameObject.SetActive(Open);
            if (Crosshair != null) {
                Crosshair.enabled = !Open;
            }
            if (seconds > 0 && Time.time - LastUpdate > 1) {
                LastUpdate = Time.time;
                seconds--;
                string secondsText = (seconds > 0) ? seconds.ToString() : "";
                RespawnText.text = $"Respawn in {secondsText}..";
            }
        }

        public void StartCountdown(string killer) {
            Open = true;
            KilledText.text = $"Killed by {killer}";
            LastUpdate = Time.time;
            seconds = 3;
            RespawnText.text = $"Respawn in {seconds}..";
        }
    }
}