Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports BaseDeDatos
Imports System.Text

Public Class DlgOperacionesObjs
  Private Function ObtenerVista() As ICollectionView
    Return CollectionViewSource.GetDefaultView(listaAlumnos.ItemsSource)
  End Function

  Private Sub btMostrarOb_Click(sender As Object, e As RoutedEventArgs)
    MostrarAsigs("Obligatorias")
  End Sub

  Private Sub btMostrarOp_Click(sender As Object, e As RoutedEventArgs)
    MostrarAsigs("Optativas")
  End Sub

  Private Sub MostrarAsigs(tipo As String)
    Dim vista As ICollectionView = ObtenerVista()
    If vista.IsEmpty Then Return
    ' Cada "alumno" es un objeto de la clase Alumno
    Dim alumnoActual As Alumno = DirectCast(vista.CurrentItem, Alumno)
    Dim list As ObservableCollection(Of Asignatura) = Nothing
    If tipo = "Obligatorias" Then
      list = alumnoActual.ListaAsigsObs
    ElseIf tipo = "Optativas" Then
      list = alumnoActual.ListaAsigsOps
    Else
      Return
    End If
    Dim s As New StringBuilder()
    For Each asig As Asignatura In list
      s.Append(asig.NomAsignatura + vbLf)
    Next
    MessageBox.Show(s.ToString())
  End Sub

  Private Sub btSiguiente_Click(sender As Object, e As RoutedEventArgs)
    Dim vista As ICollectionView = ObtenerVista()
    vista.MoveCurrentToNext()
    If vista.IsCurrentAfterLast Then
      vista.MoveCurrentToLast()
    End If
    listaAlumnos.ScrollIntoView(vista.CurrentItem)
  End Sub

  Private Sub btAnterior_Click(sender As Object, e As RoutedEventArgs)
    Dim vista As ICollectionView = ObtenerVista()
    vista.MoveCurrentToPrevious()
    If vista.IsCurrentBeforeFirst Then
      vista.MoveCurrentToFirst()
    End If
    listaAlumnos.ScrollIntoView(vista.CurrentItem)
  End Sub

  Private Sub cvOrdenar_Click(sender As Object, e As RoutedEventArgs)
    Dim vista As ICollectionView = ObtenerVista()
    If vista.CanSort AndAlso cvOrdenar.IsChecked = True Then
      If vista.SortDescriptions.Count = 0 Then
        vista.SortDescriptions.Add(New SortDescription("BecaAlumno", ListSortDirection.Descending))
        vista.SortDescriptions.Add(New SortDescription("NombAlumno", ListSortDirection.Ascending))
      End If
    Else
      vista.SortDescriptions.Clear()
    End If
  End Sub

  Private Sub cvFiltrar_Click(sender As Object, e As RoutedEventArgs)
    Dim vista As ICollectionView = ObtenerVista()
    If vista.CanFilter AndAlso cvFiltrar.IsChecked = True Then
      If vista.Filter Is Nothing Then
        vista.Filter = Function(elemento As Object) DirectCast(elemento, Alumno).BecaAlumno = True
      End If
    Else
      vista.Filter = Nothing
    End If
  End Sub

  Private Sub cvAgrupar_Click(sender As Object, e As RoutedEventArgs)
    Dim vista As ICollectionView = ObtenerVista()
    If vista.CanGroup AndAlso cvAgrupar.IsChecked = True Then
      If vista.GroupDescriptions.Count = 0 Then
        ' Agrupar por BecaAlumno
        vista.GroupDescriptions.Add(New PropertyGroupDescription("BecaAlumno"))
      End If
    Else
      vista.GroupDescriptions.Clear()
    End If
  End Sub
End Class
