Imports BDNotasAlumnos

Public Class ModificarNota
  Private contextoDeObjs As New bd_notasAlumnosEntities()

  Private m_nota As Single = 0.0F

  Public ReadOnly Property Nota() As Single
    Get
      Return m_nota
    End Get
  End Property

  Public ReadOnly Property IdAlumno() As Integer
    Get
      Return CInt(listaDesAlums.SelectedValue)
    End Get
  End Property

  Public ReadOnly Property IdAsignatura() As Integer
    Get
      Return CInt(listaDesAsigs.SelectedValue)
    End Get
  End Property

  Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    ' Consulta: alumnos
    Dim alums =
      From alum In contextoDeObjs.alumnos
      Select alum
    ' Configurar la lista fija de alumnos
    listaDesAlums.DisplayMemberPath = "nombre"
    listaDesAlums.SelectedValuePath = "id_alumno"
    listaDesAlums.SelectedIndex = 0
    listaDesAlums.ItemsSource = alums

    ' Configurar la lista fija de asignaturas
    listaDesAsigs.DisplayMemberPath = "nombre"
    listaDesAsigs.SelectedValuePath = "id_asignatura"
    listaDesAsigs.SelectedIndex = 0
  End Sub

  Private Sub listaDesAlums_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
    ' Consulta: asignaturas del alumno seleccionado
    Dim id_al As Integer = Convert.ToInt32(listaDesAlums.SelectedValue)
    Dim alumAsigs =
      From al_as In contextoDeObjs.alums_asigs
      Where al_as.id_alumno = id_al
      Select al_as.asignatura()
    listaDesAsigs.ItemsSource = alumAsigs
  End Sub

  Private Sub listaDesAsigs_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
    ' Nota de la asignatura y del alumno seleccionados
    If TryCast(sender, ComboBox).SelectedIndex = -1 Then
      TryCast(sender, ComboBox).SelectedIndex = 0
    End If
    Dim id_al As Integer = Convert.ToInt32(listaDesAlums.SelectedValue)
    Dim id_as As Integer = Convert.ToInt32(listaDesAsigs.SelectedValue)
    Dim consulta =
      From al_as In contextoDeObjs.alums_asigs
      Where al_as.id_alumno = id_al AndAlso al_as.id_asignatura = id_as
      Select al_as.nota
    ctNota.Text = consulta.First().ToString()
  End Sub

  Private Sub btAceptar_Click(sender As Object, e As RoutedEventArgs)
    If Not NotaNoValida(ctNota.Text) Then
      MessageBox.Show("Nota no válida")
      Return
    End If
    DialogResult = True
  End Sub

  Private Function NotaNoValida(snota As String) As Boolean
    If Not Single.TryParse(snota, m_nota) Then Return False
    If m_nota < 0 OrElse m_nota > 10 Then Return False
    Return True
  End Function
End Class
