using System;
using TMPro;
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.BossRoom.Utils;
using Unity.Netcode;
using Unity.Services.Lobbies.Models;
using UnityEngine;

namespace Unity.BossRoom.Gameplay.UI
{
    /// <summary>
    /// Class containing references to UI children that we can display. Both are disabled by default on prefab.
    /// </summary>
    public class UIStateDisplay : MonoBehaviour
    {
        public SwapConfirmationPanel swapConfirmationPanel;
        public string playerName;
        [SerializeField]
        UIName m_UIName;

        [SerializeField]
        UIHealth m_UIHealth;
        public FloatingScore floatingScorePrefab;
        PartyHUD m_PartyHUD;
        private void Awake()
        {
            m_PartyHUD=FindObjectOfType<PartyHUD>();
        }
        private void OnEnable()
        {
            ScoreManager.OnScoreUpdated += OnScoreChanged;
        }

        private void OnDisable()
        {
            ScoreManager.OnScoreUpdated -= OnScoreChanged;
        }
        public void DisplayName(NetworkVariable<FixedPlayerName> networkedName)
        {
            m_UIName.gameObject.SetActive(true);
            m_UIName.Initialize(networkedName);
            playerName=networkedName.Value.ToString();
            //var servercharacterName = GetComponentInParent<ServerCharacter>();
            //servercharacterName.name = playerName;
            swapConfirmationPanel.gameObject.name += networkedName.Value;
        }

        public void DisplayHealth(NetworkVariable<int> networkedHealth, int maxValue)
        {
           // m_UIHealth.gameObject.SetActive(true);
           // m_UIHealth.Initialize(networkedHealth, maxValue);
        }

        public void HideHealth()
        {
            m_UIHealth.gameObject.SetActive(false);
        }
        [SerializeField] private Transform scoreEffectSpawnPoint; // position above the head for the effect
        private int previousScore = 0;

        

        
        // Called when the NetworkVariable changes.
        private void OnScoreChanged(ulong playerID, int newScore,Color color=default)
        {
            // Trigger a floating effect on all clients.
            // Using a ClientRpc so that every client sees the same effect.
           // if (NetworkManager.Singleton.IsServer)
            {
                RpcPlayScoreAnimationClientRpc(newScore, playerID,color);
            }
        }
        // This ClientRpc will be executed on all clients.
        [ClientRpc]
        private void RpcPlayScoreAnimationClientRpc(int change, ulong playerID,Color color=default)
        {
            if (NetworkManager.Singleton.LocalClientId != playerID)
                return; // Only the local player should see their own floating score

            FloatingScore effect = Instantiate(floatingScorePrefab, m_PartyHUD.ScoreSpawnPos);
            effect.transform.localPosition = Vector3.zero;
            effect.transform.localEulerAngles = Vector3.zero;
            effect.Initialize(change,color);
        }
    }
}