Imports System.IO
Imports MySql.Data.MySqlClient

Module functions

    '-------------------------------------------------------------------------------------------------------------
    ' Person
    '-------------------------------------------------------------------------------------------------------------

    Public Function GetPersonList() 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
            left join
	            event_matches on (person.id = coalesce(event_matches.blue_fighter, 0) or person.id = coalesce(event_matches.red_fighter, 0))
            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

    '-------------------------------------------------------------------------------------------------------------
    ' File Functions
    '-------------------------------------------------------------------------------------------------------------

    Public Function GetFilePath() As String

        'Creating A New Dialog
        Dim OpenVideoFile As New OpenFileDialog

        'Filtering The Types
        OpenVideoFile.Filter = "Video Files | *.mp4; *.avi; *.mkv;"

        'Openning The File
        If OpenVideoFile.ShowDialog = System.Windows.Forms.DialogResult.OK Then

            Try
                Dim FilePath As String = OpenVideoFile.FileName()
                Return FilePath
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return Nothing
            End Try

        Else

            Return Nothing

        End If

    End Function

    '-------------------------------------------------------------------------------------------------------------
    ' Picture Function
    '-------------------------------------------------------------------------------------------------------------

    Public Function ResizePicture(YourPictureBox As PictureBox, Width As Integer, Height As Integer) As Byte()

        'Creating A New Dialog
        Dim OpenPictureFile As New OpenFileDialog

        'Filtering The Types
        OpenPictureFile.Filter = "Image Files | *.jpg; *.jpeg; *.bmp; *.png; *.gif;"

        'Openning The File
        If OpenPictureFile.ShowDialog = System.Windows.Forms.DialogResult.OK Then

            Try
                ' Getting File Path
                Dim FilePath As String = OpenPictureFile.FileName()
                Dim FileName As String = System.IO.Path.GetFileName(OpenPictureFile.FileName)

                ' Picture File
                Dim Picture As New MemoryStream

                ' Define New Image Size
                Dim LogoSize As New Size(Width, Height)

                ' Resizing The Image
                Dim ResizedImage = New Bitmap(Image.FromFile(FilePath), LogoSize)

                ' Display The Image
                YourPictureBox.Image = ResizedImage

                ' Saving The Image As PNG Format
                YourPictureBox.Image.Save(Picture, System.Drawing.Imaging.ImageFormat.Png)

                ' Read The Image As Bytes
                Dim arrImage() As Byte = Picture.GetBuffer

                ' Closing The Picture
                Picture.Close()

                ' Returns Image Data In Bytes Format
                Return arrImage

            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return Nothing
            End Try

        Else

            Return Nothing

        End If

    End Function

    '-------------------------------------------------------------------------------------------------------------
    ' Country Functions
    '-------------------------------------------------------------------------------------------------------------

    Public Sub GetCountryList(ComboBox As ComboBox)

        Dim QRY As String = "select * from countries order by name 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

        ComboBox.DataSource = LST
        ComboBox.ValueMember = "ID"
        ComboBox.DisplayMember = "name"

    End Sub

    Public Sub GetCountryFlag(FlagPictureBox As PictureBox, CountryID As Integer)

        Dim QRY As String

        Try
            QRY = "select flag from countries where id=" & CountryID
        Catch ex As Exception
            Exit Sub
        End Try

        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
                        Dim data As Byte() = DirectCast(RDR("flag"), Byte())
                        Dim flag As New System.IO.MemoryStream(data)
                        FlagPictureBox.Image = Image.FromStream(flag)
                    Catch ex As Exception
                        FlagPictureBox.Image = Nothing
                        Exit Sub
                    End Try
                End While
            End If
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

    End Sub

    Public Sub UpdateCountryFlag(FlagPictureBox As PictureBox, CountryID As Integer)

        ' Query To Update
        Dim QRY As String = "update countries set flag = @flag where id=" & CountryID

        ' Connection
        Dim CON As New MySqlConnection(CON_STRING)
        Dim COM As New MySqlCommand(QRY, CON)

        ' Preparing The Image
        Dim Picture As New MemoryStream
        FlagPictureBox.Image.Save(Picture, System.Drawing.Imaging.ImageFormat.Png)
        Dim arrImage() As Byte = Picture.GetBuffer
        Picture.Close()

        Try
            CON.Open()
            COM.Parameters.AddWithValue("@flag", arrImage)
            COM.ExecuteNonQuery()
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

    End Sub

    '-------------------------------------------------------------------------------------------------------------
    ' Clubs Functions
    '-------------------------------------------------------------------------------------------------------------

    Public Sub GetClubsList(ComboBox As ComboBox)

        Dim QRY As String = "select id, title from clubs order by title 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("title")))
                End While
            End If
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

        ComboBox.DataSource = LST
        ComboBox.ValueMember = "ID"
        ComboBox.DisplayMember = "name"

    End Sub

    Public Sub GetClubLogo(LogoPictureBox As PictureBox, ClubID As Integer)

        Dim QRY As String

        Try
            QRY = "select logo from clubs where id=" & ClubID
        Catch ex As Exception
            Exit Sub
        End Try

        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
                        Dim data As Byte() = DirectCast(RDR("logo"), Byte())
                        Dim flag As New System.IO.MemoryStream(data)
                        LogoPictureBox.Image = Image.FromStream(flag)
                    Catch ex As Exception
                        LogoPictureBox.Image = Nothing
                        Exit Sub
                    End Try
                End While
            End If
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

    End Sub

    Public Sub UpdateClubLogo(LogoPictureBox As PictureBox, ClubID As Integer)

        ' Query To Update
        Dim QRY As String = "update clubs set logo = @logo where id=" & ClubID

        ' Connection
        Dim CON As New MySqlConnection(CON_STRING)
        Dim COM As New MySqlCommand(QRY, CON)

        ' Preparing The Image
        Dim Picture As New MemoryStream
        LogoPictureBox.Image.Save(Picture, System.Drawing.Imaging.ImageFormat.Png)
        Dim arrImage() As Byte = Picture.GetBuffer
        Picture.Close()

        Try
            CON.Open()
            COM.Parameters.AddWithValue("@logo", arrImage)
            COM.ExecuteNonQuery()
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

    End Sub

    Public Sub EventList(ListBox As ListBox)

        Try
            ListBox.Items.Clear()
        Catch ex As Exception

        End Try

        Dim QRY As String = "select * from event order by event_start 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("title")))
                End While
            End If
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

        ListBox.DataSource = LST
        ListBox.ValueMember = "ID"
        ListBox.DisplayMember = "name"

    End Sub

    '-------------------------------------------------------------------------------------------------------------
    ' Clubs Functions
    '-------------------------------------------------------------------------------------------------------------

    Public Function IsFormOpen(ByVal frm As Form) As Boolean
        If Application.OpenForms.OfType(Of Form).Contains(frm) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Sub ClearComboBox(ComboBox As ComboBox)

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

    End Sub

    Public Sub ClearListBox(ListBox As ListBox)

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

    End Sub

    Public Sub SubPopulateComboBox(Data As List(Of dataset), ComboBox As ComboBox)

        ClearComboBox(ComboBox)
        ComboBox.DataSource = Data
        ComboBox.ValueMember = "ID"
        ComboBox.DisplayMember = "NAME"

    End Sub

    Public Sub SubPopulateListBox(Data As List(Of dataset), ListBox As ListBox)

        ClearListBox(ListBox)
        ListBox.DataSource = Data
        ListBox.ValueMember = "ID"
        ListBox.DisplayMember = "NAME"

    End Sub

End Module
