using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Repository.Interface;
using GxPress.Request.App.Middle;
using GxPress.Request.Notice;
using GxPress.Request.NoticeFolder;
using GxPress.Result.App.Middle;
using GxPress.Service.Interface.Middle;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// 中间件
    /// </summary>
    [Route("api/app/middle")]
    [ApiController]
    [Authorize]
    public class MiddleController : ControllerBase
    {
        public readonly IMiddleService _middleService;
        public readonly ILoginContext _loginContext;
        private readonly IFolderUserRepository _folderUserRepository;

        public MiddleController(IMiddleService middleService, ILoginContext loginContext,
            IFolderUserRepository folderUserRepository)
        {
            _middleService = middleService;
            _loginContext = loginContext;
            _folderUserRepository = folderUserRepository;
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("sort")]
        public async Task<bool> MiddleSort(MiddleSortRequest request)
        {
            return await _middleService.MiddleSortAsync(request);
        }
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("add-folder")]
        public async Task<bool> Insert(NoticeFolderInRequest request)
        {
            request.UserId = _loginContext.AccountId;
            var middleInsertRequest = new MiddleInsertRequest
            {
                FolderName = request.FolderName,
                FolderType = request.TypeId,
                ParentId = request.ParentId,
                UserId = request.UserId,
                RoleId = request.RoleId,
                UserIds = request.UserIds,
                UserMiddles = request.UserMiddles
            };
            return await _middleService.InsertAsync(middleInsertRequest) > 0;

        }
        /// <summary>
        /// 设置置顶
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<bool> SetTop(int id)
        {
            return await _middleService.SetTopAsync(id);
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<bool> Delete(NoticeDeRequest request)
        {
            var userId = _loginContext.AccountId;
            request.UserId = userId;
            return await _middleService.DeleteAsync(request);
        }

        /// <summary>
        /// 恢复删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("recover-delete")]
        public async Task<bool> RecoverDelete(NoticeDeRequest request)
        {
            return await _middleService.RecoverDeleteAsync(request);
        }
        /// <summary>
        /// 恢复全部
        /// </summary>
        /// <param name="folderType">文件夹类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组</param>
        /// <returns></returns>
        [HttpPut("recover-all/{folderType}")]
        public async Task<bool> RecoverAllAsync(int folderType)
        {
            return await _middleService.RecoverAllAsync(folderType, _loginContext.AccountId);
        }
        /// <summary>
        /// 删除全部
        /// </summary>
        /// <param name="folderType">文件夹类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组</param>
        /// <returns></returns>
        [HttpDelete("delete-all/{folderType}")]
        public async Task<bool> DeleteAllAsync(int folderType)
        {
            return await _middleService.DeleteAllAsync(folderType, _loginContext.AccountId);
        }
        /// <summary>
        /// 移动
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("move")]
        public async Task<bool> SetMove(MiddleMoveRequest request)
        {
            return await _middleService.SetMove(request);
        }

        /// <summary>
        /// 修改文件夹名称
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<bool> UpdateFolderName(MiddleFolderNameRequest request)
        {
            return await _middleService.UpdateFolderNameAsync(request);
        }

        /// <summary>
        /// 查询文件夹
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("search-folder")]
        public async Task<List<MiddleSearchFolderResult>> SearchFolderAsync(MiddleSearchFolderRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _middleService.SearchFolderAsync(request);
        }

        /// <summary>
        /// 获取话题 笔记共享范围文件夹
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("search-note-folder")]
        public async Task<List<MiddleSearchFolderResult>> SearchNoteFolderAsync(MiddleSearchFolderRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _middleService.SearchNoteFolderAsync(request);
        }
        /// <summary>
        /// 修改文件夹名称或权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("update-role")]
        public async Task<bool> UpdateFolderRole(MiddleUpdateFolderRoleRequest request)
        {
            return await _middleService.UpdateFolderRoleAsync(request);
        }
        /// <summary>
        /// 根据文件夹ID查询文件夹信息
        /// </summary>
        /// <param name="middleId"></param>
        /// <returns></returns>
        [HttpGet("find-folder-info/{middleId}")]
        public async Task<MiddleFolderInfoResult> GetFolderUser(int middleId)
        {
            return await _middleService.GetFolderInfo(middleId);
        }

        /// <summary>
        /// 获取默认的共享文件夹范围
        /// </summary>
        /// <returns></returns>
        [HttpGet("enjoy-default/{folderType}")]
        public async Task<MiddleDefaultResult> GetEnjoyDefault(int folderType)
        {
            return await _middleService.GetEnjoyDefaultAsync(_loginContext.AccountId,folderType);
        }
    }
}