using Capnp.Rpc;
using FabAccessAPI.Exceptions;
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>();
        }
        #endregion

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

        public Session? Session { get; private set; } = 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 Auth(string mech, Dictionary<string, object> kvs, CancellationToken cancellationToken_ = default)
        {
            IReadOnlyList<string>? mechs = await _BootstrapCap.Mechanisms();
            if (!mechs.Contains(mech))
            {
                throw new UnsupportedMechanismException();
            }

            if (_Auth == null)
            {
                IAuthentication? authCap = await _BootstrapCap.CreateSession(mech, cancellationToken_).ConfigureAwait(false);
                _Auth = new Auth(authCap);
            }

            Session = await _Auth.Authenticate(mech, kvs);
        }
        #endregion
    }
}