Imports System.IO
Imports MySql.Data.MySqlClient

' Weight Class To Generate Weight (Category, Class) From Gender, Weight, Date Of Birth.
Public Class weight

    Private _id As Integer              ' Weight Profile ID
    Private _age As age                 ' Age Object (years, monthes, days)
    Private _gender As String           ' Weight Filter Gender
    Private _weight As Double           ' Weight In KG
    Private _weight_class As String     ' Weight Class Name
    Private _weight_category As String  ' Weight Category Name

    Public Sub New(Birthdate As Date, Gender As String, Weight As Double)

        _age = New age(Birthdate)
        _gender = Gender
        _weight = Weight

        ' Weight Filter That Gets
        WeightFilter(_age.Years, Weight, Gender)

    End Sub

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

    ReadOnly Property Weight As String
        Get
            Return Math.Round(_weight, 1).ToString
        End Get
    End Property

    ReadOnly Property WeightClass As String
        Get
            Return _weight_class
        End Get
    End Property

    ReadOnly Property WeightCategory As String
        Get
            Return _weight_category
        End Get
    End Property

    Private Sub WeightFilter(Age As Integer, Weight As String, Gender As String)

        Dim QRY As String = ("

            SELECT
                id,
	            Category,
                ClassName
            FROM
	            weight_divisions 
            WHERE
	            ('" & Age & "' BETWEEN weight_divisions.minAge AND weight_divisions.maxAge) 
                AND
                (weight_divisions.gender = '" & Gender & "') 
                AND
                ('" & Weight & "' > weight_divisions.minWeight AND '" & Weight & "' <= weight_divisions.maxWeight)

        ")

        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
                    _id = RDR("id")
                    _weight_category = RDR("Category")
                    _weight_class = RDR("ClassName")
                End While
            End If

            CON.Close()
        Catch ex As MySqlException
            _weight_category = Nothing
            _weight_class = Nothing
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

    End Sub

    Private Sub GetPersonWeight(PersonID As Integer)

        Dim QRY As String =
        ("
            select
                person.weight,
                person.gender,
                person.date_of_birth,
            FROM 
	            person
            where 
	            person.id=" + PersonID.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
                        _age = New age(RDR("date_of_birth"))
                    Catch ex As Exception

                    End Try

                    Try
                        _weight = RDR("weight")
                    Catch ex As Exception
                        _weight = 0
                    End Try

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

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

    End Sub

    Public Sub UpdateWeight(PersonID As Integer, NewWeight As Double)

        ' If Person ID = 0 Just Dont Do any Thing
        If PersonID = 0 Then Exit Sub

        Dim QRY As String = ("

            /* Take The New Reading */

            SET @pid 		= " & PersonID.ToString & ";     /* Person ID  */
            SET @weight 	= " & NewWeight.ToString & ";  /* New Weight Reading */
            SET SQL_SAFE_UPDATES = 0;                   /* Disable Safe Mode For Updates */

            /* Update Person Weight */

            UPDATE person SET weight = @weight WHERE id = @pid;

            /* Update Participants Weight Classes */

            UPDATE
	            person
            LEFT JOIN
	            weight_divisions
                ON
                (YEAR(CURDATE()) - YEAR(person.date_of_birth) BETWEEN weight_divisions.minAge AND weight_divisions.maxAge) AND
                (weight_divisions.gender = person.gender) AND
                (person.weight > weight_divisions.minWeight AND person.weight <= weight_divisions.maxWeight)
            SET
                person.wp = weight_divisions.id,
	            person.category = weight_divisions.category,
                person.weight_Class = weight_divisions.className
            WHERE
                person.id = @pid;

            /* Take Snap Shot Copy Current Weight To Person Body As A History */

            INSERT INTO person_body (pid, weight, category, weight_class) SELECT id, weight, category, weight_class FROM person WHERE id = @pid;

            /* Enable Safe Mode For Updates */

            SET SQL_SAFE_UPDATES = 1;
        ")

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

        Try
            CON.Open()
            COM.ExecuteNonQuery()
            CON.Close()
        Catch ex As MySqlException
            MessageBox.Show(ex.Message)
        Finally
            CON.Dispose()
        End Try

    End Sub

End Class
