public MainWindow()
 {
     InitializeComponent();
     DBHandler.Connect();
 }
        private void TbWrittenPhrase_KeyDown(object sender, KeyEventArgs e)
        {
            if (tbWrittenPhrase.Text.Length == tbSamplePhrase.Text.Length - 1)
            {
                string phrase = tbWrittenPhrase.Text + e.Key.ToString().ToLower();
                string sample = tbSamplePhrase.Text;
                tbWrittenPhrase.Text = phrase;

                int nonSimilarity = 0;

                for (int i = 0; i < sample.Length; i++)
                {
                    if (phrase[i] != sample[i])
                    {
                        nonSimilarity++;
                    }
                }

                if (nonSimilarity > 5)
                {
                    ra = new RegistrationArray(sample.Length - 1);
                    MessageBox.Show("Не совпадаеть");
                    tbSamplePhrase.Text  = "";
                    tbWrittenPhrase.Text = "";
                    tbUsername.IsEnabled = true;

                    authorizationProcessStarted = false;


                    return;
                }

                if (DBHandler.Authorize(tbUsername.Text, ra.CountPerfect()))
                {
                    MessageBox.Show("Авторизация успешна");

                    timer = DateTime.MinValue;

                    tbSamplePhrase.Text         = "";
                    tbWrittenPhrase.Text        = "";
                    tbUsername.IsEnabled        = true;
                    authorizationProcessStarted = false;

                    ApplicationWindow aw = new ApplicationWindow();
                    aw.Show();
                }
                else
                {
                    MessageBox.Show("Скорости не совпадают, повторите попытку");
                    ra    = new RegistrationArray(sample.Length - 1);
                    timer = DateTime.MinValue;
                    tbSamplePhrase.Text         = "";
                    tbWrittenPhrase.Text        = "";
                    tbUsername.IsEnabled        = true;
                    authorizationProcessStarted = false;

                    e.Handled = true;
                }
            }
            if (e.Key == Key.LeftShift)
            {
                if (!authorizationProcessStarted)
                {
                    if (!DBHandler.IsUserExists(tbUsername.Text))
                    {
                        MessageBox.Show("Такого пользователя не существует");
                        return;
                    }
                    string sampleEncrypted = DBHandler.GetAuthorizationString(tbUsername.Text);

                    string sample = Decryptor.Decrypt(sampleEncrypted);

                    authorizationProcessStarted = true;
                    tbUsername.IsEnabled        = false;

                    ra = new RegistrationArray(sample.Length - 1);
                    tbSamplePhrase.Text = sample;
                }
            }
            if (e.Key == Key.Back)
            {
                ra.Pop();
            }

            if (authorizationProcessStarted && e.Key != Key.Back && e.Key != Key.LeftShift)
            {
                if (timer != DateTime.MinValue)
                {
                    ra.Add(DateTime.Now - timer);
                }
                timer = DateTime.Now;
            }
        }
        private void TbWrittenPhrase_KeyDown(object sender, KeyEventArgs e)
        {
            if (tbWrittenPhrase.Text.Length == tbSamplePhrase.Text.Length - 1)
            {
                string phrase       = tbWrittenPhrase.Text + e.Key.ToString().ToLower();
                string samplePhrase = tbSamplePhrase.Text;

                int treshold = samplePhrase.Length * 10 / 100;

                int similarity = 0;

                for (int i = 0; i < samplePhrase.Length; i++)
                {
                    if (phrase[i] == samplePhrase[i])
                    {
                        similarity++;
                    }
                }

                if (similarity < samplePhrase.Length - treshold)
                {
                    MessageBox.Show("Вообще мимо");
                    return;
                }

                registrationStage++;
                tbWrittenPhrase.Text = "";

                timer          = DateTime.MinValue;
                label2.Content = 3 - registrationStage;
                e.Handled      = true;
            }

            if (registrationStage == 3)
            {
                double perfect = ra.CountPerfect();

                label1.Visibility = Visibility.Hidden;
                label2.Visibility = Visibility.Hidden;

                tbUsername.IsEnabled = true;

                if (DBHandler.RegisterUser(tbUsername.Text, perfect, userQuestionID))
                {
                    MessageBox.Show("Регистрация прошла успешно");
                    registrationProcessStarted = false;
                    registrationStage          = 0;
                    return;
                }

                MessageBox.Show("Что-то пошло не так");
                registrationProcessStarted = false;
                registrationStage          = 0;
                return;
            }

            if (e.Key == Key.LeftShift)
            {
                if (!registrationProcessStarted)
                {
                    if (tbUsername.Text != "")
                    {
                        if (DBHandler.IsUserExists(tbUsername.Text))
                        {
                            MessageBox.Show("Пользователь с таким именем уже существует");
                            return;
                        }

                        label1.Visibility = Visibility.Visible;
                        label2.Visibility = Visibility.Visible;

                        label2.Content = 3;

                        tbUsername.IsEnabled = false;

                        registrationProcessStarted = true;

                        string regStrEncrypted = DBHandler.GetRegistrationString(ref userQuestionID);

                        string regStr = Decryptor.Decrypt(regStrEncrypted);

                        ra = new RegistrationArray(regStr.Length * 3 - 3);

                        tbSamplePhrase.Text = regStr;
                    }
                    else
                    {
                        MessageBox.Show("Имя напиши, гадость!");
                    }
                }
            }

            if (e.Key == Key.Back)
            {
                ra.Pop();
            }

            if (registrationProcessStarted && e.Key != Key.Back && e.Key != Key.LeftShift)
            {
                if (timer != DateTime.MinValue)
                {
                    ra.Add(DateTime.Now - timer);
                }
                timer = DateTime.Now;
            }
        }