Imports System.IO
Imports MySql.Data.MySqlClient

Public Class friendly_fighter

    ' Scoring Information
    Private _match As Integer           ' Match Number
    Private _person As person           ' Fighter ID
    Private _score As Integer           ' Total Score
    Private _gamjum As Integer          ' Total Gamgum Foul

    '-----------------------------------------------------------------------------------------------------------
    ' Properties
    '-----------------------------------------------------------------------------------------------------------

    Public Sub New(FighterID As Integer)
        _person = New person(FighterID)
        _score = 0
        _gamjum = 0
    End Sub

    '-----------------------------------------------------------------------------------------------------------
    ' Properties
    '-----------------------------------------------------------------------------------------------------------

    ReadOnly Property Person As person
        Get
            Return _person
        End Get
    End Property

    ReadOnly Property Score As Integer
        Get
            Return _score
        End Get
    End Property

    ReadOnly Property Gamjum As Integer
        Get
            Return _gamjum
        End Get
    End Property

    '-----------------------------------------------------------------------------------------------------------
    ' Subroutines
    '-----------------------------------------------------------------------------------------------------------

    Public Sub AdjustScore(Amount As Integer)

        _score += Amount

    End Sub

    Public Sub AdjustGamjum(Amount As Integer)

        _gamjum += Amount

    End Sub

    '-----------------------------------------------------------------------------------------------------------
    ' Subroutines
    '-----------------------------------------------------------------------------------------------------------

    Public Sub RegisterScore(Score As Integer, Gamjum As Integer)

        Dim QRY As String = ("
            insert into event_matches_scores (mid, pid, score, foul) values (" & _match & ", " & _person.ID & ", " & Score & ", " & Gamjum & ");
            select sum(score) as 'score', sum(gamjum) as 'gamjum' from event_matches_scores where eid = null and mid = " & _match & " and pid = " & _person.ID & ";
        ")

        ' Connection
        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 ' Score
                        _score = RDR("score")
                    Catch ex As Exception
                        _score = 0
                    End Try

                    Try ' Gamjum
                        _gamjum = RDR("gamjum")
                    Catch ex As Exception
                        _gamjum = 0
                    End Try

                End While

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

    End Sub

    Private Sub GetMatchScores()

        Dim QRY As String = ("select sum(score) as 'score', sum(foul) as 'gamjum' from event_matches_scores where eid = null and mid = " & _match & " and pid = " & _person.ID & ";")
        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 ' Score
                        _score = RDR("score")
                    Catch ex As Exception
                        _score = 0
                    End Try

                    Try ' Gamjum
                        _gamjum = RDR("gamjum")
                    Catch ex As Exception
                        _gamjum = 0
                    End Try

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

    End Sub

    Public Function GetPointsList() As List(Of country)

        Dim QRY As String = ("
            select 
                score, 
                gamjum 
            from 
                event_matches_scores 
            where 
                eid = null and mid = " & _match & " and pid = " & _person.ID & "
            order by
                eid,
                mid
                desc;
        ")

        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
