Imports System.IO

Public Class SimpleDB

    Private fSimpleDatabase As SimpleDatabase
    Public Sub New()
        fSimpleDatabase = New SimpleDatabase
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        updateView()
    End Sub

    Public Sub updateView()
        itemComboBox.Items.Clear()
        definitionTextBox.Clear()
        For Each key As String In fSimpleDatabase.WordList
            itemComboBox.Items.Add(key)
        Next
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        AboutSimpleDB.ShowDialog()
    End Sub

    Private Sub itemComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemComboBox.SelectedIndexChanged
        'The description of the selected word is displayed.
        definitionTextBox.Text = _
      fSimpleDatabase.Description(itemComboBox.SelectedItem.ToString())
    End Sub

    Private Sub ContentsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContentsToolStripMenuItem.Click
        ' This event handler displays a dialog box with the instructions
        ' for playing the game.
        Dim anInstructDialog As SimpleDB_Instructions
        anInstructDialog = New SimpleDB_Instructions
        anInstructDialog.ShowDialog()
        anInstructDialog.Dispose()
    End Sub

    
    

    Private Sub saveMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveMenuItem.Click
        'If the file name is empty, the event handler for saveAsMenuItem is used.
        'Otherwise the glossary is saved and the view is updated. If an exception is 
        'caught, the file name is set to "" and an error message is displayed.
        'Add your code here
        If fSimpleDatabase.FileName = "" Then
            saveAsMenuItem_Click(sender, e)
        Else
            Try
                fSimpleDatabase.save()
                updateView()
            Catch ex As ArgumentException
                fSimpleDatabase.FileName = ""
                MessageBox.Show(ex.Message)
            Catch ex As IOException
                fSimpleDatabase.FileName = ""
                MessageBox.Show(ex.Message)
            Catch ex As UnauthorizedAccessException
                fSimpleDatabase.FileName = ""
                MessageBox.Show(ex.Message)
            Catch ex As Exception
                fSimpleDatabase.FileName = ""
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Private Sub saveAsMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveAsMenuItem.Click
        If (aSaveFileDialog.ShowDialog() = DialogResult.OK) Then
            fSimpleDatabase.FileName = aSaveFileDialog.FileName
            saveMenuItem_Click(sender, e) 'Reuse code to save the file
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Close()
    End Sub

    Private Sub openMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openMenuItem.Click
        If fSimpleDatabase.Modified Then
            If MessageBox.Show("Database has been modified, save?", "SimpleDatabase", _
                  MessageBoxButtons.YesNo) = DialogResult.Yes Then
                saveMenuItem_Click(sender, e)  'Reuse code to save the file
            End If
        End If
        If (anOpenFileDialog.ShowDialog() = DialogResult.OK) Then
            Try
                fSimpleDatabase.FileName = anOpenFileDialog.FileName
                fSimpleDatabase.load()
            Catch ex As ArgumentException
                fSimpleDatabase.FileName = ""
                MessageBox.Show(ex.Message)
            Catch ex As IOException
                fSimpleDatabase.FileName = ""
                MessageBox.Show(ex.Message)
            Catch ex As UnauthorizedAccessException
                fSimpleDatabase.FileName = ""
                MessageBox.Show(ex.Message)
            Catch ex As Exception
                fSimpleDatabase.FileName = ""
                MessageBox.Show(ex.Message)
            End Try
        End If
        updateView()
    End Sub

    Private Sub AddItemBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddItemBut.Click
        'A dialogue box for a new entry is displayed. If an appropriate entry is
        'made and OK is clicked, the entry is added to the glossary. If the entry
        'already exists, a warning is displayed instead. The GUI is updated.
        Dim aNewEntryDialog As NewEntry
        Dim word As String
        Dim description As String
        aNewEntryDialog = New NewEntry
        If (aNewEntryDialog.ShowDialog() = DialogResult.OK) Then
            word = aNewEntryDialog.itemTextBox.Text
            description = aNewEntryDialog.descriptionTextBox.Text
            If fSimpleDatabase.WordList.Contains(word) Then
                MessageBox.Show("Cannot add new entry - item already contained in database")
            Else
                fSimpleDatabase.add(word, description)
            End If
        End If
        aNewEntryDialog.Dispose()
        updateView()
    End Sub

    
End Class
