using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using OASystem.API.OAMethodLib;
using OASystem.API.OAMethodLib.Hub.HubClients;
using OASystem.API.OAMethodLib.Hub.Hubs;
using OASystem.API.OAMethodLib.SignalR.Hubs;

namespace OASystem.API.Controllers
{
    /// <summary>
    /// 消息客户端
    /// </summary>
    [Route("api/[controller]/[action]")]
    public class ClientHubController : ControllerBase
    {
        private readonly ILogger<ClientHubController> _logger;
        private readonly IHubContext<ChatHub, IChatClient> _hubContext;

        public ClientHubController(ILogger<ClientHubController> logger, IHubContext<ChatHub, IChatClient> hubContext )
        {
            _logger = logger;
            _hubContext = hubContext;
        }

        /// <summary>
        /// 获取在线用户
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetOnlineUsers", Name = "GetOnlineUsers")]
        public List<UserModel> GetOnlineUsers()
        {
            return UserStore.OnlineUser;
        }


        /// <summary>
        /// 发送消息给客户端
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="date"></param>
        /// <param name="hubContext"></param>
        /// <returns></returns>
        [HttpPost("SendAllUserMessage", Name = "SendAllUserMessage")]
        public async Task<IActionResult> SendAllUserMessage( string msg)
        {
            await _hubContext.Clients.All.ReceiveMessage(msg);
            return Ok("Send Successful!");
        }

        /// <summary>
        /// 发送指定的消息给指定的客户端
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="date"></param>
        /// <param name="hubContext"></param>
        /// <returns></returns>
        [HttpPost("SendCustomUserMessage", Name = "SendCustomUserMessage")]
        public async Task<IActionResult> SendCustomUserMessage(List<int> userIds,string title,string content )
        {
            string connId = string.Empty;

            //UserModel user = UserStore.OnlineUser.Where(it => it.UserId == userId).FirstOrDefault();
            //if (user != null)
            //{
            //    connId = user.ConnectionId;
            //}
            //else
            //{
            //    return Ok("Send Failed! User Not Online!");
            //}

            //await _hubContext.Clients.Client(connId).ReceiveMessage(date);

            GeneralMethod.MessageIssueAndNotification(MessageTypeEnum.Announcement,title, content, userIds);

            return Ok("Send Successful!");
        }

        /// <summary>
        /// 消息 发布And 通知
        /// </summary>
        /// <param name="msgTypeEnum"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        [HttpPost("MessageIssuserAndNotification", Name = "MessageIssuserAndNotification")]
        public async Task<IActionResult> MessageIssuserAndNotification(MessageTypeEnum msgTypeEnum, string title, string content, List<int> userIds)
        {
            var status = await GeneralMethod.MessageIssueAndNotification(msgTypeEnum,title, content, userIds);
            if (status)
            {
                return Ok("Operation Successful!");
            }
            else
            {
                return Ok("Operation Fail!");
            }
            
        }

    }
}
