using HslCommunication.Enthernet.Redis;
using MWS.Data.InterFace;
using MWS.Data.Manager;
using MWS.Library.Common;
using MWS.Library.Common.MD5Poxy;
using MWS.Model;
using MWSDomain.Common.CoreLib;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MWS.Domain.View.SystemManage
{
    /// <summary>
    /// PasswordManagePage.xaml 的交互逻辑
    /// </summary>
    public partial class PasswordManagePage : Page
    {

        IUser user;
        private RedisClient _redisClient;
        private User t_User;
        public PasswordManagePage()
        {
            InitializeComponent();
            _redisClient = new RedisClient("127.0.0.1", 6379, null);
            _redisClient.SelectDB(1);
            user = new UserManager();
            t_User = new User();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var oldpassword = oldPassword.Text.Trim();
            var newpassword = newPassword.Text.Trim();
            var repeat = repeatPassword.Text.Trim();
            if (string.IsNullOrEmpty(oldpassword))
            {
                Msg.Error("旧密码不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(newpassword))
            {
                Msg.Error("新密码不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(repeat))
            {
                Msg.Error("确认密码不能为空！");
                return;
            }
            if (!newpassword.Equals(repeat))
            {
                Msg.Error("两次密码输入不一致，请重新输入");
                return;
            }
            //获取redis中的用户账号
            JObject userObject;
            var operateResult = _redisClient.ReadKey("UserLogin");
            if (operateResult.Content != null)
            {
                userObject = JObject.Parse(operateResult.Content);
                t_User.Id = userObject.GetValue("Id").ToSafeInt();
                t_User.Account = userObject.GetValue("Account").ToSafeString();
                t_User.UserName = userObject.GetValue("UserName").ToSafeString();
                t_User.Password = userObject.GetValue("Password").ToSafeString();
                
            }
            var loginTask = await user.Login(t_User.Account, CEncoder.Encode(oldpassword));
            if (loginTask.Success)
            {
                if (user.UpdatePassWord(t_User.Account, CEncoder.Encode(newpassword)) > 0)
                {
                    userObject = new JObject {
                                   new JProperty("Id", t_User.Id),
                                   new JProperty("Account", t_User.Account),
                                   new JProperty("UserName", t_User.UserName),
                                   new JProperty("Password",CEncoder.Encode(newpassword)),
                                 };
                    var result = _redisClient.WriteKey("UserLogin", userObject.ToString());
                    Msg.Error("密码修改成功！");
                    //更新缓存
                }
                else
                {

                    Msg.Error("密码修改失败！");
                }
            }
            else
            {
                Msg.Error("密码错误，请确认！");
                return;
            }

        }
    }
}
