using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NPOI.Util;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using OpenAuth.WebApi.Model.CustomAttribute;

namespace OpenAuth.WebApi.Controllers
{

    /// 角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[ApiExplorerSettings(GroupName = "角色管理_Roles")]
    public class RolesController : ControllerBase
    {
        private readonly RoleApp _app;
        public RolesController(RoleApp app)
        {
            _app = app;
        }

        #region 查询

        #region 分页
        /// <summary>
        /// 获取所有的角色
        /// </summary>
        [HttpGet]
        [ApiExplorerSettings(IgnoreApi = true)]
        public async Task<Response<PageInfo<List<SysRole>>>> LoadAllPage([FromQuery] QueryRoleListReq request)
        {
            return await _app.LoadAllPage(request);
        }

        /// <summary>
        /// x用户可访问角色分页
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [AllData("Roles", "LoadAllPage")]
        public async Task<Response<PageInfo<List<SysRole>>>> LoadPage([FromQuery] QueryRoleListReq request)
        {
            return await _app.LoadPage(request);
        }
        #endregion

        #region 角色实体
        [HttpGet]
        public Response<SysRole> Get(long id)
        {
            var result = new Response<SysRole>();
            try
            {
                result.Result = _app.Get(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 用户关联的角色
        [HttpGet]
        public async Task<Response<List<long>>> UserRoles(long userId)
        {
            var result = new Response<List<long>>();
            try
            {
                result.Result = await _app.UserRoles(userId);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion
        #endregion

        #region 增删改

        #region 添加
        /// <summary>
        /// 添加角色
        /// </summary>
        [HttpPost]
        public async Task<Response<bool>> Add(RoleView obj)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.Add(obj);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 修改
        [HttpPost]
        public async Task<Response<bool>> Update(RoleView obj)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.Update(obj);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 删除
        [HttpPost]
        public async Task<Response<bool>> Delete([FromBody] long[] ids)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.Delete(ids);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 为角色分配菜单和按钮
        /// <summary>
        /// 为角色分配菜单和按钮
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AssignModule(ModuleElementWithRole model)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.AssignModule(model);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #endregion
    }
}