using Masuit.Tools.Models;
using Microsoft.AspNetCore.Mvc;
using PVDEMCS.Common;
using PVDEMCS.Services.Models;
using PVDEMCS.Services;
using System;
using Masuit.Tools.Security;
using Masuit.Tools;
using Microsoft.AspNetCore.Identity;
using System.Security.Policy;

namespace PVDEMCS.Controllers
{
    /// <summary>
    /// 用户信息
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class UserController : Controller
    {
        private string salt = "75846542";
        private ISysUserService _sysUserService;

        public UserController(ISysUserService sysUserService)
        {
            this._sysUserService = sysUserService;
        }

        /// <summary>
        /// 获取用户信息分页列表
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="activated">是否其哟个</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        [HttpGet]
        public Result<PagedList<SysUser>> GetSysUserPageList(string userName, bool? activated, int page, int size = 20)
        {
            var result = this._sysUserService.GetSysUserPageList(userName, activated, page, size);
            return result;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        [HttpPost]
        public Result<SysUser> Lgoin(string userName, string pwd)
        {
            var user = _sysUserService.GetSysUserDetail("", userName);
            if (!user.IsSuccess)
            {
                return user;
            }
            if (!user.Content.IsNullOrEmpty())
            {
                var value = Hash(pwd);
                if (user.Content.Password == value)
                {
                    user.Content.Password = null;
                    return user;
                }
            }
            user.Content = null;
            user.Message = "用户名或密码不正确！";
            return user;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id">用户id</param>
        /// <param name="oldPwd">旧密码</param>
        /// <param name="newPwd">新密码</param>
        /// <returns></returns>
        [HttpPost]
        public Result UpdatePassword(string id, string oldPwd, string newPwd)
        {
            var result = this._sysUserService.GetSysUserDetail(id, "");
            if (!result.IsSuccess)
            {
                return result;
            }

            var old = Hash(oldPwd);
            if (old != result.Content.Password)
            {
                return new Result("原密码不正确！");
            }

            var pwd = Hash(newPwd);
            var res = _sysUserService.UpdatePassword(id, pwd);
            if (!res.IsSuccess)
            {
                return res;
            }
            return new Result();

        }

        /// <summary>
        /// 获取用户明细
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns></returns>
        [HttpGet]
        public Result<SysUser> GetSysUserDetail(string id)
        {
            var result = this._sysUserService.GetSysUserDetail(id, "");
            if (result.IsSuccess)
            {
                result.Content.Password = null;
            }
            return result;
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public Result AddSysUser(SysUser info)
        {
            info.Password = Hash(info.Password);
            var result = this._sysUserService.AddSysUser(info);
            return result;
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public Result UpdateSysUser(SysUser info)
        {
            info.Password = null;
            var result = this._sysUserService.UpdateSysUser(info);
            return result;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public Result DeleteSysUser(string id)
        {
            var result = this._sysUserService.DeleteUser(id);
            return result;
        }

        [NonAction]
        private string Hash(string value)
        {
            byte[] passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(value + salt);
            byte[] hashBytes = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes);
            string hashString = Convert.ToBase64String(hashBytes);
            return hashString;
        }
    }
}
