Imports Microsoft.SqlServer.Server
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports System.Data.Linq
Imports System.Data.Linq.SqlClient
Imports System.Linq

Public Class frmRecipeOverview

    'Validator to use to validate the data before sending it to entities tier
    Dim validator As New PresentationValidation()
    'The WCF service used to communicate between tiers
    Dim recipeService As New PresentationServiceReference.UserServicesClient


    Private Sub frmRecipeOverview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try
            retrieveRecipes()

            txtMaxPrepTime.Enabled = False
            cboCourseType.SelectedIndex = cboCourseType.FindString("None")
            cboCuisineType.SelectedIndex = cboCuisineType.FindString("None")

            txtRecipeName.Focus()
        Catch ex As SqlException
            MessageBox.Show(ex.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally

        End Try

    End Sub

    ''' <summary>
    ''' When this button is clicked, the dgvRecipes's datasource is cleared and set to the new search results
    ''' retrieved from the WCF service. The user inputs what criteria he/she wants to search for.
    ''' </summary>
    ''' <param name="sender">Object that raised the event</param>
    ''' <param name="e">Information about the event</param>
    ''' <remarks></remarks>
    Private Sub btnSearchRecords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchRecords.Click
       
        Try

            Dim prepMin As Nullable(Of Integer)
            Dim prepMax As Nullable(Of Integer)

            If txtMinPrepTime.Text.Length <= 0 Then
                prepMin = Nothing
            Else
                prepMin = CInt(txtMinPrepTime.Text)
            End If

            If txtMaxPrepTime.Text.Length <= 0 Then
                prepMin = Nothing
            Else
                prepMax = CInt(txtMaxPrepTime.Text)
            End If

            recipeService.searchRecipeIngredientTable(RecipeHolderDatabaseDataSet, txtRecipeName.Text, _
                                                     txtIngredient.Text, cboCourseType.Text, _
                                                      cboCuisineType.Text, prepMin, prepMax)

            RetrieveRecipeTableBindingSource.DataSource = RecipeHolderDatabaseDataSet

        Catch ex As SqlException
            MessageBox.Show(ex.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' When this button is clicked, the dgvRecipes's datasource is cleared and set to the new search results
    ''' retrieved from the WCF service. WCF service retrieves all of the recipes under the current account id.
    ''' </summary>
    ''' <param name="sender">Object that raised the event</param>
    ''' <param name="e">Information about the event</param>
    ''' <remarks></remarks>
    Private Sub btnRetrieveAllRecords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrieveAllRecords.Click

        Try
            retrieveRecipes()

        Catch ex As SqlException
            MessageBox.Show(ex.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub retrieveIngredients()
        Try
            RecipeHolderDatabaseDataSet.Ingredients.Merge(recipeService.retrieveIngredients())

        Catch ex As SqlException
            Throw ex
        Catch ex As Exception
            Throw ex
        End Try


    End Sub

    Private Sub retrieveCourses()
        Try
            RecipeHolderDatabaseDataSet.Courses.Merge(recipeService.retrieveCourses)

        Catch ex As SqlException
            Throw ex
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub retrieveFoodTypes()
        Try
            RecipeHolderDatabaseDataSet.FoodTypes.Merge(recipeService.retrieveFoodTypes)

        Catch ex As SqlException
            Throw ex
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub retrieveCuisines()
        Try
            RecipeHolderDatabaseDataSet.CuisineTypes.Merge(recipeService.retrieveCuisines)

        Catch ex As SqlException
            Throw ex
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub retrieveUnitMeasurements()
        Try
            RecipeHolderDatabaseDataSet.UnitMeasurements.Merge(recipeService.retrieveUnitMeasurements)

        Catch ex As SqlException
            Throw ex
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub retrieveRecipes()
        Try
            retrieveIngredients()
            retrieveCourses()
            retrieveCuisines()
            retrieveFoodTypes()
            retrieveUnitMeasurements()

            'Get records from database
            recipeService.retrieveRecipeIngredientTableByAccount(RecipeHolderDatabaseDataSet, modGlobals.CurrentAccountID)
            'Reset the binding
            RetrieveRecipeTableBindingSource.DataSource = RecipeHolderDatabaseDataSet
            RetrieveRecipeTableBindingSource.DataMember = "RetrieveRecipeTable"
        Catch ex As SqlException
            Throw ex
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Private Sub cbBetweenPrepTime_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbBetweenPrepTime.CheckedChanged
        'Flip on the criteria to search for a range of preperation times or to do a single numeric search
        If cbBetweenPrepTime.Checked = True Then
            txtMaxPrepTime.Enabled = True
        Else
            txtMaxPrepTime.Enabled = False
        End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    ''' <summary>
    ''' Allows only digits in a textbox
    ''' </summary>
    ''' <param name="sender">Object that raised the event</param>
    ''' <param name="e">Information about the evnet</param>
    ''' <remarks></remarks>
    Private Sub allowOnlyDigits_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMinPrepTime.KeyPress, txtMaxPrepTime.KeyPress
        validator.ValidateAddRecipeFormInstance.textboxOnlyDigits(sender, e)

    End Sub


End Class