using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SignalRSamples.Hubs
{
    public class HubTChat : Hub<IChatClient>
    {
        public override async Task OnConnectedAsync()
        {
            await Clients.All.Receive($"{Context.ConnectionId} joined");
        }

        public override async Task OnDisconnectedAsync(Exception ex)
        {
            await Clients.Others.Receive($"{Context.ConnectionId} left");
        }

        public Task Send(string message)
        {
            return Clients.All.Receive($"{Context.ConnectionId}: {message}");
        }

        public Task Login(long userId)
        {
            return Clients.Caller.LoginSuccess(userId);
        }

        public Task SendToOthers(string message)
        {
            return Clients.Others.Receive($"{Context.ConnectionId}: {message}");
        }

        public Task SendToGroup(string groupName, string message)
        {
            return Clients.Group(groupName).Receive($"{Context.ConnectionId}@{groupName}: {message}");
        }

        public Task SendToOthersInGroup(string groupName, string message)
        {
            return Clients.OthersInGroup(groupName).Receive($"{Context.ConnectionId}@{groupName}: {message}");
        }

        public async Task JoinGroup(string groupName)
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, groupName);

            await Clients.Group(groupName).Receive($"{Context.ConnectionId} joined {groupName}");
        }

        public async Task LeaveGroup(string groupName)
        {
            await Clients.Group(groupName).Receive($"{Context.ConnectionId} left {groupName}");

            await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
        }

        public Task Echo(string message)
        {
            return Clients.Caller.Receive($"{Context.ConnectionId}: {message}");
        }
    }

    public interface IChatClient
    {
        Task Receive(string message);
        Task LoginSuccess(long userId);
    }
}
