Imports System.IO
Imports MySql.Data.MySqlClient

Public Class referee

    ' Referee Data Variables
    Private _id As Integer                  ' Member ID
    Private _first_name As String           ' First Name
    Private _middle_name As String          ' Middle Name
    Private _last_name As String            ' Last Name
    Private _gender As String               ' Gender
    Private _birthday As Date               ' Date Of Birth
    Private _age As Integer                 ' Calculated From Birthday
    Private _picture As MemoryStream        ' Picture

    ' Referee Team / Club
    Private _team As club                   ' Team Within Country

    ' Referee Country
    Private _nationality As country         ' Country Name

    '--------------------------------------------------------------------------------------
    ' CONSTRUCTORS
    '--------------------------------------------------------------------------------------

    ' With Initial Parameter
    Public Sub New(ID As Integer)
        GetRefereeData(ID)
    End Sub

    '--------------------------------------------------------------------------------------
    ' PROPERTIES
    '--------------------------------------------------------------------------------------

    Public Property ID As Integer
        Get
            Return _id
        End Get
        Set(value As Integer)
            _id = value
            GetRefereeData(_id)
        End Set
    End Property

    ReadOnly Property FirstNAME As String
        Get
            Return _first_name
        End Get
    End Property

    ReadOnly Property LastNAME As String
        Get
            Return _last_name
        End Get
    End Property

    ReadOnly Property MiddleNAME As String
        Get
            Return _first_name + " " + _last_name
        End Get
    End Property

    ReadOnly Property ShortName As String
        Get
            Return _first_name + " " + _last_name
        End Get
    End Property

    ReadOnly Property FullName As String
        Get
            Return _first_name + " " + _middle_name + " " + _last_name
        End Get
    End Property

    ReadOnly Property Age As Integer
        Get
            Return _age
        End Get
    End Property

    ReadOnly Property Birthday As Date
        Get
            Return _birthday
        End Get
    End Property

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

    ReadOnly Property Team As club
        Get
            Return _team
        End Get
    End Property

    ReadOnly Property Picture As MemoryStream
        Get
            Return _picture
        End Get
    End Property

    '--------------------------------------------------------------------------------------
    ' SUBROUTINES
    '--------------------------------------------------------------------------------------
    Private Sub GetRefereeData(FighterID As Integer)

        Dim QRY As String =
        ("
            select
	            person.id as 'pid',
	            person.fname,
                person.mname,
                person.lname,
                TIMESTAMPDIFF(YEAR, person.date_of_birth, CURDATE()) as 'age',
                person.date_of_birth,
	            person.gender,
                person.club as 'club',
                person.country as 'country',
                person.picture
            FROM 
	            person
            where 
	            person.id=" + FighterID.ToString
        )

        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 = RDR("pid")
                    Catch ex As Exception
                        _id = 0
                    End Try

                    Try
                        _first_name = RDR("fname")
                    Catch ex As Exception
                        _first_name = Nothing
                    End Try

                    Try
                        _middle_name = RDR("mname")
                    Catch ex As Exception
                        _middle_name = Nothing
                    End Try

                    Try
                        _last_name = RDR("lname")
                    Catch ex As Exception
                        _last_name = Nothing
                    End Try

                    Try
                        _gender = RDR("gender")
                    Catch ex As Exception
                        _gender = Nothing
                    End Try

                    Try
                        _birthday = CDate(RDR("date_of_birth"))
                    Catch ex As Exception
                        _birthday = Nothing
                    End Try

                    Try
                        _age = RDR("age")
                    Catch ex As Exception
                        _age = 0
                    End Try

                    Try
                        _team = New club(Int(RDR("club")))
                    Catch ex As Exception
                        _team = Nothing
                    End Try

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

                    Try ' Fighter Personal Picture ----------------------------------
                        Dim arrImage() As Byte = DirectCast(RDR("picture"), Byte())
                        _picture = New MemoryStream(arrImage)
                    Catch ex As Exception
                        _picture = Nothing
                    End Try '--------------------------------------------------------

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

    End Sub

End Class