using Long.Core.Query;
using Long.DAO.Basedata;
using Long.Common.Basedata;
using Long.Utils.Encryptor;
using Microsoft.Extensions.Configuration;
using Long.DAO.System;
using Long.Core.Extensions;

namespace Long.Service.UserService.Basedata
{
    /// <summary>
    /// 系统用户·服务
    /// </summary>
    public class UserService : IUserService
    {
        private readonly IConfiguration configuration;
        private readonly IUserDatabase _userDatabase = new UserDatabase();
        private readonly INextValueDatabase _nextValueDatabase = new NextValueDatabase();

        public UserService(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        /// <summary>
        /// 获取全部实体列表
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<User>> Get()
        {
            return await _userDatabase.GetAsync();
        }

        /// <summary>
        /// 通过条件获取实体列表
        /// </summary>
        /// <param name="queryParamenter"></param>
        /// <returns></returns>
        public async Task<IEnumerable<User>> Get(QueryParamenter queryParamenter)
        {
            return await _userDatabase.GetAsync(queryParamenter);
        }

        /// <summary>
        /// 通过 Id 获取系统用户实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<User> GetById(long id)
        {
            return await _userDatabase.GetFirstAsync(id);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public async Task<User> GetByUserName(string userName)
        {
            return await _userDatabase.GetByUserNameAsync(userName);
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<long> Regist(RegistInput input)
        {
            string key = configuration.GetSection("EncryptorKey").Value ?? "";
            var user = await _userDatabase.GetUserAsync(input.UserName);
            if (user != null)
            {
                throw new ApplicationException("该用户已注册。");
            }

            user = new User();

            if (input.UserName.IsEmail())
            {
                user.Email = input.UserName;
            }
            else if (input.UserName.IsPhone())
            {
                user.Phone = input.UserName;
            }
            else
            {
                throw new ApplicationException("请输入正确的账号。");
            }

            user.Password = DESEncryptor.Encrypt(input.Password, key);
            user.Opened = true;
            user.Activited = true;
            user.ActivationCode = new Random(999999).Next().ToString().PadLeft('0');
            user.ActivationCodeOn = DateTime.Now;

            long id = await _userDatabase.InsertAsync(user);

            // 发送激活邮件
            var url = CreateActivetionUrl(user.PromotionCode);

            return id;
        }

        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="id">内码</param>
        /// <param name="password">新密码</param>
        /// <returns>记录更新数</returns>
        public async Task<int> UpdatePassword(long id, string password)
        {
            string key = this.configuration.GetSection("EncryptorKey").Value ?? "";
            return await _userDatabase.UpdatePassword(id, DESEncryptor.Encrypt(password, key));
        }

        /// <summary>
        /// 创建激活链接
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public string CreateActivetionUrl(string code)
        {
            var str = Environment.UserDomainName;
            return $"https://";
        }
    }
}
