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

Public Class DlgInsertarBorrar
  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
    End If
    ' 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

  Private Sub btBorrar_Click(sender As Object, e As RoutedEventArgs)
    ' Recuperar el recurso objBBDD de Application
    Dim objBBDD As bbdd = TryCast(Me.FindResource("objBBDD"), bbdd)
    objBBDD.Alumnos.Remove(TryCast(listaAlumnos.SelectedItem, Alumno))
  End Sub

  Private Sub btAñadir_Click(sender As Object, e As RoutedEventArgs)
    Dim nuevo As Alumno = CrearNuevoAlumno()
    Dim dlg As New DlgAñadir(nuevo)
    'DlgBindingGroup dlg = new DlgBindingGroup(nuevo);
    If dlg.ShowDialog() = True Then
      ' Verificar si el IdAlumno existe en la colección
      Dim objBBDD As bbdd = TryCast(Me.FindResource("objBBDD"), bbdd)
      Dim id As Integer = Integer.Parse(dlg.ctIdAlumno.Text)
      If objBBDD.ObtenerAlumnoPorId(id) IsNot Nothing Then
        MessageBox.Show(String.Format("El Id {0} ya existe", id))
        Return
      End If
      ' Añadir las asignaturas seleccionadas
      AñadirAsigs(nuevo, dlg)
      ' Añadir el nuevo alumno a la colección
      objBBDD.Alumnos.Add(nuevo)
    End If
  End Sub

  Private Sub AñadirAsigs(nuevo As Alumno, dlg As DlgAñadir)
    ' Añadir las asignaturas seleccionadas en las listas
    ' a las colecciones correspondientes
    Dim id As Integer
    Dim nombre As String
    For Each s As String In dlg.lsListaObs.SelectedItems
      id = Integer.Parse(s.Substring(0, 2))
      nombre = s.Substring(3)
      nuevo.ListaAsigsObs.Add(New Asignatura(id, nombre, -1))
    Next
    For Each s As String In dlg.lsListaOps.SelectedItems
      id = Integer.Parse(s.Substring(0, 2))
      nombre = s.Substring(3)
      nuevo.ListaAsigsOps.Add(New Asignatura(id, nombre, -1))
    Next
  End Sub

  Private Function CrearNuevoAlumno() As Alumno
    ' Construir la colección de asignaturas
    Dim listaAsigs As New ObservableCollection(Of Asignaturas)()
    listaAsigs.Add(New Asignaturas("Obligatorias", New ObservableCollection(Of Asignatura)()))
    listaAsigs.Add(New Asignaturas("Optativas", New ObservableCollection(Of Asignatura)()))
    ' Construir el objeto Alumno
    Dim nuevo As New Alumno()
    nuevo.ListaCoAsignaturas = listaAsigs
    Return nuevo
  End Function
End Class
