using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
using System.Collections.Generic;
using System.IO;
using Teachers.Models;
using Teachers.ViewModels;

namespace Teachers;

public partial class AddCourseView : UserControl
{
    public AddCourseView()
    {
        InitializeComponent();
        DataContext = new AddTeacherViewModel();
    }

    private async void bAddCourse(object sender, RoutedEventArgs e)
    {
        AddCourse();
        var window = this.VisualRoot as Window;
        ButtonResult result = await MessageBoxManager.GetMessageBoxStandard("Внимание", "При добавлении курса все заполненные поля будут очищены. Вы уверены, что хотите добавить новый курс?", MsBox.Avalonia.Enums.ButtonEnum.YesNo).ShowAsync();
        switch (result)
        {
            case ButtonResult.Yes:
                {
                    window?.Close();
                    MainWindowViewModel.Instance.Us = new AddTeacherView();
                    break;
                }
            case ButtonResult.No:
                {
                    window?.Close();
                    break;
                }
        }        
    }

    public void AddCourse()
    {
        Course newCourse = new Course() { Name = tbCourse.Text, Duration = int.Parse(tbDuration.Text) };
        MainWindowViewModel.myConnection.Courses.Add(newCourse);
        MainWindowViewModel.myConnection.SaveChanges();
    }
}