Imports System.IO
Imports MySql.Data.MySqlClient

Public Class country

    Private _id As Integer
    Private _name As String
    Private _flag As MemoryStream
    Private _flat_flag As String
    Private _shiny_flag As String
    Private _iso As String
    Private _iso3 As String
    Private _num_code As Integer
    Private _call_code As Integer
    Private _currency As String

    Public Sub New()

    End Sub

    Public Sub New(id As Integer)
        GetCountryData(id)
    End Sub

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

    ReadOnly Property Name As String
        Get
            Return _name
        End Get
    End Property

    ReadOnly Property Flag As MemoryStream
        Get
            Return _flag
        End Get
    End Property

    ReadOnly Property FlatFlag As String
        Get
            Return _flat_flag
        End Get
    End Property

    ReadOnly Property ShinyFlag As String
        Get
            Return _shiny_flag
        End Get
    End Property

    ReadOnly Property ISO As String
        Get
            Return _iso
        End Get
    End Property

    ReadOnly Property ISO3 As String
        Get
            Return _iso3
        End Get
    End Property

    ReadOnly Property NumCode As Integer
        Get
            Return _num_code
        End Get
    End Property

    ReadOnly Property CallCode As Integer
        Get
            Return _call_code
        End Get
    End Property

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

    Private Sub GetCountryData(ID As Integer)

        Dim QRY As String = ("select * from countries 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
                        _id = RDR("id")
                    Catch ex As Exception
                        _id = 0
                    End Try

                    Try ' First Name
                        _name = RDR("name")
                    Catch ex As Exception
                        _name = Nothing
                    End Try

                    Try ' Middle Name
                        _flat_flag = RDR("flatFlag")
                    Catch ex As Exception
                        _flat_flag = Nothing
                    End Try

                    Try ' Last Name
                        _shiny_flag = RDR("shinyFlag")
                    Catch ex As Exception
                        _shiny_flag = Nothing
                    End Try

                    Try ' Gender
                        _iso = RDR("iso")
                    Catch ex As Exception
                        _iso = Nothing
                    End Try

                    Try ' Birthday
                        _iso3 = RDR("iso3")
                    Catch ex As Exception
                        _iso3 = Nothing
                    End Try

                    Try ' Age
                        _num_code = RDR("numcode")
                    Catch ex As Exception
                        _num_code = 0
                    End Try

                    Try ' Weight
                        _call_code = RDR("callcode")
                    Catch ex As Exception
                        _call_code = 0
                    End Try

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

                    Try ' Country Flag --------------------------------------
                        Dim data As Byte() = DirectCast(RDR("flag"), Byte())
                        _flag = New System.IO.MemoryStream(data)
                    Catch ex As Exception
                        _flag = Nothing
                    End Try

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

    End Sub

    Public Sub UpdateFlag(FlagPictureBox As PictureBox)

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

        ' Query To Update
        Dim QRY As String = "update countries set flag = @flag 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
        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

    Public Function GetCountryList() As List(Of country)

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

        Try
            CON.Open()
            RDR = COM.ExecuteReader
            If RDR.HasRows = True Then
                While RDR.Read
                    LST.Add(New country(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

End Class
