using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Data.Entity;
namespace BeachOrder.Hub
{
    public static class UserHandler
    {
        public static HashSet<string> ConnectedIds = new HashSet<string>();
    }
    public class Notification : Microsoft.AspNet.SignalR.Hub
    {

        private IHubContext ctx = null;
        public bool SendToClient(string senderName, string message, string connId, string garsonMsgId = "")
        {
            if (ctx == null)
                ctx = GlobalHost.ConnectionManager.GetHubContext<Notification>();

            var client = ctx.Clients.Client(connId);//.receiveNotification(senderName, message, garsonMsgId);
            if (client != null)
            {
                client.receiveNotification(senderName, message, garsonMsgId);
                return true;
            }
            else
            {
                return false;
            }

            //SendToAllClients(senderName, message, garsonMsgId);
        }
        [Authorize]
        public override Task OnConnected()
        {
            UserHandler.ConnectedIds.Add(Context.ConnectionId);
            var userName = Context.ConnectionId;
            return base.OnConnected();
        }

        public override Task OnDisconnected(bool stopCalled)
        {
            UserHandler.ConnectedIds.Remove(Context.ConnectionId);
            return base.OnDisconnected(stopCalled);
        }

        public int activeUserCount()
        {
            return UserHandler.ConnectedIds.Count;
        }
        public void SendToAllClients(string senderName = "", string message = "", string garsonMsgId = "")
        {
            if (ctx == null)
                ctx = GlobalHost.ConnectionManager.GetHubContext<Notification>();
            ctx.Clients.All.sendAll(senderName, message, garsonMsgId);
        }

        public void SendToClientByName(string who, string message, string senderName, string garsonMsgId = "")
        {
            if (ctx == null)
                ctx = GlobalHost.ConnectionManager.GetHubContext<Notification>();

        }

        public void RefreshCache()
        {
            if (ctx == null)
                ctx = GlobalHost.ConnectionManager.GetHubContext<Notification>();
            ctx.Clients.All.refreshCache();
        }
        public void RefreshCacheByUserId(string connId)
        {
            if (ctx == null)
                ctx = GlobalHost.ConnectionManager.GetHubContext<Notification>();

            var client = ctx.Clients.Client(connId);
            if (client != null)
                client.refreshCacheByUserId();
        }
    }
    //public class ConnectionFactory : IUserIdProvider
    //{
    //    public string GetUserId(IRequest request)
    //    {
    //        var cookieCollection = request.Cookies;
    //        Cookie c = null;
    //        if (cookieCollection.TryGetValue("srconnectionid",out c))
    //        {
    //            return request.Cookies["srconnectionid"].Value;
    //        }
    //        else
    //        {
    //            return Guid.NewGuid().ToString();
    //        }
    //    }
    //}
}