Imports System.IO
Imports MySql.Data.MySqlClient

Public Class club

    Private _id As Integer
    Private _title As String
    Private _owner As person
    Private _country As country
    Private _address As String
    Private _location As String
    Private _logo As MemoryStream

    Public Sub New(ID As Integer)
        GetClubData(ID)
    End Sub

    ReadOnly Property ID As Integer
        Get
            Return _id
        End Get
    End Property

    ReadOnly Property Title As String
        Get
            Return _title
        End Get
    End Property

    ReadOnly Property Country As country
        Get
            Return _country
        End Get
    End Property

    ReadOnly Property Location As String
        Get
            Return _location
        End Get
    End Property

    ReadOnly Property Logo As MemoryStream
        Get
            Return _logo
        End Get
    End Property

    Public Sub GetClubData(ID As Integer)

        Dim QRY As String = "select * from clubs where id = " & ID & " limit 1"

        Dim CON As New MySqlConnection(CON_STRING)
        Dim COM As New MySqlCommand(QRY, CON)
        Dim RDR As MySqlDataReader

        Try
            CON.Open()
            RDR = COM.ExecuteReader
            If RDR.HasRows = True Then
                While RDR.Read

                    Try
                        _id = Int(RDR("id"))
                    Catch ex As Exception
                        _id = 0
                    End Try

                    Try
                        _title = RDR("title")
                    Catch ex As Exception
                        _title = Nothing
                    End Try

                    Try
                        _country = New country(Int(RDR("country")))
                    Catch ex As Exception
                        _country = Nothing
                    End Try

                    Try
                        _location = RDR("location")
                    Catch ex As Exception
                        _location = Nothing
                    End Try

                    Try ' Club Logo -----------------------------------------
                        Dim data As Byte() = DirectCast(RDR("logo"), Byte())
                        _logo = New System.IO.MemoryStream(data)
                    Catch ex As Exception
                        _logo = Nothing
                    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 Create(Title As String, Country As country, Location As String, Logo As MemoryStream)

    End Sub

    Public Sub UpdateLogo(LogoPictureBox As PictureBox)

        ' To Avoid Wrong Execution Of Code
        If _id = 0 Then Exit Sub

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

        ' 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

End Class
