using AlarmInfoServerSim.Services;
using CommunityToolkit.Mvvm.ComponentModel;

namespace AlarmInfoServerSim.ViewModels
{
    public partial class StatusBarViewModel : ObservableObject
    {
        private readonly ISharedConfig _sharedConfig;
        private readonly IInfoSendingService _sendInfoService;

        [ObservableProperty]
        private string _status;

        [ObservableProperty]
        private string _ipAddress;

        [ObservableProperty]
        private ushort _port;

        public StatusBarViewModel(ISharedConfig sharedConfig, IInfoSendingService sendInfoService)
        {
            _sharedConfig = sharedConfig;
            _status = "Disconnected";
            _ipAddress = "Unknown";
            _port = _sharedConfig.BasicInfo?.RTServerPort ?? 0;

            _sendInfoService = sendInfoService;
            _sendInfoService.ConnectionStatusChanged += OnConnectionStatusChanged;
            _sendInfoService.TcpConnectionChanged += OnTcpConnectionChanged;
        }

        private void OnTcpConnectionChanged(object? sender, UniversalNetFrame451.IO.TcpConnection e)
        {
            if(Status== "Connected")
            {
                IpAddress=e.RemoteEndPoint.Address.ToString();
            }
        }

        private void OnConnectionStatusChanged(object? sender, bool e)
        {
            Status = e ? "Connected" : "Disconnected";
        }
    }
}
