using System.ComponentModel.DataAnnotations;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Interactivity;
using kursovaya.Models;

namespace kursovaya;

public partial class MainWindow : Window
{
    private bool _isPasswordVisible = false;
    public MainWindow()
    {
        InitializeComponent();
        PasswordName.PasswordChar = '•';
    }

    private void Button_OnClick(object? sender, RoutedEventArgs e)
    {
        var login = LoginName.Text;
        var password = PasswordName.Text;

        if (string.IsNullOrWhiteSpace(login) || string.IsNullOrWhiteSpace(password))
        {
            ErrorMessage.Text = "Поля не должны быть пустыми!";
            return;
        }

        var ctx = new DatabaseContext();

        var user = ctx.Users.FirstOrDefault(u => u.Login == login && u.Password == password);
        
        
        if (user == null)
        {
            ErrorMessage.Text = "Вы ввели неверный логин или пароль. Пожалуйста, проверьте ещё раз данные";
            return;
        }
        
        ErrorMessage.Text = "";
        
        switch (user.RoleId)
        {
            case 1:
                StudentWindow studentWindow = new StudentWindow(user);
                studentWindow.ShowDialog(this);
                break;
            case 2:
                TeacherWindow teacherWindow = new TeacherWindow(user);
                teacherWindow.ShowDialog(this);
                break;
            case 3:
                AdminWindow adminWindow = new AdminWindow();
                adminWindow.ShowDialog(this);
                break;
        }
    }
    
    private void TogglePasswordVisibilityClick(object? sender, RoutedEventArgs e)
    {
        _isPasswordVisible = !_isPasswordVisible;

        if (_isPasswordVisible)
        {
            PasswordName.PasswordChar = '\0';
            TogglePasswordVisibility.Content = "Hide";
        }
        else
        {
            PasswordName.PasswordChar = '•';
            TogglePasswordVisibility.Content = "Show";
        }
    }
}