using CrazyStudio.Core.Common.Eitities.Page;
using Microsoft.AspNetCore.Mvc;
using XPrintServer.Business.Dto;
using XPrintServer.Business.Services;
using XPrintServer.DataModel.Models;

namespace XPrintServer.Admin.Api.Controllers
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {

        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly RoleService _roleService;
        private readonly UserMenuInRoleService _userMenuInRoleService;
        private readonly ActionLogService _actionLogService;

        public RoleController(IHttpContextAccessor httpContextAccessor, RoleService roleService, UserMenuInRoleService userMenuInRoleService, ActionLogService actionLogService)
        {
            _httpContextAccessor = httpContextAccessor;
            _roleService = roleService;
            _userMenuInRoleService = userMenuInRoleService;
            _actionLogService = actionLogService;
        }

        [HttpPost]
        public async Task<ActionResult> AddOrUpdateRole([FromBody] RoleInfo roleInfo)
        {
            try
            {
                //开始事务
                await _roleService.Context.Ado.BeginTranAsync();

                await _roleService.AddOrUpdateRole(roleInfo.Role);

                await _userMenuInRoleService.AddOrUpdateUserMenuInRoles(roleInfo.Role.Id, roleInfo.MenuIds);
                //提交事务
                await _roleService.Context.Ado.CommitTranAsync();
                return Ok(roleInfo.Role);
            }
            catch (Exception ex)
            {
                //异常事务回滚
                await _roleService.Context.Ado.RollbackTranAsync();

                await _actionLogService.AddActionLog(new ActionLog { Title = "AddOrUpdateRole Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }


        [HttpPost]
        public async Task<ActionResult<PageResult<Role>>> RoleList([FromBody] PageData pageData)
        {
            try
            {
                var roleList = await _roleService.GetRoleList(pageData);
                return Ok(roleList);
            }
            catch (Exception ex)
            {
                await _actionLogService.AddActionLog(new ActionLog { Title = "RoleList Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }

        [HttpPost]
        public async Task<ActionResult<List<Role>>> AllRoleList()
        {
            try
            {
                var roleList = await _roleService.AllRoleList();
                return Ok(roleList);
            }
            catch (Exception ex)
            {
                await _actionLogService.AddActionLog(new ActionLog { Title = "AllRoleList Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }

        [HttpPost]
        public async Task<ActionResult> RoleById([FromBody] Guid Id)
        {
            try
            {
                var role = await _roleService.GetByIdAsync(Id).ConfigureAwait(false);
                return Ok(role);
            }
            catch (Exception ex)
            {
                await _actionLogService.AddActionLog(new ActionLog { Title = "RoleById Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }

        [HttpPost]
        public async Task<ActionResult<List<Guid>>> RoleMenus([FromBody] Guid Id)
        {
            try
            {
                var roleMenus = await _userMenuInRoleService.SelectRoleUserMenuIdsInRoles(Id).ConfigureAwait(false);
                return Ok(roleMenus);
            }
            catch (Exception ex)
            {
                await _actionLogService.AddActionLog(new ActionLog { Title = "RoleMenus Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }


        [HttpPost]
        public async Task<ActionResult> DeleteRole([FromBody] Guid Id)
        {
            try
            {
                //开始事务
                await _roleService.Context.Ado.BeginTranAsync();
                var isOK = await _roleService.DeleteByIdAsync(Id);
                await _userMenuInRoleService.DeleteRoleUserMenuInRoles(Id);
                //提交事务
                await _roleService.Context.Ado.CommitTranAsync();
                return Ok(isOK);
            }
            catch (Exception ex)
            {
                //异常事务回滚
                await _roleService.Context.Ado.RollbackTranAsync();
                await _actionLogService.AddActionLog(new ActionLog { Title = "DeleteRole Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }
    }
}
