using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HubCallerContextDict = System.Collections.Concurrent.ConcurrentDictionary<string, Microsoft.AspNetCore.SignalR.HubCallerContext>;
namespace WK_KLI_LAPS_COMMONTOOL_Service.Hub
{
    public class Userhub
    {
        public string Userid { get; set; }
        public HashSet<string> ConnectionIds { get; set; }
    }
    [EnableCors("CorsPolicy")]

    [AllowAnonymous]
    public class AuthHub : Microsoft.AspNetCore.SignalR.Hub
    {
        public static HubCallerContextDict _HubDict = new HubCallerContextDict();

        [Authorize]
        public async Task SendMessage()
        {
            await Clients.All.SendAsync("Logoutclient");

        }

        public override Task OnConnectedAsync()
        {

            //if (this.Context.User.Identity.IsAuthenticated)
            // {
            //   var _userId = this.Context.User.Identity.Name;

            var _userId = Context.GetHttpContext().Request.Query["userid"];
            if (_HubDict.TryGetValue(_userId, out HubCallerContext _clientHub))
            {
                if (
                    !object.Equals(_clientHub.ConnectionId, this.Context.ConnectionId)
                    )
                {
                    _HubDict.TryRemove(_userId, out _clientHub);
                    _clientHub.Abort();
                }
            }
            _HubDict.TryAdd(_userId, this.Context);
            // }

            return base.OnConnectedAsync();
        }

        public override Task OnDisconnectedAsync(Exception exception)
        {
            if (this.Context.User.Identity.IsAuthenticated)
            {
                var _userId = this.Context.User.Identity.Name;
                if (_HubDict.TryGetValue(_userId, out HubCallerContext _clientHub))
                {
                    if (object.Equals(this.Context.ConnectionId, _clientHub.ConnectionId))
                    {
                        _HubDict.TryRemove(_userId, out _clientHub);
                        _clientHub.Abort();
                    }
                }
            }
            return base.OnDisconnectedAsync(exception);
        }

        public string GetUser(string userid)
        {

            string status = "no";
            if (_HubDict.TryGetValue(userid, out HubCallerContext _clientHub))
            {
                status = "yes";
            }
            return status;
        }

        public string GetConnectionId(string userid)
        {
            string clientid = "";
            if (_HubDict.TryGetValue(userid, out HubCallerContext _clientHub))
            {
                clientid = _clientHub.ConnectionId;
            }
            return clientid;
        }


        public void logout(string to)
        {

            _HubDict.TryRemove(to, out HubCallerContext _clientHub);

        }
    }
}
