using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using demko_term.Models;

namespace demko_term;

public partial class AcceptStatementWindow : Window
{
    
    public List<string> AcceptApplicantList { get; set; }
    public List<string> AcceptSpecialtyList { get; set; }
    
    public AcceptStatementWindow()
    {
        using var context = new DemoCourseworkContext();
        InitializeComponent();
        DisplayComboBoxes();
    }

    public void LoadComboBoxes()
    {
        using var context = new DemoCourseworkContext();
        AcceptApplicantList = context.Applicants.Select(a => a.Code.ToString()).ToList();
        
        AcceptSpecialtyList = context.Specialties.Select(s => s.Code).ToList();
    }

    public void DisplayComboBoxes()
    {
        LoadComboBoxes();
        
        AcceptApplicantComboBox.ItemsSource = AcceptApplicantList;
        AcceptSpecialtyListBox.ItemsSource = AcceptSpecialtyList;
    }
    
    private void AcceptStatementButton_OnClick(object? sender, RoutedEventArgs e)
    {
        using var context = new DemoCourseworkContext();
        
        if (AcceptApplicantComboBox.SelectedItem == null || AcceptSpecialtyListBox.SelectedItems == null)
        {
            return;
        }
        
        string selectedApplicantCodeStr = AcceptApplicantComboBox.SelectedItem.ToString()!;
        var applicant = context.Applicants.FirstOrDefault(a => a.Code.ToString() == selectedApplicantCodeStr);

        if (applicant == null)
        {
            return;
        }
        
        int? points = null;
        if (int.TryParse(PointsTextBox.Text, out int parsedPoints))
        {
            points = parsedPoints;
        }
        
        foreach (var selectedSpecialtyCode in AcceptSpecialtyListBox.SelectedItems.Cast<string>())
        {
            var specialty = context.Specialties.FirstOrDefault(s => s.Code == selectedSpecialtyCode);
            if (specialty == null)
                continue;
            
            string personalNumber = $"{specialty.Code}-{applicant.Code}";

            var statement = new Statement
            {
                PersonalNumber = personalNumber,
                SubmissionDate = DateOnly.FromDateTime(DateTime.Now),
                SubmissionTime = TimeOnly.FromDateTime(DateTime.Now),
                ApplicantCode = applicant.Id,
                ApplicantSpeciality = specialty.Id,
                Passport = PassportCheckBox.IsChecked,
                Diploma = DiplomaCheckBox.IsChecked,
                Points = points
            };

            context.Statements.Add(statement);
        }

        context.SaveChanges();
        
    }
    
    private async void CreateApplicantWindowButton_OnClick(object? sender, RoutedEventArgs e)
    {
        CreateApplicantWindow createApplicantWindow = new CreateApplicantWindow();
        await createApplicantWindow.ShowDialog(this);
        
        DisplayComboBoxes();
    }
}