// /************************************************************************
// * @file         PasswordChangeDialog.xaml.cs
// * @author   Su Liang
// * @date       2023/03/09
// *
// * @copyright &copy Sicentury Inc.
// *
// * @brief
// *
// * @details
// *
// *
// * *****************************************************************************/

using System.Windows;
using System.Windows.Media;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.Common.Utilities;

namespace MECF.Framework.UI.Client.Ctrlib.Controls
{
    public partial class PanelLockerPasswordChanger : Window
    {
        public PanelLockerPasswordChanger()
        {
            InitializeComponent();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            txtOldPass.Focus();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            #region Validation

            if (txtOldPass.Password == "")
            {
                txtInfo.Text = "Invalid Old Password";
                txtOldPass.Focus();
                return;
            }

            if (txtNewPass.Password == "")
            {
                txtInfo.Text = "Invalid New Password";
                txtNewPass.Focus();
                return;
            }

            if (txtNewPass.Password != txtConfirmPass.Password)
            {
                txtInfo.Text = "New password does not equal to Confirm Password";
                txtNewPass.Focus();
                return;
            }

            if (txtNewPass.Password == txtOldPass.Password)
            {
                txtInfo.Text = "New password can not be equal to old password";
                txtOldPass.Focus();
                return;
            }

            #endregion

            // 获取密码密文
            var oldPassHash = Sha256.EncodeString(txtOldPass.Password);
            var newPassHash = Sha256.EncodeString(txtNewPass.Password);
            
            // 发送到RT进行保存
            InvokeClient.Instance.Service.DoOperation("System.SetNewRecipeEditorPassword", oldPassHash, newPassHash);
            DialogResult = true;
        }
        
        
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
    }
}