using LifePayment.Application.Contracts;
using LifePayment.Domain.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Identity;
using ZeroD.Util;

namespace LifePayment.HttpApi
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UserRoleController : AbpController
    {
        private readonly IAccountService _accountService;
        private readonly IUserRoleService _userRoleService;
        private readonly IIdentityUserAppService _identityUserService;
        private readonly IIdentityRoleAppService _identityRoleService;

        public UserRoleController(
               IAccountService accountService,
               IUserRoleService userRoleService,
               IIdentityUserAppService identityUserService,
               IIdentityRoleAppService identityRoleService)
        {
            _accountService = accountService;
            _userRoleService = userRoleService;
            _identityUserService = identityUserService;
            _identityRoleService = identityRoleService;
        }

        /// <summary>
        /// 新增后台管理账户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Guid> CreateBackClientUser(CreateBackClientUserInput input)
        {


            return await _accountService.CreateAccount(ObjectMapper.Map<CreateBackClientUserInput, CreateAccountInput>(input), isAdminCreate: true);
        }

        /// <summary>
        /// 后台管理账户列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<UserDto>> GetBackClientUsers(GetBackClientUsersInput input)
        {
            return await _userRoleService.GetBackClientUsers(input);
        }

        /// <summary>
        /// 后台管理账户编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateBackClientUser(UpdateBackClientUserInput input)
        {

            var identityUser = await _identityUserService.GetAsync(input.Id);

            CheckExtensions.IfTrueThrowUserFriendlyException(identityUser == null,
                                                             "用户不存在");
            await _identityUserService.UpdateAsync(input.Id, new IdentityUserUpdateDto
            {
                Name = input.Name,
                PhoneNumber = input.PhoneNumber,
                UserName = input.UserName,
                RoleNames = input.RoleNames,
            });
            return await _userRoleService.UpdateBackClientUser(input);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual async Task<int> ResetUserPassword(ResetPasswordBaseInput input)
        {
            await _userRoleService.ResetPassword(input);

            return Constant.SUCCESS;
        }

        /// <summary>
        /// 删除后台管理账户
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> DeleteBackClientUser(Guid id)
        {
            await _identityUserService.DeleteAsync(id);
            return await _userRoleService.DeleteBackClientUser(id);
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Guid> CreateRole(CreateBaseRoleInput input)
        {
            if (input.DataRange == LifePaymentConstant.DataRange.PowerPerson)
            {
                input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerPerson, string.Empty) + LifePaymentConstant.DataRangePower.PowerPerson;
            }
            else
            {
                input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerAll, string.Empty) + LifePaymentConstant.DataRangePower.PowerAll;
            }

            var res = await _identityRoleService.CreateAsync(new IdentityRoleCreateDto
            {
                Name = input.Name,
                Sequence = input.Sequence,
                Note = input.Remark,
            });
            var newInput = ObjectMapper.Map<CreateBaseRoleInput, CreateOrUpdateRoleInput>(input);
            newInput.Id = res.Id;
            await _userRoleService.CreateRole(newInput);
            return res.Id;
        }

        /// <summary>
        /// 角色列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<RoleInfo>> GetRoles(GetRolesInput input)
        {
            return await _userRoleService.GetRoles(input);
        }

        /// <summary>
        /// 角色编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateRole(CreateOrUpdateRoleInput input)
        {
            if (input.DataRange == LifePaymentConstant.DataRange.PowerPerson)
            {
                input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerPerson, string.Empty) + LifePaymentConstant.DataRangePower.PowerPerson;
            }
            else
            {
                input.Name = input.Name.Replace(LifePaymentConstant.DataRangePower.PowerAll, string.Empty) + LifePaymentConstant.DataRangePower.PowerAll;
            }


            await _identityRoleService.UpdateAsync(input.Id, new IdentityRoleUpdateDto
            {
                Name = input.Name,
                Sequence = input.Sequence,
                Note = input.Remark,
            });
            return await _userRoleService.UpdateRole(input);
        }

        /// <summary>
        /// 角色启用/禁用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> RoleEnableOrForbid(RoleEnableOrForbidInput input)
        {
            await _identityRoleService.UpdateAsync(input.Id, new IdentityRoleUpdateDto
            {
                IsSetEnable = true,
                IsEnable = input.IsEnable,
                Name = input.Name,
            });
            return await _userRoleService.RoleEnableOrForbid(input.Id, input.IsEnable);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> DeleteRole(Guid id)
        {
            await _identityRoleService.DeleteAsync(id);
            return await _userRoleService.DeleteRole(id);
        }
    }
}