Imports ColeccionDeDatos
Imports System.Windows.Media.Imaging
Imports System.Windows.Data
Imports System.Windows.Navigation
Imports System.Collections.Specialized

Partial Public Class PagMostrarPersona
  Inherits Page
  Private fuenteDeDatos As PagedCollectionView = Nothing
  Private ventanaHija As VenSecAgregarTfno = Nothing

  Public Sub New()
    InitializeComponent()
    fuenteDeDatos = New PagedCollectionView(TryCast(Application.Current, App).ColTelefonos)
    LayoutRoot.DataContext = fuenteDeDatos
  End Sub

  ' Se ejecuta cuando el usuario navega a esta página.
  Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
    ' Manejar el evento CurrentChanged de la vista
    AddHandler fuenteDeDatos.CurrentChanged, AddressOf fuenteDeDatos_CurrentChanged
    ' Manejar el evento CollectionChanged de la vista
    AddHandler fuenteDeDatos.CollectionChanged, AddressOf fuenteDeDatos_CollectionChanged
    ' Actualizar posición actual y total elementos
    ActualizarPosicionReg()
  End Sub

  Public Sub fuenteDeDatos_CurrentChanged(sender As Object, e As EventArgs)
    ActualizarPosicionReg()
  End Sub

  Private Sub ActualizarPosicionReg()
    If fuenteDeDatos.CurrentItem Is Nothing Then Return
    ' Actualizar posición actual y total registros
    Dim posicionActual As String = Convert.ToString(fuenteDeDatos.CurrentPosition + 1)
    btxPosicionReg.Text = posicionActual
    btxTotalReg.Text = fuenteDeDatos.Count.ToString()

    ' Poner una foto ficticia si este campo es Nothing
    If TryCast(fuenteDeDatos.CurrentItem, Telefono).Foto IsNot Nothing Then Return
    Dim wbm As WriteableBitmap = New WriteableBitmap(0, 0).FromResource("Assets/foto.jpg")
    imFoto.Source = wbm
  End Sub

  Private Sub btPrimero_Click(sender As Object, e As RoutedEventArgs)
    fuenteDeDatos.MoveCurrentToFirst()
  End Sub

  Private Sub btAnterior_Click(sender As Object, e As RoutedEventArgs)
    fuenteDeDatos.MoveCurrentToPrevious()
    If fuenteDeDatos.IsCurrentBeforeFirst Then
      fuenteDeDatos.MoveCurrentToFirst()
    End If
  End Sub

  Private Sub btSiguiente_Click(sender As Object, e As RoutedEventArgs)
    fuenteDeDatos.MoveCurrentToNext()
    If fuenteDeDatos.IsCurrentAfterLast Then
      fuenteDeDatos.MoveCurrentToLast()
    End If
  End Sub

  Private Sub btUltimo_Click(sender As Object, e As RoutedEventArgs)
    fuenteDeDatos.MoveCurrentToLast()
  End Sub

  Private Sub ctFiltrar_TextChanged(sender As Object, e As TextChangedEventArgs)
    Dim ct As TextBox = TryCast(sender, TextBox)
    If ct Is Nothing Then Return
    If fuenteDeDatos.CanFilter Then
      fuenteDeDatos.Filter = Function(elemento As Object) _
        DirectCast(elemento, Telefono).NumTelefono.Contains(ct.Text)
    End If
  End Sub

  Public Sub fuenteDeDatos_CollectionChanged(sender As Object, e As NotifyCollectionChangedEventArgs)
    ActualizarPosicionReg()
  End Sub

  Private Sub imFoto_MouseLeftButtonDown(sender As Object, e As System.Windows.Input.MouseButtonEventArgs)
    Dim dlg As New OpenFileDialog()
    dlg.Filter = "ficheros jpg (*.jpg)|*.jpg|" &
                 "ficheros jpeg (*.jpeg)|*.jpeg|" &
                 "ficheros png (*.png)|*.png|" &
                 "Todos (*.*)|*.*"

    If dlg.ShowDialog() = True Then
      If dlg.File.Name Is Nothing OrElse dlg.File.Name.Length = 0 Then
        Return
      End If
      Dim imagen As New WriteableBitmap(0, 0)
      imagen.SetSource(dlg.File.OpenRead())
      imFoto.Source = imagen
    End If
  End Sub

  Private Sub btAñadir_Click(sender As Object, e As RoutedEventArgs)
    ventanaHija = New VenSecAgregarTfno()
    AddHandler ventanaHija.Closed, AddressOf ventanaHija_Closed
    ventanaHija.Show()
  End Sub

  Private Sub ventanaHija_Closed(sender As Object, e As EventArgs)
    If ventanaHija.DialogResult = True Then
      If ventanaHija.NuevoObjTfno IsNot Nothing Then
        TryCast(fuenteDeDatos.SourceCollection, List(Of Telefono)).Add(ventanaHija.NuevoObjTfno)
        ' Añadir el objeto a la base de datos
        ' ...
        fuenteDeDatos.MoveCurrentToLast()
      End If
    End If
  End Sub

  Private Sub btBorrar_Click(sender As Object, e As RoutedEventArgs)
    ' Quitar de la colección el elemento mostrado
    Dim tfno As Telefono = DirectCast(fuenteDeDatos.CurrentItem, Telefono)
    If tfno IsNot Nothing Then
      Dim siguientePosActual As Integer = (fuenteDeDatos.CurrentPosition - 1)
      TryCast(fuenteDeDatos.SourceCollection, List(Of Telefono)).Remove(tfno)
      fuenteDeDatos.Refresh()
      If siguientePosActual > -1 Then
        fuenteDeDatos.MoveCurrentToPosition(siguientePosActual)
        ' Borrar el objeto de la base de datos
        ' ...
      End If
    End If
  End Sub

  Private Sub btGuardar_Click(sender As Object, e As RoutedEventArgs)
    ' Guardar modificaciones en la base de datos
    ' ...
  End Sub
End Class
