Imports System.IO
Imports MySql.Data.MySqlClient

Public Class platformevent

    ' TAKEONE Event
    Private _id As Integer
    Private _title As String
    Private _organizer As String
    Private _type As String
    Private _rules As String

    ' Location
    Private _address As String
    Private _location As String
    Private _country As country

    ' Event Parameters
    Private _court As Integer
    Private _enroll_start As Date
    Private _enroll_end As Date
    Private _weigh_in As Date
    Private _event_start As Date
    Private _event_end As Date
    Private _fee As Double
    Private _currency As String
    Private _logo As MemoryStream

    ' Making New Instance Without Any Action
    Public Sub New()

    End Sub

    ' Make New Instance Of Event By Reading Data From Its ID
    Public Sub New(id As Integer)

        GetEventData(id)

    End Sub

    ' Make New Instance Directly By Creating A New Event Record
    Public Sub New(EventTitle As String, Type As String, Rounds As Integer, Organizer As String, Country As Integer, Address As String, Location As String, Courts As Integer, Rules As String, Currency As String, Fee As Integer, EnrollStart As String, EnrollEnd As String, WeighIn As String, EventStart As String, EventEnd As String, Logo As PictureBox)

        CreateNewEvent(EventTitle, Type, Rounds, Organizer, Country, Address, Location, Courts, Rules, Currency, Fee, EnrollStart, EnrollEnd, WeighIn, EventStart, EventEnd, Logo)
        GetEventData(_id)

    End Sub


    ' Set Of Properties

    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 Organizer As String
        Get
            Return _organizer
        End Get
    End Property

    ReadOnly Property Type As String
        Get
            Return _type
        End Get
    End Property

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

    ReadOnly Property Address As String
        Get
            Return _address
        End Get
    End Property

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

    ReadOnly Property Courts As Integer
        Get
            Return _court
        End Get
    End Property

    ReadOnly Property EnrollStartDate As Date
        Get
            Return _enroll_start
        End Get
    End Property

    ReadOnly Property EnrollEndDate As Date
        Get
            Return _enroll_end
        End Get
    End Property

    ReadOnly Property WeighInDate As Date
        Get
            Return _weigh_in
        End Get
    End Property

    ReadOnly Property EventStartDate As Date
        Get
            Return _event_start
        End Get
    End Property

    ReadOnly Property EventEndDate As Date
        Get
            Return _event_end
        End Get
    End Property

    ReadOnly Property Rules As String
        Get
            Return _rules
        End Get
    End Property

    ReadOnly Property Currency As String
        Get
            Return _currency
        End Get
    End Property

    ReadOnly Property Fee As Integer
        Get
            Return _fee
        End Get
    End Property

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

    ' Set Of Functions

    Public Function EventList()

        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 platformevent)

        Try
            CON.Open()
            RDR = COM.ExecuteReader
            If RDR.HasRows = True Then
                While RDR.Read
                    LST.Add(New platformevent(RDR("id")))
                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 GetEventData(id As Integer)

        Dim QRY As String = ("select event.* from event where event.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
                        _organizer = RDR("organizor")
                    Catch ex As Exception
                        _organizer = Nothing
                    End Try

                    Try
                        _type = RDR("type")
                    Catch ex As Exception
                        _type = Nothing
                    End Try

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

                    Try
                        _address = RDR("address")
                    Catch ex As Exception
                        _address = Nothing
                    End Try

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

                    Try
                        _court = RDR("courts")
                    Catch ex As Exception
                        _court = 0
                    End Try

                    Try
                        _enroll_start = CDate(RDR("enroll_start"))
                    Catch ex As Exception
                        _enroll_start = Now
                    End Try

                    Try
                        _enroll_end = CDate(RDR("enroll_end"))
                    Catch ex As Exception
                        _enroll_end = Now
                    End Try

                    Try
                        _weigh_in = CDate(RDR("weigh_in"))
                    Catch ex As Exception
                        '_weigh_in = Now
                    End Try

                    Try
                        _event_start = CDate(RDR("event_start"))
                    Catch ex As Exception
                        '_event_start = Now
                    End Try

                    Try
                        _event_end = CDate(RDR("event_end"))
                    Catch ex As Exception
                        '_event_end = Now
                    End Try

                    Try
                        _rules = RDR("rules")
                    Catch ex As Exception
                        _rules = Nothing
                    End Try

                    Try
                        _currency = RDR("currency ")
                    Catch ex As Exception
                        _currency = Nothing
                    End Try

                    Try
                        _fee = RDR("participation_fee")
                    Catch ex As Exception
                        _fee = 0
                    End Try

                    Try ' Event Logo -----------------------------------------
                        Dim data As Byte() = DirectCast(RDR("logo"), Byte())
                        _logo = New 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

    ' Set Of Routines

    Public Sub CreateNewEvent(EventTitle As String, Type As String, Rounds As Integer, Organizer As String, Country As Integer, Address As String, Location As String, Courts As Integer, Rules As String, Currency As String, Fee As Integer, EnrollStart As String, EnrollEnd As String, WeighIn As String, EventStart As String, EventEnd As String, Logo As PictureBox)

        ' Query To Update
        Dim QRY As String = ("

            /* This Is To Insert The Information Of The New Event */
            insert into
            event	(title, type, rounds, organizor, country, address, location, courts, rules, currency, fee, enroll_start, enroll_end, weigh_in, event_start, event_end, logo)
            values	('" & EventTitle & "', '" & Type & "', '" & Rounds & "', '" & Organizer & "', " & Country & ", '" & Address & "', '" & Location & "', " & Courts & ", '" & Rules & "', '" & Currency & "', " & Fee & ", '" & EnrollStart & "', '" & EnrollEnd & "', '" & WeighIn & "', '" & EventStart & "', '" & EventEnd & "', @logo);

            /* This Is To Return The Newly Created Event Record */
            select last_insert_id() as 'eid';

        ")

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

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


        Dim RDR As MySqlDataReader

        Try
            CON.Open()
            COM.Parameters.AddWithValue("@logo", arrImage)
            RDR = COM.ExecuteReader
            If RDR.HasRows = True Then

                While RDR.Read

                    Try ' ID
                        _id = RDR("eid")
                    Catch ex As Exception
                        _id = 0
                    End Try

                End While

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

    End Sub

    Public Sub UpdateLogo(Logo As PictureBox)

        ' Query To Update
        Dim QRY As String = "update event 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
        Logo.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
