using UnityEngine;
using TMPro;
using FishNet;
using IStalk.Network;

namespace IStalk.UI
{
    public class NetworkStatusUI : MonoBehaviour
    {
        [SerializeField] private TextMeshProUGUI _pingText;
        [SerializeField] private TextMeshProUGUI _connectionText;
        [SerializeField] private GameObject _disconnectedWarning;

        private float _updateInterval = 0.5f;
        private float _timer;

        private void Update()
        {
            _timer += Time.deltaTime;
            if (_timer < _updateInterval) return;
            _timer = 0;

            bool connected = RelayClient.Instance != null && RelayClient.Instance.IsConnected;

            if (_disconnectedWarning != null)
                _disconnectedWarning.SetActive(!connected);

            if (_connectionText != null)
                _connectionText.text = connected ? "Connected" : "Disconnected";

            if (_pingText != null && InstanceFinder.TimeManager != null)
            {
                long rtt = (long)(InstanceFinder.TimeManager.RoundTripTime * 1000);
                _pingText.text = $"{rtt}ms";
            }
        }
    }
}
