Imports System.ComponentModel
Imports System.Xml
Imports System.Text

Public Class DlgOperacionesXml
  Private Function ObtenerVista() As ICollectionView
    Dim proveedor As DataSourceProvider = DirectCast(Me.FindResource("xmlAlumnos"), DataSourceProvider)
    Return CollectionViewSource.GetDefaultView(proveedor.Data)
  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 XmlElement
    Dim alumnoActual As XmlElement = DirectCast(vista.CurrentItem, XmlElement)
    Dim list As XmlNodeList = alumnoActual.GetElementsByTagName("asignaturas")
    For Each asigs As XmlElement In list
      If asigs.GetAttribute("tipo") = tipo Then
        list = asigs.GetElementsByTagName("asignatura")
        Dim s As New StringBuilder()
        For Each asig As XmlElement In list
          s.Append(asig.FirstChild.InnerText + vbLf)
        Next
        MessageBox.Show(s.ToString())
      End If
    Next
  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("beca", ListSortDirection.Descending))
        vista.SortDescriptions.Add(New SortDescription("nombre", 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, XmlElement).ChildNodes(4).InnerText = "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 beca
        vista.GroupDescriptions.Add(New PropertyGroupDescription("beca"))
      End If
    Else
      vista.GroupDescriptions.Clear()
    End If
  End Sub
End Class
