using InABox.Clients;
using InABox.Configuration;
using InABox.Core;
using InABox.Wpf;
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.Shapes;
namespace PRSDesktop.Forms
{
///
/// Interaction logic for ChangePassword.xaml
///
public partial class ChangePassword : ThemableWindow
{
private Guid UserGuid { get; set; }
public string? Password { get; set; }
public ChangePassword(Guid userGuid)
{
InitializeComponent();
UserGuid = userGuid;
}
private void SetError(string message)
{
Error.Content = message;
}
public static void ChangeUserPassword(User user, string password)
{
user.Password = password;
}
private void ChangePassword_Click(object sender, RoutedEventArgs e)
{
var newPassword = NewPassword.Password;
if (string.IsNullOrWhiteSpace(newPassword))
{
MessageBox.Show("Please enter a new password.");
return;
}
else if(newPassword != ConfirmPassword.Password)
{
MessageBox.Show("The confirmed password does not match the new password!");
NewPassword.Clear();
ConfirmPassword.Clear();
return;
}
var user = new Client().Query(
new Filter(x => x.ID).IsEqualTo(UserGuid)
.And(x => x.Password).IsEqualTo(CurrentPassword.Password),
Columns.None().Add(x => x.ID)).Rows.FirstOrDefault();
if(user == null)
{
MessageBox.Show("The current password is incorrect!");
CurrentPassword.Clear();
return;
}
else if(newPassword == CurrentPassword.Password)
{
MessageBox.Show("The new password cannot be the same as the old one!");
NewPassword.Clear();
ConfirmPassword.Clear();
return;
}
Password = newPassword;
DialogResult = true;
Close();
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
}
}