Public Class Phonebook

    Public username As String
    Public password As String

    Dim groupid_list As New ArrayList
    Dim groupname_list As New ArrayList

    Dim contactid_list As New ArrayList
    Dim contactname_list As New ArrayList
    Dim contactno_list As New ArrayList

    Dim webClient As New System.Net.WebClient
    Dim result As String

    Dim selected_groupid As Object
    Dim selected_contactname As Object

    Public groupname As String

    Private Sub Phonebook_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        username = username.ToString.Replace(" ", "%20")
        password = password.ToString.Replace(" ", "%20")

        DataGridView1.ColumnCount = 2
        DataGridView1.Columns(0).Name = "Group ID"
        DataGridView1.Columns(1).Name = "Group Name"
        setGroupTable("http://www.isms.com.my/api_list_of_contact_group.php?un=" + username + "&pwd=" + password + "")


        DataGridView2.ColumnCount = 3
        DataGridView2.Columns(0).Name = "Contact Name"
        DataGridView2.Columns(1).Name = "Contact No."
        DataGridView2.Columns(2).Name = "Contact ID"
        setContactTable("http://www.isms.com.my/api_list_of_contact.php?un=" + username + "&pwd=" + password + "")

        search_cmb.SelectedIndex = 0

    End Sub

    Private Sub set_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles set_btn.Click

        Demo.groupid_phonebook = selected_groupid.ToString
        Demo.contactname_phonebook = selected_contactname.ToString
        Me.Close()

    End Sub

    Private Sub cancel_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel_btn.Click

        selected_groupid = ""
        selected_contactname = ""
        Me.Close()

    End Sub

    Private Sub setGroupTable(ByVal url As String)

        groupid_list.Clear()
        groupname_list.Clear()
        DataGridView1.Rows.Clear()

        result = webClient.DownloadString(url)
        response_area.AppendText("----- Command Breaker -----" + vbLf)
        response_area.AppendText(url + vbLf)
        response_area.AppendText(result + vbLf)
        response_area.SelectionStart = response_area.Text.Length
        response_area.ScrollToCaret()

        Dim result_index As Integer = -1
        Dim new_result As String = result

        For i As Integer = 0 To 1 Step 0

            result_index = new_result.IndexOf("|v|")

            If (result_index > -1) Then

                groupid_list.Add(new_result.Substring(0, result_index))

                new_result = new_result.Remove(0, result_index + 3)

                result_index = new_result.IndexOf("||")

                If (result_index > -1) Then

                    groupname_list.Add(new_result.Substring(0, result_index))

                    new_result = new_result.Remove(0, result_index + 2)
                    result_index = -1

                End If

            Else

                Exit For

            End If

        Next

        search_cmb.Items.Clear()
        search_cmb.Items.Add("Show all")

        Dim content As String
        Dim cntr As Integer = 0

        For Each content In groupid_list

            Dim row As String() = New String() {groupid_list(cntr), groupname_list(cntr)}
            DataGridView1.Rows.Add(row)

            search_cmb.Items.Add(content)

            cntr = cntr + 1

        Next

        DataGridView1.ClearSelection()

    End Sub

    Private Sub setContactTable(ByVal url As String)

        contactid_list.Clear()
        contactname_list.Clear()
        contactno_list.Clear()

        DataGridView2.Rows.Clear()

        result = webClient.DownloadString(url)

        response_area.AppendText("----- Command Breaker -----" + vbLf)
        response_area.AppendText(url + vbLf)
        response_area.AppendText(result + vbLf)
        response_area.SelectionStart = response_area.Text.Length
        response_area.ScrollToCaret()

        Dim result_index As Integer = -1
        Dim new_result As String = result

        For i As Integer = 0 To 1 Step 0

            result_index = new_result.IndexOf("|v|")

            If (result_index > -1) Then

                contactid_list.Add(new_result.Substring(0, result_index))

                new_result = new_result.Remove(0, result_index + 3)

                result_index = new_result.IndexOf("|v|")

                If (result_index > -1) Then

                    contactname_list.Add(new_result.Substring(0, result_index))

                    new_result = new_result.Remove(0, result_index + 3)

                    result_index = new_result.IndexOf("||")

                    If (result_index > -1) Then

                        contactno_list.Add(new_result.Substring(0, result_index))

                        new_result = new_result.Remove(0, result_index + 2)
                        result_index = -1

                    End If

                Else

                    Exit For

                End If

            Else

                Exit For

            End If

        Next

        Dim content As String
        Dim cntr As Integer = 0

        For Each content In contactname_list

            Dim row As String() = New String() {contactid_list(cntr), contactname_list(cntr), contactno_list(cntr)}
            DataGridView2.Rows.Add(row)

            cntr = cntr + 1

        Next

        DataGridView2.ClearSelection()

    End Sub

    Private Sub search_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search_btn.Click

        If search_cmb.SelectedItem.ToString() = "Show all" Then

            setContactTable("http://www.isms.com.my/api_list_of_contact.php?un=" + username + "&pwd=" + password + "")

        Else

            setContactTable("http://www.isms.com.my/api_list_of_contact.php?un=" + username + "&pwd=" + password + "&groupid=" + search_cmb.SelectedItem.ToString() + "")

        End If

    End Sub

    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick

        selected_contactname = DataGridView2.Rows(e.RowIndex).Cells(0).Value

        selected_contactname = selected_contactname.ToString

    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        selected_groupid = DataGridView1.Rows(e.RowIndex).Cells(0).Value

        selected_groupid = selected_groupid.ToString

    End Sub

    Private Sub removegroup_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removegroup_btn.Click

        If selected_groupid <> "" Then

            result = webClient.DownloadString("http://www.isms.com.my/api_delete_contact_group.php?un=" + username + "&pwd=" + password + "&groupid=" + selected_groupid.ToString + "")

            response_area.AppendText("----- Command Breaker -----" + vbLf)
            response_area.AppendText("http://www.isms.com.my/api_delete_contact_group.php?un=" + username + "&pwd=" + password + "&groupid=" + selected_groupid.ToString + "" + vbLf)
            response_area.AppendText(result + vbLf)

            setGroupTable("http://www.isms.com.my/api_list_of_contact_group.php?un=" + username + "&pwd=" + password + "")

        Else

            MsgBox("Please select a group from the table.")

        End If

    End Sub

    Private Sub updategroup_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updategroup_btn.Click

        If selected_groupid = "" Then

            MsgBox("Please select a group from the table.")

        Else

            GroupEditor.ShowDialog()

            result = webClient.DownloadString("http://www.isms.com.my/api_edit_contact_group.php?un=" + username + "&pwd=" + password + "&name=" + groupname + "&groupid=" + selected_groupid + "")

            response_area.AppendText("----- Command Breaker -----" + vbLf)
            response_area.AppendText("http://www.isms.com.my/api_edit_contact_group.php?un=" + username + "&pwd=" + password + "&name=" + groupname + "&groupid=" + selected_groupid + "" + vbLf)
            response_area.AppendText(result + vbLf)

            setGroupTable("http://www.isms.com.my/api_list_of_contact_group.php?un=" + username + "&pwd=" + password + "")

        End If

    End Sub

    Private Sub newgroup_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newgroup_btn.Click

        GroupEditor.ShowDialog()

        result = webClient.DownloadString("http://www.isms.com.my/api_add_contact_group.php?un=" + username + "&pwd=" + password + "&name=" + groupname + "")

        response_area.AppendText("----- Command Breaker -----" + vbLf)
        response_area.AppendText("http://www.isms.com.my/api_add_contact_group.php?un=" + username + "&pwd=" + password + "&name=" + groupname + "" + vbLf)
        response_area.AppendText(result + vbLf)

        setGroupTable("http://www.isms.com.my/api_list_of_contact_group.php?un=" + username + "&pwd=" + password + "")

    End Sub
End Class