using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Interface;
using OpenAuth.App.ModuleManager;
using OpenAuth.App.ModuleManager.Response;
using OpenAuth.Repository.Core;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 模块及菜单管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[ApiExplorerSettings(GroupName = "模块菜单_Modules")]
    public class ModulesController : ControllerBase
    {
        private ModuleManagerApp _app;
        private IAuth _authUtil;
        public ModulesController(IAuth authUtil, ModuleManagerApp app)
        {
            _app = app;
            _authUtil = authUtil;
        }

        #region 查询

        #region 根据角色 Id 查询相关的菜单及按钮 Id 集合
        /// <summary>
        /// 根据角色 Id 查询相关的菜单及按钮 Id 集合
        /// </summary>
        /// <param name="roleId">角色 Id</param>
        /// <returns>菜单Id集合、按钮Id集合</returns>
        [HttpGet]
        public async Task<Response<ModulesWithElements>> LoadForRole(long roleId)
        {
            var result = new Response<ModulesWithElements>();
            try
            {
                result = await _app.LoadForRole(roleId);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 菜单树【全部数据】
        /// <summary>
        /// 菜单树【全部数据】
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<List<TreeItemString>> ModulesTree(string name)
        {
            return _app.ModulesTree(name);
        }
        #endregion

        #region 根据 ModuleId 获取 Elements
        /// <summary>
        /// 根据 ModuleId 获取 Elements
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<SysModuleElement>>>> ElementsByModule([FromQuery] QueryElementListReq req)
        {
            return await _app.ElementsByModule(req);
        }
        #endregion

        #region 根据 Id 获取 Module
        /// <summary>
        /// 根据 Id 获取 Module
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<SysModule>> ModuleById(string id)
        {
            var result = new Response<SysModule>();
            try
            {
                result = await _app.ModuleById(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        [HttpGet]
        public async Task<Response<List<SysModule>>> AllModule()
        {
            var result = new Response<List<SysModule>>();
            try
            {
                result = await _app.AllModule();
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        #endregion

        #endregion

        #region 增删改

        #region 新增菜单
        /// <summary>
        /// 新增菜单
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> Add(SysModule obj)
        {
            var result = new Response<bool>();
            try
            {
                result = _app.Add(obj);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 修改菜单
        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> Update(SysModule obj)
        {
            var result = new Response<bool>();
            try
            {
                result = _app.Update(obj);

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

            return result;
        }
        #endregion

        #region 删除菜单
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> Delete([FromBody] string[] 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>
        [HttpPost]
        public Response<bool> AddMenu(SysModuleElement obj)
        {
            var result = new Response<bool>();
            try
            {
                result = _app.AddMenu(obj);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 修改按钮
        /// <summary>
        /// 修改按钮
        /// </summary>
        [HttpPost]
        public Response<bool> UpdateMenu(SysModuleElement obj)
        {
            var result = new Response<bool>();
            try
            {
                result = _app.UpdateMenu(obj);

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

            return result;
        }
        #endregion

        #region 删除按钮
        /// <summary>
        /// 删除按钮
        /// </summary>
        [HttpPost]
        public async Task<Response<bool>> DeleteMenu([FromBody] string[] ids)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.DelMenu(ids);

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

            return result;
        }
        #endregion

        #endregion
    }
}