Partial Public Class winViewProfiles
    Dim arrlstProfiles As ArrayList


    Private Sub winProfiles_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        'when form is loaded set the bindings and set a reference to the current form
        Me.setListViewBindings()
        modGlobal.setStartupForm(Me)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdd.Click
        'if the add perfile form is not created then create it. Show the form, and put the form in focus
        If modGlobal.cApplicationWindows.winAddPersonalProfile Is Nothing Then
            modGlobal.cApplicationWindows.winAddPersonalProfile = New winAddPersonalInfomation
        End If

        modGlobal.cApplicationWindows.winAddPersonalProfile.Show()
        modGlobal.cApplicationWindows.winAddPersonalProfile.Focus()

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
        If lstvProfiles.SelectedIndex >= 0 Then
            Dim deletion As MessageBoxResult = MessageBox.Show("Do you wanna delete this profile?", "Deleting", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.OK)

            If deletion = MessageBoxResult.OK Then
                arrlstProfiles.RemoveAt(lstvProfiles.SelectedIndex)
                'if file exists delete previous file, serialize the arraylist and then refresh the data source
                If IO.File.Exists(modGlobal.strXmlFilePath) Then
                    IO.File.Delete(modGlobal.strXmlFilePath)
                    modSerialization.serializeClass(arrlstProfiles, modGlobal.strXmlFilePath)
                    Dim winDelete1 As New winDelete
                    winDelete1.Show()
                    System.Threading.Thread.Sleep(400)
                    winDelete1.Close()
                    refreshDataSource()
                    'else serialize to disk and refresh data source.
                Else
                    modSerialization.serializeClass(arrlstProfiles, modGlobal.strXmlFilePath)
                    Dim winDelete1 As New winDelete
                    winDelete1.Show()
                    System.Threading.Thread.Sleep(400)
                    winDelete1.Close()
                    refreshDataSource()
                End If

            End If
        
        End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnExit.Click

        If Not modGlobal.cApplicationWindows.winAddPersonalProfile Is Nothing Then
            modGlobal.cApplicationWindows.winAddPersonalProfile.Close()
            Me.Close()
        Else
            Me.Close()
        End If

    End Sub

    Private Sub winViewProfiles_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        If Not modGlobal.cApplicationWindows.winAddPersonalProfile Is Nothing Then
            modGlobal.cApplicationWindows.winAddPersonalProfile.Close()
        End If
    End Sub

    'bind the data source to listview
    Public Sub setListViewBindings()
        arrlstProfiles = modSerialization.deSerializeClass(modGlobal.strXmlFilePath)
        lstvProfiles.DataContext = arrlstProfiles
    End Sub

    'refresh the data binding
    Public Sub refreshDataSource()
        CollectionViewSource.GetDefaultView(arrlstProfiles).Refresh()
    End Sub

    

End Class
