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 Hotel_Course_Project
{
    /// <summary>
    /// Логика взаимодействия для HelpQustionWindow.xaml
    /// </summary>
    public partial class HelpQustionWindow : Window
    {
        public Staff _user;
        public HelpQustionWindow(Staff user)
        {
            InitializeComponent();
            _user = user;
            TypeQuestionCB.ItemsSource = DataBase.db.HelpQuestion.ToList();
            if (user.Id_HelpQuestion != null)
            {
                TypeQuestionCB.Text = user.ToString();
                TypeQuestionCB.IsEditable = false;
            }
            else
            {
                TypeQuestionCB.IsEditable = true;
            }
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void AcceptBtn_Click(object sender, RoutedEventArgs e)
        {
            if (TypeQuestionCB.SelectedItem == null)
            {
                MessageBox.Show("Выберите вопрос");
            }
            else if (AnswerTB.Text == "" || AnswerTB.Text == string.Empty)
            {
                MessageBox.Show("Поле с ответом -- пустое");
            }
            else
            {
                int numQuestion = DataBase.db.HelpQuestion.SingleOrDefault(x => x.Name == TypeQuestionCB.Text).id;
                if (_user.Id_HelpQuestion == null)
                {
                    _user.Id_HelpQuestion = numQuestion;
                    _user.AnswerOnHelpQuestion = AnswerTB.Text;
                    MessageBox.Show("Способ восстановления пароля добавлен");
                    DialogResult = true;
                }
                else
                {
                    if (_user.Id_HelpQuestion != numQuestion || (_user.Id_HelpQuestion == numQuestion && _user.AnswerOnHelpQuestion != AnswerTB.Text))
                    {
                        MessageBox.Show("Ошибка при восстановлении пароля");
                        AnswerTB.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("Пароль сброшен!\nНовый пароль: 123");
                        _user.Password = SomeMethods.Hash("123");
                        DataBase.db.SaveChanges();
                        DialogResult = true;
                    }
                }
            }
        }
    }
}
