using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Repository.Interface;
using GxPress.Request.App.Group;
using GxPress.Request.App.GroupUser;
using GxPress.Request.App.Topic;
using GxPress.Result.App.Group;
using GxPress.Result.App.GroupUser;
using GxPress.Result.App.Topic;
using GxPress.Service.Interface.Group;
using GxPress.Service.Interface.Topic;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// 小组
    /// </summary>
    [Route("api/app/group")]
    [ApiController]
    [Authorize]
    public class GroupController : ControllerBase
    {
        private readonly ILogger<GroupController> _logger;
        private readonly IGroupRepository _groupRepository;
        private readonly IGroupUserRepository _groupUserRepository;
        private readonly IGroupFolderRepository _groupFolderRepository;
        private readonly IGroupService _groupService;
        private readonly ILoginContext _loginContext;
        private readonly ITopicRepository _topicRepository;
        private readonly ITopicService _topicService;

        public GroupController(ILogger<GroupController> logger, IGroupRepository groupRepository,
            IGroupFolderRepository groupFolderRepository, IGroupUserRepository groupUserRepository,
            ILoginContext loginContext, IGroupService groupService, ITopicRepository topicRepository, ITopicService topicService)
        {
            _logger = logger;
            _groupFolderRepository = groupFolderRepository;
            _groupRepository = groupRepository;
            _groupUserRepository = groupUserRepository;
            _loginContext = loginContext;
            _groupService = groupService;
            _topicRepository = topicRepository;
            _topicService = topicService;
        }

        /// <summary>
        /// 新建小组
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("insert")]
        public async Task<GroupDetailResult> Insert(GroupInRequest request)
        {
            request.UserId = _loginContext.AccountId;
            var result = await _groupService.InsertGroup(request);
            //插入小组用户
            if (result.Id > 0 && request.UserIds.Count > 0)
            {
                var groupUserInRequest = new GroupUserInRequest();
                groupUserInRequest.UserId = request.UserId;
                groupUserInRequest.UserIds = request.UserIds;
                groupUserInRequest.SourceType = 2;
                groupUserInRequest.GroupId = result.Id;
                await _groupService.InsertsAsync(groupUserInRequest);
            }
            return result;
        }

        /// <summary>
        ///  添加小组成员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("insert-user")]
        public async Task<bool> AddGroupUser(GroupUserInRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _groupService.InsertsAsync(request);
        }

        /// <summary>
        /// 删除小组成员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("delete-user")]
        public async Task<bool> DeleteGroupUser(GroupUserDeRequest request)
        {
            return await _groupService.DeleteAsync(request);
        }

        /// <summary>
        /// 退出小组
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [HttpDelete("{groupId}")]
        public async Task<bool> DeleteAsync(int groupId)
        {
            return await _groupService.DeleteAsync(_loginContext.AccountId, groupId);
        }

        /// <summary>
        /// 根据文件夹ID查询小组列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("find")]
        public async Task<PagedList<GroupUserFindResult>> FindGroupByGroupFolderId(GroupSearchRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _groupService.FindGroupByGroupFolderIdAsync(request);
        }

        /// <summary>
        /// 获取小组详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("detail")]
        public async Task<GroupDetailResult> GroupDetailAsync(GroupDetailRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _groupService.GroupDetailAsync(request);
        }

        /// <summary>
        /// 修改小组
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<bool> Update(GroupUpRequest request)
        {
            return await _groupRepository.UpdateAsync(request);
        }

        /// <summary>
        ///设置权限
        /// 1 超级管理员 2 管理员 3普通用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update-user")]
        public async Task<bool> UpdateGroupUser(GroupUserUpRequest request)
        {
            return await _groupUserRepository.UpdateAsync(request);
        }

        /// <summary>
        /// 根据GroupId获取话题列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("search")]
        public async Task<PagedList<TopicListPageResult>> FindTopicByGroupId(TopicDetailListRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _topicService.FindTopicByGroupIdAsync(request);
        }

        /// <summary>
        /// 加入是否管理员同意
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [HttpPut("set-admin/{groupId}")]
        public async Task<bool> SetGroupIsAdmin(int groupId)
        {
            return await _groupRepository.SetGroupIsAdminAsync(groupId);
        }
        /// <summary>
        /// 是否禁言
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [HttpPut("set-words/{groupId}")]
        public async Task<bool> SetGroupIsWords(int groupId)
        {
            return await _groupRepository.SetGroupIsWordsAsync(groupId);
        }

        /// <summary>
        /// 扫描二维码获取小组详情
        /// </summary>
        /// <param name="guId"></param>
        /// <returns></returns>
        [HttpGet("{guId}")]
        public async Task<GroupDetailResult> GroupDetailByGuId(string guId)
        {
            var userId = _loginContext.AccountId;
            return await _groupService.GroupDetailByGuIdAsync(guId, userId);
        }

        /// <summary>
        /// 设置用户是否免打扰
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [HttpPut("set-disturb/{groupId}")]
        public async Task<bool> SetIsUDisturbAsync(int groupId)
        {
            var userId = _loginContext.AccountId;
            return await _groupUserRepository.SetIsUDisturbAsync(groupId, userId);
        }

        /// <summary>
        /// 搜索小组成员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("search-group-user")]
        public async Task<IEnumerable<GroupUserListResult>> SearchGroupUserAsync(TopicDetailListRequest request)
        {
            return await _groupRepository.SearchGroupUserAsync(request);
        }
    }
}