Imports System.Collections.ObjectModel
Imports System.ComponentModel

Public Class bbdd
  Implements INotifyPropertyChanged
  Public Sub New()
    ObtenerAlumnos()
  End Sub

  Private _alumnos As ListAlumnos
  Public Property Alumnos() As ListAlumnos
    Get
      Return _alumnos
    End Get
    Set(value As ListAlumnos)
      _alumnos = value
      OnPropertyChanged(New PropertyChangedEventArgs("Alumnos"))
    End Set
  End Property

  Public Event PropertyChanged As PropertyChangedEventHandler _
    Implements INotifyPropertyChanged.PropertyChanged
  Public Sub OnPropertyChanged(e As PropertyChangedEventArgs)
    RaiseEvent PropertyChanged(Me, e) ' generar evento
  End Sub

  ' Crear la base de datos
  Private Function ObtenerAlumnos() As ListAlumnos
    _alumnos = New ListAlumnos()
    Dim coAsignaturas As ObservableCollection(Of Asignaturas)
    Dim asignaturas As ObservableCollection(Of Asignatura)
    Dim rnd As New Random()
    For i As Integer = 1 To 9
      Dim idAl As Integer = i + i * 10 + i * 100 ' 111, 222,...
      Dim nomAl As String = "Alumno 0" & i
      Dim dirAl As String = "Dirección 0" & i
      Dim estAl As String = "Estudios 0" & i
      Dim becaAl As Boolean = If(i Mod 3 = 0, True, False)
      coAsignaturas = New ObservableCollection(Of Asignaturas)()
      ' Obligatorias
      asignaturas = New ObservableCollection(Of Asignatura)()
      For a As Integer = 1 To rnd.[Next](3, 6) - 1
        Dim idAsOb As Integer = (i - 1 + a) Mod 10 + 10 ' 11, 12, 1?
        Dim asig As String = "Asignatura ob" & idAsOb ' Asignatura ob?,...
        Dim nota As Single = CSng(CInt(rnd.NextDouble() * 100) / 10.0F)
        asignaturas.Add(New Asignatura(idAsOb, asig, nota))
      Next
      coAsignaturas.Add(New Asignaturas("Obligatorias", asignaturas))
      ' Optativas
      asignaturas = New ObservableCollection(Of Asignatura)()
      For a As Integer = 1 To rnd.[Next](2, 4) - 1
        Dim idAsOp As Integer = (i - 1 + a) Mod 10 + 60 ' 61, 62, 6?
        Dim asig As String = "Asignatura op" & idAsOp ' Asignatura op?,...
        Dim nota As Single = CSng(CInt(rnd.NextDouble() * 100) / 10.0F)
        asignaturas.Add(New Asignatura(idAsOp, asig, nota))
      Next
      coAsignaturas.Add(New Asignaturas("Optativas", asignaturas))
      _alumnos.Add(New Alumno(idAl, nomAl, dirAl, estAl, becaAl, coAsignaturas))
    Next
    Return _alumnos
  End Function

  Public Function ObtenerAlumnoPorId(idAl As Integer) As Alumno
    For Each al As Alumno In Alumnos
      If al.IdAlumno = idAl Then
        Return al
      End If
    Next
    Return Nothing
  End Function
End Class
