Imports System.Linq

Public Class FindSites

    Public Property SearchTerms As List(Of Keyword)
        Set(value As List(Of Keyword))
            lstKeywords.Items.AddRange(value.ToArray())
        End Set
        Get
            Return lstKeywords.Items.Cast(Of Keyword)().ToList()
        End Get
    End Property

#Region "Event Handlers"
    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        Dim input As New Input("Create Search Term", "Enter a Search Term:")

        If input.ShowDialog <> Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If

        Dim keyword As New Keyword With {
            .Keyword = input.Value
        }

        ' Check for duplicate keywords
        Dim keywordCount = lstKeywords.Items.Cast(Of Keyword)().Count(Function(k As Keyword) k.Keyword = keyword.Keyword)
        If keywordCount < 1 AndAlso keyword IsNot Nothing Then
            lstKeywords.Items.Add(keyword)
        End If

    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        Dim removeIndexes As New List(Of Integer)
        For Each item In lstKeywords.SelectedItems
            removeIndexes.Add(lstKeywords.Items.IndexOf(item))
            'lstKeywords.Items.Remove(item)
        Next

        For Each index In removeIndexes
            lstKeywords.Items.RemoveAt(index)
        Next
    End Sub


    Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click

        If lstKeywords.Items.Count = 0 Then
            MsgBox("Please add at least 1 keyword to continue...", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Input Required!")
            Exit Sub
        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnProject_Click(sender As System.Object, e As System.EventArgs) Handles btnProject.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
#End Region


    Private Sub lstKeywords_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstKeywords.SelectedIndexChanged
        Dim keyword = TryCast(lstKeywords.SelectedItem, Keyword)

        If keyword Is Nothing Then
            Exit Sub
        End If

    End Sub
End Class

