using Capnp.Rpc;
using FabAccessAPI.Schema;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace FabAccessAPI
{
    public class Connection
    {
        #region Private Fields
        private readonly IBootstrap? _BootstrapCap = null;
        private Auth? _Auth = null;
        #endregion

        #region Constructors
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rpcClient">Should be an already configured and connected TcpRpcClient</param>
        public Connection(TcpRpcClient rpcClient)
        {
            RpcClient = rpcClient;
            _BootstrapCap = RpcClient.GetMain<IBootstrap>();
            //_Log.Debug($"Done bootstraping API connection.");
        }
        #endregion

        #region Fields
        public TcpRpcClient? RpcClient { get; } = null;
        #endregion

        #region Methods
        /// <summary>
        /// Authenticate this connection.
        /// Calling this more then once is UB
        /// </summary>
        /// <param name="mech">The desired authentication mechanism</param>
        /// <param name="kvs">Key-Value data specific to the mechanism</param>
        /// <returns></returns>
        public async Task<bool> Auth(string mech, Dictionary<string, object> kvs, CancellationToken cancellationToken_ = default)
        {
            if(_Auth == null)
            {
                IAuthenticationSystem? authCap = await _BootstrapCap.AuthenticationSystem(cancellationToken_).ConfigureAwait(false);
                _Auth = new Auth(authCap);
            }

            IReadOnlyList<string>? mechs = await _Auth.GetMechanisms();
            if (!mechs.Contains(mech))
            {
                throw new UnsupportedMechanismException();
            }

            return await _Auth.Authenticate(mech, kvs);
        }

        /// <summary>
        /// Get a wrapped capability to interact with machines
        /// </summary>
        /// <returns>A wrapped capability to interact with machines</returns>
        public async Task<IMachineSystem> AccessMachineSystem()
        {
            return await _BootstrapCap.MachineSystem();
        }

        /// <summary>
        /// Get a wrapped capability to interact with users
        /// </summary>
        /// <returns>A wrapped capability to interact with users</returns>
        public async Task<IUserSystem> AccessUserSystem()
        {
            return await _BootstrapCap.UserSystem();
        }

        /// <summary>
        /// Get a wrapped capability to interact with permissions
        /// </summary>
        /// <returns>A wrapped capability to interact with permissions</returns>
        public async Task<IPermissionSystem> AccessPermissionSystem()
        {
            return await _BootstrapCap.PermissionSystem();
        }
        #endregion
    }
}