using EEMSClientCore;
using ServiceBase;

namespace EEMSUIClient.Services
{
    public class ClientService : IClientService, IClientBaseProvider
    {
        private readonly IClientCaller _clientCaller;
        private readonly ClientCaller _hubBase;

        private bool disposedValue;

        public ClientService()
        {
            _hubBase = new ClientCaller(this);
            _clientCaller = _hubBase;
        }

        public string? RecipePath { get; set; }
        public string? ConfigPath { get; set; }

        public bool Initialize(string ip, int port, string hub)
        {
            if (string.IsNullOrWhiteSpace(ip) || port < 0 || string.IsNullOrWhiteSpace(hub))
            {
                //log
                return false;
            }

            if (!_hubBase.Initialize())
            {
                //log
                return false;
            }

            if (!_hubBase.Open(ip, port, hub))
            {
                //log
                return false;
            }

            return true;
        }

        public Guid RegisterDevice(Models.DeviceInfo deviceInfo)
        {
            return _clientCaller.RegisterDevice(deviceInfo.Convert()).Result;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                    _hubBase?.Dispose();
                }

                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        // ~ClientService()
        // {
        //     // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
        //     Dispose(disposing: false);
        // }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }
}
