using System.Windows.Controls;
using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
using System.Windows;
using System;
using PDF_Office.ViewModels.Dialog.ToolsDialogs.SaftyDialogs;

namespace PDF_Office.Views.Dialog.ToolsDialogs.SaftyDialogs
{
    /// <summary>
    /// Interaction logic for SetPasswordDialog
    /// </summary>
    public partial class SetPasswordDialog : UserControl
    {
        private Boolean canOpenPasswordBoxHasFocus = false;
        private Boolean canOpenTextBoxHasFocus = false;

        private Boolean canEditPasswordBoxHasFocus = false;
        private Boolean canEditTextBoxHasFocus = false;

        private string canOpenPasswordBoxName = "CanOpenPasswordBox";
        private string canEditPasswordBoxName = "CanEditPasswordBox";

        private string canOpenTextBoxName = "CanOpenTextBox";
        private string canEditTextBoxName = "CanEditTextBox";

        private string showOpenPasswordBoxEyeLabelName = "ShowOpenPasswordBoxEyeLabel";
        private string showEditPasswordBoxEyeLabelName = "ShowEditPasswordBoxEyeLabel";

        private string showOpenTextBoxEyeLabelName = "ShowOpenTextBoxEyeLabel";
        private string showEditTextBoxEyeLabelName = "ShowEditTextBoxEyeLabel";

        public SetPasswordDialog()
        {
            InitializeComponent();
        }

        private void PasswordBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var passwordBox = sender as PasswordBox;
            if (passwordBox.Name == canOpenPasswordBoxName)
            {
                canOpenPasswordBoxHasFocus = true;
                canOpenTextBoxHasFocus = false;
            }
            else if (passwordBox.Name == canEditPasswordBoxName)
            {
                canEditPasswordBoxHasFocus = true;
                canEditTextBoxHasFocus = false;
            }
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox.Name == canOpenTextBoxName)
            {
                canOpenPasswordBoxHasFocus = false;
                canOpenTextBoxHasFocus = true;
            }
            else if (textBox.Name == canEditTextBoxName)
            {
                canEditPasswordBoxHasFocus = false;
                canEditTextBoxHasFocus = true;
            }
        }

        private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            var passwordBox = sender as PasswordBox;
            if (passwordBox.Name == canOpenPasswordBoxName)
            {
                if (canOpenTextBoxHasFocus)
                {
                    return;
                }
                CanOpenTextBox.Text = CanOpenPasswordBox.Password;
                SetPasswordDialogModel.PasswordForOpen = CanOpenTextBox.Text;
                ((SetPasswordDialogViewModel)this.DataContext).DelegateCanOpenTextChangedCommand.Execute();
            }
            else if (passwordBox.Name == canEditPasswordBoxName)
            {
                if (canEditTextBoxHasFocus)
                {
                    return;
                }
                CanEditTextBox.Text = CanEditPasswordBox.Password;
                SetPasswordDialogModel.PasswordForEdit = CanEditTextBox.Text;
                ((SetPasswordDialogViewModel)this.DataContext).DelegateCanOpenTextChangedCommand.Execute();
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox.Name == canOpenTextBoxName)
            {
                if (canOpenPasswordBoxHasFocus)
                {
                    return; 
                }
                CanOpenPasswordBox.Password = CanOpenTextBox.Text;
                SetPasswordDialogModel.PasswordForOpen = CanOpenPasswordBox.Password;
                ((SetPasswordDialogViewModel)this.DataContext).DelegateCanOpenTextChangedCommand.Execute();
            }
            else if (textBox.Name == canEditTextBoxName)
            {
                if (canEditPasswordBoxHasFocus)
                { return; }
                CanEditPasswordBox.Password = CanEditTextBox.Text;
                SetPasswordDialogModel.PasswordForEdit = CanEditPasswordBox.Password;
                ((SetPasswordDialogViewModel)this.DataContext).DelegateCanOpenTextChangedCommand.Execute();
            }
        }

        private void ShowPasswordBoxEyeLabel_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var label = sender as Label;
            if (label.Name == showOpenPasswordBoxEyeLabelName)
            {
                CanOpenPasswordBox.Visibility = Visibility.Hidden;
                ShowOpenPasswordBoxEyeLabel.Visibility = Visibility.Hidden;

                CanOpenTextBox.Visibility = Visibility.Visible;
                ShowOpenTextBoxEyeLabel.Visibility = Visibility.Visible;
            }
            else if (label.Name == showEditPasswordBoxEyeLabelName)
            {
                CanEditPasswordBox.Visibility = Visibility.Hidden;
                ShowEditPasswordBoxEyeLabel.Visibility = Visibility.Hidden;

                CanEditTextBox.Visibility = Visibility.Visible;
                ShowEditTextBoxEyeLabel.Visibility = Visibility.Visible;
            }
        }

        private void ShowTextBoxEyeLabel_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var label = sender as Label;
            if (label.Name == showOpenTextBoxEyeLabelName)
            {
                CanOpenPasswordBox.Visibility = Visibility.Visible;
                ShowOpenPasswordBoxEyeLabel.Visibility = Visibility.Visible;

                CanOpenTextBox.Visibility = Visibility.Hidden;
                ShowOpenTextBoxEyeLabel.Visibility = Visibility.Hidden;
            }
            else if (label.Name == showEditTextBoxEyeLabelName)
            {
                CanEditPasswordBox.Visibility = Visibility.Visible;
                ShowEditPasswordBoxEyeLabel.Visibility = Visibility.Visible;

                CanEditTextBox.Visibility = Visibility.Hidden;
                ShowEditTextBoxEyeLabel.Visibility = Visibility.Hidden;
            }
        }
    }
}
