Imports System
Imports System.ComponentModel
Imports System.IO
Imports System.Windows
Imports System.Windows.Controls

Partial Public Class EditAttachmentForm
    Inherits UserControl
    Implements INotifyPropertyChanged
    ''' <summary>
    ''' Initializes a new instance of the <see cref="EditAttachmentForm"/> class.
    ''' </summary>
    Public Sub New()
        InitializeComponent()
        DataContext = Me
    End Sub

#Region "Properties"

    Private _fileName As String

    ''' <summary>
    ''' Gets or sets the name of the file.
    ''' </summary>
    ''' <value>
    ''' The name of the file.
    ''' </value>
    Public Property FileName() As String
        Get
            Return _fileName
        End Get
        Set(ByVal value As String)
            _fileName = value
            OnPropertyChanged("FileName")
        End Set
    End Property

    Private _description As String

    ''' <summary>
    ''' Gets or sets the description.
    ''' </summary>
    ''' <value>
    ''' The description.
    ''' </value>
    Public Property Description() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
            OnPropertyChanged("Description")
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a value indicating whether [add attachment].
    ''' </summary>
    ''' <value>
    '''   <c>true</c> if [add attachment]; otherwise, <c>false</c>.
    ''' </value>
    Public Property AddAttachment() As Boolean
        Get
            Return m_AddAttachment
        End Get
        Set(ByVal value As Boolean)
            m_AddAttachment = value
        End Set
    End Property
    Private m_AddAttachment As Boolean

#End Region

    ''' <summary>
    ''' Handles the Click event of the Browse control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub Browse_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim browseFile As New OpenFileDialog()
        browseFile.Multiselect = False
        browseFile.Filter = "Any Files (*.*)|*.*"

        Dim result As System.Nullable(Of Boolean) = browseFile.ShowDialog()
        Dim boolResult As Boolean

        If Boolean.TryParse(result, boolResult) Then
            FileStream = browseFile.File.OpenRead()
            FileName = browseFile.File.Name
        End If
    End Sub

    ''' <summary>
    ''' Gets or sets the file stream.
    ''' </summary>
    ''' <value>
    ''' The file stream.
    ''' </value>
    Public Property FileStream() As FileStream
        Get
            Return m_FileStream
        End Get
        Set(ByVal value As FileStream)
            m_FileStream = value
        End Set
    End Property
    Private m_FileStream As FileStream

    ''' <summary>
    ''' Handles the Click event of the Ok control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub Ok_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        AddAttachment = True
        RaiseClosed()
    End Sub

    ''' <summary>
    ''' Handles the Click event of the Cancel control.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    Private Sub Cancel_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        AddAttachment = False
        RaiseClosed()
    End Sub

#Region "INotifyPropertyChanged Members"

    ''' <summary>
    ''' Occurs when a property value changes.
    ''' </summary>
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    ''' <summary>
    ''' Called when [property changed].
    ''' </summary>
    ''' <param name="propertyName">Name of the property.</param>
    Private Sub OnPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

#End Region

#Region "Close"

    ''' <summary>
    ''' Occurs when [closed].
    ''' </summary>
    Public Event Closed As EventHandler

    ''' <summary>
    ''' Raises the closed.
    ''' </summary>
    Private Sub RaiseClosed()
        RaiseEvent Closed(Me, New EventArgs())
    End Sub

#End Region
End Class