using AngleSharp.Dom;
using Masuit.Tools;
using Masuit.Tools.Models;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using PVDEMCS.Common;
using PVDEMCS.Common.DI;
using PVDEMCS.Services.Models;
using PVDEMCS.Services.Repositories.Entities;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace PVDEMCS.Services.Repositories
{
    /// <summary>
    /// 用户信息 数据层 实现
    /// </summary>
    internal class SysUserRepository : ISysUserRepository
    {
        /// <summary>
        /// 获取用户信息分页列表
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="activated">是否其哟个</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        public Result<PagedList<SysUser>> GetSysUserPageList(string userName, bool? activated, int page, int size)
        {
            using (var context = new EFContext())
            {
                var query = context.SysUsers.Where(f => !f.IsDelete).Select(f => new SysUser
                {
                    Id = f.Id,
                    UserName = f.UserName,
                    Activated = f.Activated,
                    Avatar = f.Avatar,
                    Email = f.Email,
                    LoginDate = f.LoginDate,
                    LoginIp = f.LoginIp,
                    NickName = f.NickName,
                    Phonenumber = f.Phonenumber,
                    Remark = f.Remark,
                    Sex = f.Sex,
                    UserType = f.UserType,
                });

                if (!userName.IsNullOrEmpty())
                {
                    query = query.Where(f => f.UserName.StartsWith(userName));
                }
                if (activated.HasValue)
                {
                    query = query.Where(f => f.Activated == activated);
                }
                var pageList = query.ToPagedList(page, size);

                return new Result<PagedList<SysUser>>(pageList);
            }
        }

        /// <summary>
        /// 获取用户明细
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="userName">名称</param>
        /// <returns></returns>
        public Result<SysUserEntity> GetSysUserDetail(string id, string userName)
        {
            var result = new Result<SysUserEntity>();
            using (var context = new EFContext())
            {
                var query = context.SysUsers.Where(f => !f.IsDelete);
                if (!id.IsNullOrEmpty())
                {
                    query = query.Where(f => f.Id == id);
                }
                if (!userName.IsNullOrEmpty())
                {
                    query = query.Where(f => f.UserName == userName);
                }
                var entity = query.FirstOrDefault();
                if (entity.IsNullOrEmpty())
                {
                    if (!id.IsNullOrEmpty())
                    {
                        result.Message = $"没有找到Id:【{id}】的数据";
                    }

                    if (!userName.IsNullOrEmpty())
                    {
                        result.Message = $"没有找到userName:【{userName}】的数据";
                    }

                    return result;
                }

                result.Content = entity;
                return result;

            }
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Result AddSysUser(SysUserEntity entity)
        {
            var result = new Result<SysUserEntity>();
            using (var context = new EFContext())
            {
                var query = context.SysUsers.Where(f => !f.IsDelete);
                if (query.Where(f => f.UserName == entity.UserName).FirstOrDefault() != null)
                {
                    result.Message = $"已存在用户名为:【{entity.UserName}】的数据";
                    return result;
                }

                entity.Create();
                context.SysUsers.Add(entity);
                context.SaveChanges();

                return result;
            }
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Result UpdateSysUser(SysUserEntity entity)
        {
            var result = new Result<SysUserEntity>();
            using (var context = new EFContext())
            {
                var query = context.SysUsers.Where(f => !f.IsDelete);
                var update = query.Where(f => f.Id == entity.Id).FirstOrDefault();
                if (update == null)
                {
                    result.Message = $"没有找到Id:【{entity.Id}】的数据";
                    return result;
                }
                if (query.Where(f => f.UserName == entity.UserName && f.Id != entity.Id).FirstOrDefault() != null)
                {
                    result.Message = $"已存在用户名为:【{entity.UserName}】的数据";
                    return result;
                }
                update.UserName = entity.UserName;
                update.UserType = entity.UserType;
                update.Activated = entity.Activated;
                update.Avatar = entity.Avatar;
                update.Email = entity.Email;
                update.NickName = entity.NickName;
                update.UserName = entity.UserName;
                update.LoginDate = entity.LoginDate;
                //update.Password = entity.Password;
                update.Phonenumber = entity.Phonenumber;
                update.Sex = entity.Sex;
                update.Remark = entity.Remark;
                update.Update();
                context.SaveChanges();
                return result;
            }
        }

        /// <summary>
        /// 更新用户密码
        /// </summary>
        /// <param name="id">用户id</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public Result UpdatePassword(string id, string pwd) 
        {
            using (var context=new EFContext())
            {
                var result = new Result();
                var entity = context.SysUsers.Where(f => f.Id == id).FirstOrDefault();
                if (entity == null)
                {
                    result.Message = $"没有找到Id:【{entity.Id}】的数据";
                    return result;
                }
                entity.Password= pwd;
                entity.Update();
                context.SaveChanges();

                return result;
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result DeleteUser(string id)
        {
            using (var context = new EFContext())
            {
                var result = new Result();

                var entity = context.SysUsers.Where(f => f.Id == id).FirstOrDefault();
                if (entity == null)
                {
                    result.Message = $"没有找到Id:【{id}】的数据";
                    return result;
                }

                entity.Delete();
                context.SaveChanges();

                return result;
            }
        }
    }
}
