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

Public Class frm_arrange_matches
    Private Sub btn_close_Click(sender As Object, e As EventArgs)

        Me.Close()

    End Sub

    Private Sub frm_arrange_matches_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing

        frm_event_control.Show()

    End Sub

    Private Sub btn_close_Click_1(sender As Object, e As EventArgs) Handles btn_close.Click

        Me.Close()

    End Sub

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

        FunGetStats()
        cbo_gender.SelectedIndex = 0
        SubPopulateListBox(FunListSetMatches(cbo_category.SelectedValue), lst_matches_set)

    End Sub

    Private Sub lst_matches_set_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lst_matches_set.SelectedIndexChanged

        Try
            Load_Match_Data(lst_matches_set.SelectedValue)
        Catch ex As Exception

        End Try

    End Sub


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

    Private Sub DisplayBlueFighter(Person As person)

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

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

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

    End Sub

    Private Sub DisplayRedFighter(Person As person)

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

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

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

    End Sub

    Private Function FunListSetMatches(WeightProfile As Integer) As List(Of dataset)

        Dim QRY As String = ("

            SELECT 
	            event_matches.id as RecordNumber,
                event_matches.eid as EventID,
                event_matches.ern as EventRound,
                event_matches.emn as MatchNumber,
                concat(blue.fname, if(isnull(blue.mname), '', concat(' ', blue.mname)), ' ', blue.lname) as BlueFighter,
                concat(red.fname, if(isnull(red.mname), '', concat(' ', red.mname)), ' ', red.lname) as RedFighter,
    
                CASE
		            WHEN (isNull(event_matches.blue_fighter) = true OR isNull(event_matches.red_fighter) = true) THEN 'FREE'
		            WHEN (isNull(event_matches.blue_fighter) = false AND isNull(event_matches.red_fighter) = true) THEN 'UNSET'
		            WHEN (event_matches.blue_fighter != 0 AND event_matches.red_fighter != 0) THEN 'SET'
		            ELSE 'ERROR'
	            END as MatchSet,
    
                CASE
		            WHEN (event_matches.started = 1 AND event_matches.ended = 1) THEN 'FINISHED'
		            WHEN (event_matches.started = 1 AND event_matches.ended = 0) THEN 'ONGOING'
		            WHEN (event_matches.started = 0 AND event_matches.ended = 0) THEN 'UPCOMING'
		            ELSE 'ERROR'
	            END as MatchStatus
    
            FROM
	            event_matches
            LEFT JOIN
	            person as blue on blue.id = event_matches.blue_fighter
            LEFT JOIN
	            person as red on red.id = event_matches.red_fighter
            WHERE
	            (blue_fighter != 0 OR red_fighter != 0)
                AND
                blue.wp = " & WeightProfile & "
                AND
                red.wp = " & WeightProfile & "
            ORDER BY
	            event_matches.ern,
	            event_matches.emn;

        ")

        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("RecordNumber"), "R" & RDR("EventRound") & " M" & RDR("MatchNumber") & " : " & RDR("BlueFighter") & " VS " & RDR("RedFighter")))
                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

    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 Sub FunGetStats()

        Dim QRY As String = ("

            SELECT
	        (select count(*) From (select * from event_matches where eid = " & SelectedEvent.ID & ") as TOTALMATCHES) as 'TOTAL',
            (select count(*) From (select * from event_matches where eid = " & SelectedEvent.ID & " and (blue_fighter != 0 AND NOT isNull(blue_fighter)) and (red_fighter != 0 AND NOT isNull(red_fighter))) as SETMATCHES) as 'SET'

        ")

        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

                    Try
                        lbl_total_matches.Text = RDR("TOTAL")
                    Catch ex As Exception
                        lbl_total_matches.Text = 0
                    End Try

                    Try
                        lbl_set_matches.Text = RDR("SET")
                    Catch ex As Exception
                        lbl_set_matches.Text = 0
                    End Try

                End While
            End If
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

    End Sub

    Private Sub Load_Match_Data(MatchID As Integer)

        Dim QRY As String = ("

            /*-----------------------------------------------------
	            THIS CODE IS FOR SEARCHING THE MATCH
                BY MATCH ID NUMBER
            -----------------------------------------------------*/

            SELECT
	            event_matches.blue_fighter as 'blue',
                event_matches.red_fighter as 'red'
            FROM 
	            event_matches
            WHERE
                (NOT event_matches.blue_fighter = 0 AND NOT isNull(event_matches.blue_fighter))
                AND 
                (NOT event_matches.red_fighter = 0 AND NOT isNull(event_matches.red_fighter))
                AND
                event_matches.id = " & MatchID & "
                AND
                event_matches.eid = " & SelectedEvent.ID & "
            ORDER BY
                ern,
                emn
	            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

                    Dim current_blue_fighter As person

                    Try

                        current_blue_fighter = New person(RDR("blue"))

                        Try
                            pb_blue_logo.Image = Image.FromStream(current_blue_fighter.Team.Logo)
                        Catch ex As Exception
                            pb_blue_logo.Image = Nothing
                        End Try

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

                        Try
                            pb_blue_fighter.Image = Image.FromStream(current_blue_fighter.Picture)
                        Catch ex As Exception
                            pb_blue_fighter.Image = Nothing
                        End Try

                    Catch ex As Exception

                        current_blue_fighter = Nothing

                    End Try

                    Dim current_red_fighter As person

                    Try

                        current_red_fighter = New person(RDR("red"))

                        Try
                            pb_red_logo.Image = Image.FromStream(current_red_fighter.Team.Logo)
                        Catch ex As Exception
                            pb_red_logo.Image = Nothing
                        End Try

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

                        Try
                            pb_red_fighter.Image = Image.FromStream(current_red_fighter.Picture)
                        Catch ex As Exception
                            pb_red_fighter.Image = Nothing
                        End Try

                    Catch ex As Exception

                        current_red_fighter = Nothing

                    End Try

                End While
            End If
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

    End Sub

    Private Function Load_Weight_Profiles(Gender As String) As List(Of dataset)

        Dim QRY As String = ("

            /*-----------------------------------------------------
	            THIS CODE IS FOR SEARCHING THE MATCH
                BY MATCH ID NUMBER
            -----------------------------------------------------*/

            SELECT
	            distinct
                weight_divisions.id,
                weight_divisions.category,
                weight_divisions.className
            FROM 
	            event_participants
            LEFT JOIN
	            person ON event_participants.pid = person.id
            LEFT JOIN
	            weight_divisions ON weight_divisions.id = person.wp
            WHERE
	            person.gender = '" & Gender & "'
            ORDER BY
	            weight_divisions.category,
                weight_divisions.gender,
                weight_divisions.className
        ")

        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("category") & " : " & RDR("className"))))
                End While
            End If
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

        Return LST

    End Function

    Private Sub cbo_gender_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbo_gender.SelectedIndexChanged

        Try
            SubPopulateComboBox(Load_Weight_Profiles(cbo_gender.Text), cbo_category)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub cbo_category_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbo_category.SelectedIndexChanged

        Try
            SubPopulateListBox(FunListSetMatches(cbo_category.SelectedValue), lst_matches_set)
        Catch ex As Exception

        End Try

    End Sub

End Class