Imports System.IO
Imports MySql.Data.MySqlClient
Imports System.ComponentModel

Public Class frm_add_participants
    Private Sub frm_add_participants_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing

        frm_event_control.Show()

    End Sub

    Private Sub frm_add_participants_Load(sender As Object, e As EventArgs) Handles Me.Load

        cbo_participants_type.SelectedIndex = 0

        Try
            SubPopulateListBox(GetPersonList(), lst_person)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub cbo_participants_type_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbo_participants_type.SelectedIndexChanged

        Try
            SubPopulateListBox(FunListParticipants(cbo_participants_type.Text, SelectedEvent.ID, txt_pfilter.Text), lst_participants_list)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub txt_pfilter_KeyDown(sender As Object, e As KeyEventArgs) Handles txt_pfilter.KeyDown

        Try
            SubPopulateListBox(FunListParticipants(cbo_participants_type.Text, SelectedEvent.ID, txt_pfilter.Text), lst_participants_list)
        Catch ex As Exception
            ClearListBox(lst_participants_list)
        End Try

    End Sub

    Private Sub txt_member_search_KeyDown(sender As Object, e As KeyEventArgs) Handles txt_member_search.KeyDown

        Try
            SubPopulateListBox(FunListPerson(txt_member_search.Text), lst_person)
        Catch ex As Exception
            ClearListBox(lst_person)
        End Try

    End Sub

    Private Sub lst_participants_list_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lst_participants_list.SelectedIndexChanged

        Dim person As person

        Try
            person = New person(lst_participants_list.SelectedValue)
        Catch ex As Exception
            person = Nothing
        End Try

        Try
            DisplayPerson(person)
        Catch ex As Exception

        End Try

    End Sub

    '----------------------------------------------------------------------------------
    '   SPECIAL FUNCTIONS
    '----------------------------------------------------------------------------------

    Private Sub ClearComboBox(ComboBox As ComboBox)

        Try
            ComboBox.DataSource = Nothing
            ComboBox.Items.Clear()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub ClearListBox(ListBox As ListBox)

        Try
            ListBox.DataSource = Nothing
            ListBox.Items.Clear()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub DisplayPerson(Person As person)

        Try
            pb_person_picture.Image = Image.FromStream(Person.Picture)
        Catch ex As Exception
            pb_person_picture.Image = Nothing
        End Try

        Try
            pb_club_logo.Image = Image.FromStream(Person.Team.Logo)
        Catch ex As Exception
            pb_club_logo.Image = Nothing
        End Try

        Try
            pb_flag.Image = Image.FromStream(Person.Team.Country.Flag)
        Catch ex As Exception
            pb_flag.Image = Nothing
        End Try

        Try
            lbl_gender.Text = Person.Gender
        Catch ex As Exception
            lbl_gender.Text = Nothing
        End Try

        Try
            lbl_category.Text = Person.Weight.WeightCategory
        Catch ex As Exception
            lbl_category.Text = Nothing
        End Try

        Try
            lbl_weight.Text = Person.Weight.Weight & "Kg"
        Catch ex As Exception
            lbl_weight.Text = Nothing
        End Try

        Try
            lbl_weightclass.Text = Person.Weight.WeightClass
        Catch ex As Exception
            lbl_weightclass.Text = Nothing
        End Try

    End Sub

    Private Function FunListParticipants(Type As String, eventID As Integer, Filter As String) As List(Of dataset)

        Dim QRY As String = ("

            select 
	            distinct
	            person.id, concat(person.fname, if(isnull(person.mname), '', concat(' ', person.mname)), ' ', person.lname) as name 
            from 
	            event_participants
            left join
	            person on person.id = event_participants.pid
            where 
	            concat(fname, if(isnull(mname), '', concat(' ', mname)), ' ', lname) like '%" & Filter & "%'
            and
	            event_participants.eid = " & eventID & "
                and
                event_participants.type = '" & Type & "'
            order by 
	            fname, 
	            mname, 
	            lname 
	            asc
        ")

        Dim CON As New MySqlConnection(CON_STRING)
        Dim COM As New MySqlCommand(QRY, CON)
        Dim RDR As MySqlDataReader
        Dim LST As New List(Of dataset)

        Try
            CON.Open()
            RDR = COM.ExecuteReader
            If RDR.HasRows = True Then
                While RDR.Read
                    LST.Add(New dataset(RDR("id"), RDR("name")))
                End While
            End If
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

        Return LST

    End Function

    Private Function FunListPerson(Filter As String) As List(Of dataset)

        Dim QRY As String = ("

            select 
	            distinct
	            person.id, concat(person.fname, if(isnull(person.mname), '', concat(' ', person.mname)), ' ', person.lname) as name 
            from 
	            person
            where 
	            concat(fname, if(isnull(mname), '', concat(' ', mname)), ' ', lname) like '%" & Filter & "%'
            order by 
	            fname, 
	            mname, 
	            lname 
	            asc
        ")

        Dim CON As New MySqlConnection(CON_STRING)
        Dim COM As New MySqlCommand(QRY, CON)
        Dim RDR As MySqlDataReader
        Dim LST As New List(Of dataset)

        Try
            CON.Open()
            RDR = COM.ExecuteReader
            If RDR.HasRows = True Then
                While RDR.Read
                    LST.Add(New dataset(RDR("id"), RDR("name")))
                End While
            End If
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

        Return LST

    End Function

End Class