Imports System.Windows.Media.Imaging
Imports ColeccionDeDatos

Partial Public Class VenSecAgregarTfno
  Inherits ChildWindow
  Private m_NuevoObjTfno As Telefono
  Public Property NuevoObjTfno() As Telefono
    Get
      Return m_NuevoObjTfno
    End Get
    Private Set(value As Telefono)
      m_NuevoObjTfno = value
    End Set
  End Property

  Public Sub New()
    InitializeComponent()
    Dim id As Integer =
      TryCast(Application.Current, App).ColTelefonos.Count + 1
    NuevoObjTfno = New Telefono(id)
    Me.DataContext = NuevoObjTfno
  End Sub

  Private Sub ChildWindow_Loaded(sender As Object, e As RoutedEventArgs)
    NuevoObjTfno.Nombre = "Nombre"
    NuevoObjTfno.Direccion = "Dirección"
    NuevoObjTfno.NumTelefono = "000000000"
    NuevoObjTfno.Observaciones = "Observaciones"
    Dim wbm As WriteableBitmap =
      New WriteableBitmap(0, 0).FromResource("Assets/foto.jpg")
    imFoto.Source = wbm
  End Sub

  Private Sub OKButton_Click(sender As Object, e As RoutedEventArgs)
    Me.DialogResult = True
  End Sub

  Private Sub CancelButton_Click(sender As Object, e As RoutedEventArgs)
    Me.DialogResult = False
  End Sub

  Private Sub imFoto_MouseLeftButtonDown(sender As Object, e As 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
End Class
