using System;
using System.Windows;
using System.Windows.Controls;

namespace MyAuth
{
    public partial class AccountInfoPanel : UserControl
    {
        public AccountInfoPanel()
        {
            InitializeComponent();

            // handle user name change
            _btnChangeName.Click += _btnChangeName_Click;
            _txtFriendlyName.TextChanged += _txtFriendlyName_TextChanged;

            // handle password change
            _btnChangePassword.Click += _btnChangePassword_Click;
            _txtOldPassword.PasswordChanged += _txt_TextChanged;
            _txtNewPassword.PasswordChanged += _txt_TextChanged;
            _txtNewPassword2.PasswordChanged += _txt_TextChanged;

            // handle log out button
            _btnLogout.Click += _btnLogout_Click;

            // show friendly name
            Loaded += (s, e) =>
                {
                    var model = DataContext as AuthenticationModel;
                    _txtFriendlyName.Text = model != null && model.User != null && model.User.FriendlyName != null
                        ? model.User.FriendlyName
                        : string.Empty;
                };
        }

        // change the password
        void _btnChangePassword_Click(object sender, RoutedEventArgs e)
        {
            ModalDialog.StartOperation(this);
            try
            {
                var pwOld = _txtOldPassword.Password.Trim();
                var pwNew = _txtNewPassword.Password.Trim();
                var model = DataContext as AuthenticationModel;
                model.ChangePassword(pwOld, pwNew, completedChange);
            }
            catch (Exception x)
            {
                completedChange(x);
            }
        }
        void completedChange(Exception x)
        {
            ModalDialog.EndOperation(this, x);
        }

        // enable/diable password change button
        void _txt_TextChanged(object sender, EventArgs e)
        {
            var pwOld = _txtOldPassword.Password.Trim();
            var pwNew = _txtNewPassword.Password.Trim();
            var pwNew2 = _txtNewPassword2.Password.Trim();

            _btnChangePassword.IsEnabled = 
                DataContext is AuthenticationModel &&
                pwOld.Length >= 4 &&
                pwNew.Length >= 4 &&
                pwNew2.Length >= 4 &&
                pwNew == pwNew2 &&
                pwNew != pwOld;
        }

        // change user's friendly name
        void _btnChangeName_Click(object sender, RoutedEventArgs e)
        {
            ModalDialog.StartOperation(this);
            try
            {
                var name = _txtFriendlyName.Text.Trim();
                var model = DataContext as AuthenticationModel;
                model.User.FriendlyName = name;
                model.SaveUserData(completedSaveData);
            }
            catch (Exception x)
            {
                completedSaveData(x);
            }
        }
        void completedSaveData(Exception x)
        {
            ModalDialog.EndOperation(this, x);
        }

        // enable saving name if not empty and different from current
        void _txtFriendlyName_TextChanged(object sender, TextChangedEventArgs e)
        {
            var name = _txtFriendlyName.Text.Trim();
            var model = DataContext as AuthenticationModel;
            _btnChangeName.IsEnabled = 
                model != null && 
                !string.IsNullOrEmpty(name) &&
                name != model.User.FriendlyName;
        }

        // log out
        void _btnLogout_Click(object sender, RoutedEventArgs e)
        {
            ModalDialog.StartOperation(this);
            var model = DataContext as AuthenticationModel;
            if (model != null)
            {
                model.Logout(completedLogOut);
            }
        }
        void completedLogOut(Exception x)
        {
            ModalDialog.EndOperation(this, x);
        }
    }
}
