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

Public Class frm_event_control

    Public EventID As String
    Public EventTitle As String

    Private Sub EventBasicStats(eventID As Integer)

        Dim QRY As String = ("
            select
	            (select count(*) From (select distinct ern from event_matches where eid = " & eventID & ") as TOTALROUNDS) as 'TR',
                (select * From (select distinct ern from event_matches where eid = " & eventID & " and started = 0 and ended = 0 limit 1) as CURRENTROUND) as 'CR',
	            (select count(*) From (select * from event_matches where eid = " & eventID & ") as TOTALMATCHES) as 'TM',
                (select count(*) From (select * from event_matches where eid = " & eventID & " and started = 0 and ended = 0) as REMMATCHES) as 'RM',
	            (select count(*) From (select distinct person.* from event_matches left join person on event_matches.blue_fighter = person.id or event_matches.red_fighter = person.id where person.gender = 'MALE' and event_matches.eid = " & eventID & ") as MALE) as 'GM',
                (select count(*) From (select distinct person.* from event_matches left join person on event_matches.blue_fighter = person.id or event_matches.red_fighter = person.id where person.gender = 'FEMALE' and event_matches.eid = " & eventID & ") as FEMALE) as 'GF',
                (select count(*) From (select distinct clubs.Title from event_matches left join person on event_matches.blue_fighter = person.id or event_matches.red_fighter = person.id left join clubs on person.club = clubs.id where event_matches.eid = " & eventID & " group by clubs.title) as CLUBS) as 'CL'
        ")

        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_rounds.Text = RDR("TR")
                    Catch ex As Exception
                        lbl_total_rounds.Text = Nothing
                    End Try

                    Try
                        lbl_current_round.Text = RDR("CR")
                    Catch ex As Exception
                        lbl_current_round.Text = Nothing
                    End Try

                    Try
                        lbl_total_matches.Text = RDR("TM")
                    Catch ex As Exception
                        lbl_total_matches.Text = Nothing
                    End Try

                    Try
                        lbl_remaining_matches.Text = RDR("RM")
                    Catch ex As Exception
                        lbl_remaining_matches.Text = Nothing
                    End Try

                    Try
                        lbl_total_clubs.Text = RDR("CL")
                    Catch ex As Exception
                        lbl_total_clubs.Text = Nothing
                    End Try

                    Try
                        lbl_total_males.Text = RDR("GM")
                    Catch ex As Exception
                        lbl_total_males.Text = Nothing
                    End Try

                    Try
                        lbl_total_females.Text = RDR("GF")
                    Catch ex As Exception
                        lbl_total_females.Text = Nothing
                    End Try

                    Try
                        lbl_total_referees.Text = 0
                    Catch ex As Exception
                        lbl_total_rounds.Text = 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 Sub btn_add_participant_Click(sender As Object, e As EventArgs) Handles btn_participants.Click

        frm_add_participants.Show()
        Me.Hide()

    End Sub

    Private Sub frm_event_control_MaximumSizeChanged(sender As Object, e As EventArgs) Handles Me.MaximumSizeChanged

        frm_main.Show()

    End Sub

    Private Sub frm_event_control_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        lbl_event_title_display.Text = SelectedEvent.Title

        Try
            pb_event_logo.Image = Image.FromStream(SelectedEvent.Logo)
        Catch ex As Exception
            pb_event_logo.Image = Nothing
        End Try

        Try
            EventBasicStats(SelectedEvent.ID)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btn_back_Click(sender As Object, e As EventArgs) Handles btn_back.Click

        frm_main.Show()
        Me.Close()

    End Sub

    Private Sub btn_create_matches_Click(sender As Object, e As EventArgs) Handles btn_start_event.Click

        frm_scorecontrol.Show()
        Me.Hide()

    End Sub

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

        frm_main.Show()

    End Sub

    Private Sub pb_event_logo_Click(sender As Object, e As EventArgs) Handles pb_event_logo.Click

        If IsNothing(pb_event_logo.Image) Then

            ' Open Picture File
            ResizePicture(pb_event_logo, 500, 500)

            ' Store The Picture
            SelectedEvent.UpdateLogo(pb_event_logo)

        ElseIf Not IsNothing(pb_event_logo.Image) Then

            Dim iPictureUpdate As DialogResult = MessageBox.Show("Would You Like To Update The Players Picture ?", "Picture Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

            If iPictureUpdate = DialogResult.Yes Then

                ' Open Picture File
                ResizePicture(pb_event_logo, 500, 500)

                ' Store The Picture
                SelectedEvent.UpdateLogo(pb_event_logo)

            End If

        End If

    End Sub

    Private Sub btn_statistics_Click(sender As Object, e As EventArgs) Handles btn_fighter_statistics.Click

        If Not SelectedEvent.ID = 0 Then
            frm_fighter_statistics.Show()
            Me.Hide()
        End If

    End Sub

    Private Sub btn_event_results_Click(sender As Object, e As EventArgs) Handles btn_event_results.Click

        frm_ranking.Show()
        Me.Hide()

    End Sub

    Private Sub btn_draw_managment_Click(sender As Object, e As EventArgs) Handles btn_draw_managment.Click

        frm_arrange_matches.Show()
        Me.Hide()

    End Sub

End Class
