Imports System.ComponentModel
Imports System.IO
Imports MySql.Data.MySqlClient

Public Class frm_friendly_match

    Public FriendlyMatch As friendly_match
    Public red As friendly_fighter
    Public blue As friendly_fighter
    Public referee As person

    Private Sub frm_new_match_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        SubPopulateComboBox(GetPersonList(), cbo_blue)
        SubPopulateComboBox(GetPersonList(), cbo_referee)
        SubPopulateComboBox(GetPersonList(), cbo_red)

    End Sub

    Private Sub cbo_blue_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbo_blue.SelectedIndexChanged

        Try

            blue = New friendly_fighter(cbo_blue.SelectedValue)

            Try
                pb_blue_picture.Image = Image.FromStream(blue.Person.PICTURE)
            Catch ex As Exception
                pb_blue_picture.Image = Nothing
            End Try

            Try
                pb_blue_flag.Image = Image.FromStream(blue.Person.TEAM.Country.Flag)
            Catch ex As Exception
                pb_blue_flag.Image = Nothing
            End Try

        Catch ex As Exception

        End Try

    End Sub

    Private Sub cbo_red_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbo_red.SelectedIndexChanged

        Try

            red = New friendly_fighter(cbo_red.SelectedValue)

            Try
                pb_red_picture.Image = Image.FromStream(red.Person.PICTURE)
            Catch ex As Exception
                pb_red_picture.Image = Nothing
            End Try

            Try
                pb_red_flag.Image = Image.FromStream(red.Person.TEAM.Country.Flag)
            Catch ex As Exception
                pb_red_flag.Image = Nothing
            End Try

        Catch ex As Exception

        End Try

    End Sub

    Private Sub cbo_referee_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbo_referee.SelectedIndexChanged

        Try

            referee = New person(cbo_referee.SelectedValue)

            Try
                pb_referee_picture.Image = Image.FromStream(referee.PICTURE)
            Catch ex As Exception
                pb_referee_picture.Image = Nothing
            End Try

            Try
                pb_referee_flag.Image = Image.FromStream(referee.TEAM.Country.Flag)
            Catch ex As Exception
                pb_referee_flag.Image = Nothing
            End Try

        Catch ex As Exception

        End Try

    End Sub

    Private Sub btn_back_Click(sender As Object, e As EventArgs) Handles btn_back.Click

        frm_main.Show()
        Me.Close()

    End Sub

    Private Sub frm_new_match_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing

        frm_main.Show()

    End Sub

    Private Sub pb_blue_picture_Click(sender As Object, e As EventArgs) Handles pb_blue_picture.Click

        If IsNothing(pb_blue_picture.Image) Then

            ' Open Picture File
            ResizePicture(pb_blue_picture, 500, 500)

            ' Store The Picture
            blue.Person.UpdatePicture(pb_blue_picture)

        ElseIf Not IsNothing(pb_blue_picture.Image) Then

            Dim iPictureUpdate As DialogResult = MessageBox.Show("Would You Like To Update The Players Picture ?", "Picture Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

            If iPictureUpdate = DialogResult.Yes Then

                ' Open Picture File
                ResizePicture(pb_blue_picture, 500, 500)

                ' Store The Picture
                blue.Person.UpdatePicture(pb_blue_picture)

            End If

        End If

    End Sub

    Private Sub pb_referee_picture_Click(sender As Object, e As EventArgs) Handles pb_referee_picture.Click

        If IsNothing(pb_referee_picture.Image) Then

            ' Open Picture File
            ResizePicture(pb_referee_picture, 500, 500)

            ' Store The Picture
            referee.UpdatePicture(pb_referee_picture)

        ElseIf Not IsNothing(pb_referee_picture.Image) Then

            Dim iPictureUpdate As DialogResult = MessageBox.Show("Would You Like To Update The Players Picture ?", "Picture Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

            If iPictureUpdate = DialogResult.Yes Then

                ' Open Picture File
                ResizePicture(pb_referee_picture, 500, 500)

                ' Store The Picture
                referee.UpdatePicture(pb_referee_picture)

            End If

        End If

    End Sub

    Private Sub pb_red_picture_Click(sender As Object, e As EventArgs) Handles pb_red_picture.Click

        If IsNothing(pb_red_picture.Image) Then

            ' Open Picture File
            ResizePicture(pb_red_picture, 500, 500)

            ' Store The Picture
            red.Person.UpdatePicture(pb_red_picture)

        ElseIf Not IsNothing(pb_red_picture.Image) Then

            Dim iPictureUpdate As DialogResult = MessageBox.Show("Would You Like To Update The Players Picture ?", "Picture Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

            If iPictureUpdate = DialogResult.Yes Then

                ' Open Picture File
                ResizePicture(pb_red_picture, 500, 500)

                ' Store The Picture
                red.Person.UpdatePicture(pb_red_picture)

            End If

        End If

    End Sub

    Private Sub btn_start_Click(sender As Object, e As EventArgs) Handles btn_start.Click

        ' checking routine
        If CheckEntry() = True Then
            FriendlyMatch = New friendly_match(blue.Person.ID, red.Person.ID, referee.ID, 3)
            frm_friendly_scorecontrol.Show()
            Me.Hide()
        End If

    End Sub

    Private Function CheckEntry() As Boolean

        If blue.Person.ID = red.Person.ID Then
            MsgBox("Blue & Red Fighters Can't Be The Same Person", vbOKOnly + vbCritical, "Fighters Selection Error")
            Return False
        ElseIf blue.Person.ID = referee.ID Then
            MsgBox("Blue Fighter & Referee Can't Be The Same Person", vbOKOnly + vbCritical, "Fighter Referee Selection Error")
            Return False
        ElseIf red.Person.ID = referee.ID Then
            MsgBox("Red Fighter & Referee Cant Be The Same Person", vbOKOnly + vbCritical, "Fighter Referee Selection Error")
            Return False
        Else
            Return True
        End If

    End Function

End Class