using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using demko_term.Models;
using Microsoft.EntityFrameworkCore;

namespace demko_term;

public partial class FormPersonalFileWindow : Window
{
    
    private ObservableCollection<StatementPresenter> statements = new();
    private List<StatementPresenter> dataSourceStatements;
    
    private List<string> ApplicantCodeList;
    
    public FormPersonalFileWindow()
    {
        InitializeComponent();
        using var context = new DemoCourseworkContext();
        
        ApplicantCodeList = context.Statements
            .Include(a => a.ApplicantCodeNavigation)
            .Select(a => a.ApplicantCodeNavigation.Code.ToString()).Distinct().ToList();
        ApplicantCodeListComboBox.ItemsSource = ApplicantCodeList;
        
        DisplayStatements();
    }
    
    public class StatementPresenter : Statement
    {
        public string ApplicantCode { get; set; }
        
        public string ApplicantSpecialty { get; set; }
        
        public bool? PassportBool { get; set; }
        
        public bool? DiplomaBool { get; set; }
        
        public string Passport => PassportBool.HasValue ? (PassportBool.Value ? "Да" : "Нет") : "";
        
        public string Diploma => DiplomaBool.HasValue ? (DiplomaBool.Value ? "Да" : "Нет") : "";
    }

    public void LoadData()
    {
        using var context = new DemoCourseworkContext();
        dataSourceStatements = context.Statements
            .Include(s => s.ApplicantCodeNavigation)
            .Include(s => s.ApplicantSpecialityNavigation)
            .Select(s => new StatementPresenter
            {
                Id = s.Id,
                PersonalNumber = s.PersonalNumber,
                SubmissionDate = s.SubmissionDate,
                SubmissionTime = s.SubmissionTime,
                ApplicantCode = s.ApplicantCodeNavigation.Code.ToString(),
                ApplicantSpecialty = s.ApplicantSpecialityNavigation.Code.ToString(),
                PassportBool = s.Passport,
                DiplomaBool = s.Diploma,
                Points = s.Points,
            }).ToList();
    }
    
    private void DisplayStatements()
    {
        LoadData();
        
        var temp = dataSourceStatements;
        
        if (ApplicantCodeListComboBox.SelectedItem is string selectedApplicantCode)
        {
            temp = temp.Where(it => it.ApplicantCode == selectedApplicantCode).ToList();
        }

        statements.Clear();
        foreach (var item in temp)
        {
            statements.Add(item);
        }
        
        StatementListBox.ItemsSource = statements;
    }

    public void ApplicantCodeListComboBox_OnSelectionChanged(object? sender, RoutedEventArgs e)
    {
        DisplayStatements();
    }
    
    private void ViewApplicantButton_OnClick(object? sender, RoutedEventArgs e)
    {
        ViewApplicantWindow viewApplicantWindow = new ViewApplicantWindow();
        viewApplicantWindow.ShowDialog(this);
    }
}