using System.Collections;
using GameNetcodeStuff;
using HarmonyLib;
using TMPro;
using UnityEngine;

namespace MuzikaGromche
{
    static class DeathScreenGameOverTextManager
    {
        private const string GameOverTextVanilla = "[LIFE SUPPORT: OFFLINE]";

        public const string GameOverTextModdedDefault = "[ MUZIKA: GROMCHE ]";

        public static void Clear()
        {
            SetTextImpl(GameOverTextVanilla);
        }

        public static void SetText(string? text)
        {
            SetTextImpl(text ?? GameOverTextModdedDefault);
        }

        public static void SetTextAndClear(string? text)
        {
            HUDManager.Instance.StartCoroutine(SetTextAndClearImpl(text));
        }

        public static IEnumerator SetTextAndClearImpl(string? text)
        {
            SetText(text);
            // Game Over animation duration is about 4.25 seconds
            yield return new WaitForSeconds(5f);
            Clear();
        }

        private static void SetTextImpl(string text)
        {
            GameObject textGameObject = GameObject.Find("Systems/UI/Canvas/DeathScreen/GameOverText");
            if (textGameObject == null)
            {
                return;
            }
            TextMeshProUGUI tmp = textGameObject.GetComponent<TextMeshProUGUI>();
            if (tmp == null)
            {
                return;
            }
            var transform = textGameObject.GetComponent<RectTransform>();
            if (transform == null)
            {
                return;
            }
            // Default transform width is 645.8 which only fit the default message and no extra characters
            Vector2 size = transform.sizeDelta;
            if (Mathf.Approximately(size.x, 645.8f))
            {
                size.x += 100f;
                transform.sizeDelta = size;
            }
            tmp.text = text;
        }
    }

    [HarmonyPatch(typeof(RoundManager))]
    static class DeathScreenGameOverTextResetPatch
    {
        [HarmonyPatch(nameof(RoundManager.OnDestroy))]
        [HarmonyPrefix]
        static void OnDestroy(RoundManager __instance)
        {
            var _ = __instance;
            DeathScreenGameOverTextManager.Clear();
        }
    }

    [HarmonyPatch(typeof(PlayerControllerB))]
    static class PlayerControllerBKillPlayerPatch
    {
        // Use prefix to test distance from listener before the listener is reassigned
        [HarmonyPatch(nameof(PlayerControllerB.KillPlayer))]
        [HarmonyPrefix]
        static void KillPlayerPrefix(PlayerControllerB __instance)
        {
            if (__instance.IsOwner && !__instance.isPlayerDead && __instance.AllowPlayerDeath())
            {
                // KILL LOCAL PLAYER
                var list = Object.FindObjectsByType<EnemyAI>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
                foreach (var jester in list)
                {
                    if (jester.TryGetComponent<MuzikaGromcheJesterNetworkBehaviour>(out var behaviour) &&
                        behaviour.IsPlaying && Plugin.LocalPlayerCanHearMusic(jester))
                    {
                        behaviour.OverrideDeathScreenGameOverText();
                        break;
                    }
                }
            }
        }
    }
}
