using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using kursovaya.Models;
using Microsoft.EntityFrameworkCore;

namespace kursovaya;

public partial class StudentWindow : Window
{
    public ObservableCollection<AttestationPresenter> Attestations = new ObservableCollection<AttestationPresenter>();
    List<AttestationPresenter> dataSourceAttestations;
    private Student student;
    public StudentWindow()
    {
        InitializeComponent();
    }
    
    public StudentWindow(User user): this()
    {
        var ctx = new DatabaseContext();
        student = ctx.Students.FirstOrDefault(s => s.Login == user.Login);
        
        string infoPrint = $"ФИО: {student.Fio} Группа: {student.IdGroup}";
        InfoTextBlock.Text = infoPrint;
        
        var disciplineIds = ctx.Attestations.Where(d => d.IdUser == student.Id).Select(d => d.IdDiscipline).ToList();
        var disciplines = ctx.Disciplines.Where(d => disciplineIds.Contains(d.Id)).Select(d => d.Name).ToList();
        
        dataSourceAttestations = ctx.Attestations.Where(at => at.IdUser == student.Id).Select(at => new AttestationPresenter()
        {
            IdUser = at.IdUser,
            IdDiscipline = at.IdDiscipline,
            AttestFirst = at.AttestFirst,
            AttestSecond = at.AttestSecond,
            AttestThird = at.AttestThird,
            Total = at.Total,
            Grade = at.Grade,
        }).ToList();
        
        DisciplineComboBox.ItemsSource = disciplines;
        FlatAttestationGrid.ItemsSource = Attestations;
        // ListBoxAttestation.ItemsSource = Attestations;
    }
    
    public class AttestationPresenter() : Attestation
    {
        public string DisciplineName
        {
            get
            {
                var ctx = new DatabaseContext();
                return ctx.Disciplines.Where(d => d.Id == IdDiscipline).FirstOrDefault().Name;
            }
        }

        public string TeacherName
        {
            get
            {
                var ctx = new DatabaseContext();
                return ctx.Teachers.Include(t => t.DisciplineTeachers).FirstOrDefault(t => t.DisciplineTeachers.Any(dt => dt.IdDiscipline == IdDiscipline))?.Fio ?? "empty";
            }
        }
    }
    
    public void DisplayServices()
    {
        var ctx = new DatabaseContext();
        var temp = dataSourceAttestations;
        Attestations.Clear();

        if (DisciplineComboBox.SelectionBoxItem != null)
        {
            var idDiscipline = ctx.Disciplines.Where(d => d.Name == DisciplineComboBox.SelectionBoxItem)
                .Select(d => d.Id).FirstOrDefault();
            temp = temp.Where(u => u.IdDiscipline == idDiscipline && u.IdUser == student.Id).ToList();
        }
        
        foreach (var item in temp)
        {
            Attestations.Add(item);
        }
    }
    
    private void DisciplineCombobox_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        // DisplayServices();
    }

    private void ButtonBack_OnClick(object? sender, RoutedEventArgs e)
    {
        Close();
    }

    private void CheckAttest_OnClick(object? sender, RoutedEventArgs e)
    {
        DisplayServices();
    }
}