Imports EntitiesLayer

Public Class frmAddIngredientTypes
    Dim recipeService As New PresentationServiceReference.UserServicesClient
    Dim ingredientChange As Boolean = False
    Dim courseChange As Boolean = False
    Dim cuisineChange As Boolean = False
    Dim unitMeasurementChange As Boolean = False
    Dim foodTypeChange As Boolean = False
    Dim formLoaded As Boolean = False

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

        'Fill the typed dataset tables that we will use with data 
        RecipeHolderDatabaseDataSet.FoodTypes.Merge(recipeService.retrieveFoodTypes)
        RecipeHolderDatabaseDataSet.Ingredients.Merge(recipeService.retrieveIngredients)
        RecipeHolderDatabaseDataSet.ViewIngredientsFoodTypes.Merge(recipeService.viewIngredientsFoodTypes)
        RecipeHolderDatabaseDataSet.Courses.Merge(recipeService.retrieveCourses)
        RecipeHolderDatabaseDataSet.UnitMeasurements.Merge(recipeService.retrieveUnitMeasurements)
        RecipeHolderDatabaseDataSet.CuisineTypes.Merge(recipeService.retrieveCuisines)
        formLoaded = True
        'set the update buttons to not enabled
        btnUnitMeasurementUpdate.Enabled = False
        btnCourseTypeUpdate.Enabled = False
        btnIngredientUpdate.Enabled = False
        btnCuisineTypeUpdate.Enabled = False
        btnFoodTypeUpdate.Enabled = False

        'Change color of None so it is known that this row cannot be deleted
        For Each dgvRow As DataGridViewRow In dgvAddCourseType.Rows

            If CStr(dgvRow.Cells("colName").Value) = "None" Then
                dgvRow.DefaultCellStyle.BackColor = Color.LightBlue
            End If
        Next

        For Each dgvRow As DataGridViewRow In dgvAddFoodType.Rows

            If CStr(dgvRow.Cells("colFoodType").Value) = "None" Then
                dgvRow.DefaultCellStyle.BackColor = Color.LightBlue
            End If
        Next
        For Each dgvRow As DataGridViewRow In dgvCuisineType.Rows

            If CStr(dgvRow.Cells("colCuisineName").Value) = "None" Then
                dgvRow.DefaultCellStyle.BackColor = Color.LightBlue
            End If
        Next

        For Each dgvRow As DataGridViewRow In dgvUnitMeasurement.Rows

            If CStr(dgvRow.Cells("colUnitName").Value) = "None" Then
                dgvRow.DefaultCellStyle.BackColor = Color.LightBlue
            End If
        Next

    End Sub


    '************Ingredients*****************
    Private Sub btnIngredientAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIngredientAdd.Click
        'Create Ingredient row
        Dim ingredientRows As EntitiesLayer.RecipeHolderDatabaseDataSet.IngredientsRow
        Try

       
            'Add items to the ingredient row and then add it to Ingredients table
            ingredientRows = RecipeHolderDatabaseDataSet.Ingredients.NewIngredientsRow
            ingredientRows.FoodTypeID = CInt(cboFoodType.SelectedValue)
            ingredientRows.Name = txtIngredientName.Text
            RecipeHolderDatabaseDataSet.Ingredients.AddIngredientsRow(ingredientRows)

            'Create viewIngredientFoodType row
            Dim viewIngredientFoodTypeRow As EntitiesLayer.RecipeHolderDatabaseDataSet.ViewIngredientsFoodTypesRow
            viewIngredientFoodTypeRow = RecipeHolderDatabaseDataSet.ViewIngredientsFoodTypes.NewViewIngredientsFoodTypesRow

            'Add items to the viewIngredientFoodType row and then add it to ViewIngredientsFoodTypes table
            viewIngredientFoodTypeRow.FoodTypeID = CInt(cboFoodType.SelectedValue)
            viewIngredientFoodTypeRow.FoodTypeName = cboFoodType.Text
            viewIngredientFoodTypeRow.IngredientName = txtIngredientName.Text

            RecipeHolderDatabaseDataSet.ViewIngredientsFoodTypes.AddViewIngredientsFoodTypesRow(viewIngredientFoodTypeRow)
            ingredientChange = True

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

    Private Sub ViewIngredientsFoodTypesDataGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ViewIngredientsFoodTypesDataGridView.CellValueChanged
        If formLoaded And ViewIngredientsFoodTypesDataGridView.IsCurrentCellDirty = True Then
            Dim rowToUpdate As RecipeHolderDatabaseDataSet.IngredientsRow


            rowToUpdate = RecipeHolderDatabaseDataSet.Ingredients.FindByIngredientID(CInt(ViewIngredientsFoodTypesDataGridView.CurrentRow.Cells("colIngredientID").Value))

            rowToUpdate.Name = CStr(ViewIngredientsFoodTypesDataGridView.CurrentRow.Cells("colIngredientName").Value)
            rowToUpdate.FoodTypeID = CInt(ViewIngredientsFoodTypesDataGridView.CurrentRow.Cells("colFoodTypeID").Value)

            ingredientChange = True
            btnIngredientUpdate.Enabled = True

        End If
    End Sub

    Private Sub ViewIngredientsFoodTypesDataGridView_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles ViewIngredientsFoodTypesDataGridView.UserDeletingRow
        Dim rowToDelete As RecipeHolderDatabaseDataSet.IngredientsRow = RecipeHolderDatabaseDataSet.Ingredients.NewIngredientsRow

        'Delete row from the ingredients table, and set the ingredient changed flag to true
        rowToDelete = RecipeHolderDatabaseDataSet.Ingredients.FindByIngredientID(CInt(e.Row.Cells("colIngredientID").Value))
        If Not rowToDelete.RowState = DataRowState.Added Then
            rowToDelete.Delete()
            btnIngredientUpdate.Enabled = True
            ingredientChange = True
        End If
        

        
    End Sub

    Private Sub btnIngredientUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIngredientUpdate.Click
        Try
            'If the ingredients datagridview has any changes
            'Then go ahead with the update and if the rows get updated
            'Message yes
            'Else the rows didnt get updated
            If RecipeHolderDatabaseDataSet.Ingredients.HasErrors Or RecipeHolderDatabaseDataSet.HasChanges Then

            End If
            If ingredientChange = True Then
                ' ViewIngredientsFoodTypesDataGridView.EndEdit()
                If recipeService.updateIngredients(RecipeHolderDatabaseDataSet) = True Then
                    RecipeHolderDatabaseDataSet.Ingredients.AcceptChanges()
                    MessageBox.Show("Ingredients updated")
                    ingredientChange = False
                Else
                    MessageBox.Show("Ingredients Not Updated")
                End If
            End If


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

    End Sub


    '************Course Type*****************
    Private Sub btnCourseTypeAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCourseTypeAdd.Click

        Dim courseRow As RecipeHolderDatabaseDataSet.CoursesRow = RecipeHolderDatabaseDataSet.Courses.NewCoursesRow


        For Each dgvRow As DataGridViewRow In dgvAddCourseType.Rows
            If CStr(dgvRow.Cells("colName").Value) = txtCourseName.Text Then
                MessageBox.Show("Course already exist in the table", "Course Already Exists", MessageBoxButtons.OK)
                Exit Sub
            End If
        Next

        'Add the new text to course row, change the courseChanged flag to true 
        'Enable the update button and set the textbox to no text
        courseRow.Name = txtCourseName.Text
        RecipeHolderDatabaseDataSet.Courses.AddCoursesRow(courseRow)
        courseChange = True
        btnCourseTypeUpdate.Enabled = True
        txtCourseName.Text = ""
    End Sub
    
    Private Sub btnCourseTypeUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCourseTypeUpdate.Click
        Try
            'If the Courses datagridview has any changes
            'Then go ahead with the update and if the rows get updated
            'Message yes
            'Else the rows didnt get updated
            If courseChange = True Then
                If recipeService.updateCourses(RecipeHolderDatabaseDataSet) = True Then
                    RecipeHolderDatabaseDataSet.Courses.AcceptChanges()
                    MessageBox.Show("Courses updated")
                    courseChange = False
                    btnCourseTypeUpdate.Enabled = False
                Else
                    MessageBox.Show("Courses Not Updated")
                End If
            End If

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

    Private Sub dgvAddCourseType_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgvAddCourseType.CellBeginEdit
        'Make sure that the None row is only readonly
        If CStr(dgvAddCourseType.CurrentRow.Cells("colName").Value) = "None" Then
            e.Cancel = True
        End If

    End Sub

    Private Sub dgvAddCourseType_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvAddCourseType.CellValueChanged
        'If the value has changed, tell the flag the record has been edited and enable update button
        
        If dgvAddCourseType.IsCurrentCellDirty Then
            courseChange = True
            btnCourseTypeUpdate.Enabled = True
        End If
    End Sub


    Private Sub dgvAddCourseType_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgvAddCourseType.UserDeletingRow
        
        'Make sure the None tag isn't deleted, 
        'if its a different row then set flag to edited and enable update button
        If CStr(dgvAddCourseType.CurrentRow.Cells("colName").Value) = "None" Then
            e.Cancel = True
        Else
            courseChange = True
            btnCourseTypeUpdate.Enabled = True
        End If
    End Sub


    '************Food Type*****************
    Private Sub btnFoodTypeAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFoodTypeAdd.Click

        Dim foodTypeRow As RecipeHolderDatabaseDataSet.FoodTypesRow = RecipeHolderDatabaseDataSet.FoodTypes.NewFoodTypesRow


        For Each dgvRow As DataGridViewRow In dgvAddFoodType.Rows
            If CStr(dgvRow.Cells("colFoodType").Value) = txtFoodType.Text Then
                MessageBox.Show("Food Type already exist in the table", "Food Type Already Exists", MessageBoxButtons.OK)
                Exit Sub
            End If
        Next

        'Add the new text to foodType row, change the foodTypeChange flag to true 
        'Enable the update button and set the textbox to no text
        foodTypeRow.Name = txtFoodType.Text
        RecipeHolderDatabaseDataSet.FoodTypes.AddFoodTypesRow(foodTypeRow)
        foodTypeChange = True
        btnFoodTypeUpdate.Enabled = True
        txtFoodType.Text = ""

    End Sub

    Private Sub btnFoodTypeUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFoodTypeUpdate.Click
        Try
            'If the foodtype datagridview has any changes
            'Then go ahead with the update and if the rows get updated
            'Message yes
            'Else the rows didnt get updated
            If foodTypeChange = True Then
                If recipeService.updateFoodTypes(RecipeHolderDatabaseDataSet) = True Then
                    RecipeHolderDatabaseDataSet.FoodTypes.AcceptChanges()
                    MessageBox.Show("Food Type updated")
                    foodTypeChange = False
                    btnFoodTypeUpdate.Enabled = False
                Else
                    MessageBox.Show("Food Type Not Updated")
                End If
            End If

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

    Private Sub dgvAddFoodType_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgvAddFoodType.CellBeginEdit
        'Make sure that the None row is only readonly
        If CStr(dgvAddFoodType.CurrentRow.Cells("colFoodType").Value) = "None" Then
            e.Cancel = True
        End If
    End Sub

    Private Sub dgvAddFoodType_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvAddFoodType.CellValueChanged
        If dgvAddFoodType.IsCurrentCellDirty Then
            foodTypeChange = True
            btnFoodTypeUpdate.Enabled = True
        End If
    End Sub

    Private Sub dgvAddFoodType_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgvAddFoodType.UserDeletingRow
       
        'Make sure the None tag isn't deleted, 
        'if its a different row then set flag to edited and enable update button
        If CStr(dgvAddFoodType.CurrentRow.Cells("colFoodType").Value) = "None" Then
            e.Cancel = True
        Else
            foodTypeChange = True
            btnFoodTypeUpdate.Enabled = True

        End If
    End Sub

    '************Cuisine Type*****************
    Private Sub btnCuisineTypeAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCuisineTypeAdd.Click
        Dim cuisineTypeRow As RecipeHolderDatabaseDataSet.CuisineTypesRow = RecipeHolderDatabaseDataSet.CuisineTypes.NewCuisineTypesRow

        For Each cuisineType As DataGridViewRow In dgvCuisineType.Rows
            If txtCuisineType.Text = cuisineType.Cells("colCuisineName").Value.ToString Then
                MessageBox.Show("Cuisine Type already exist", "Cuisine Type Already Exists", MessageBoxButtons.OK)
                Exit Sub
            End If
        Next

        'Add the new text to cuisine row, change the cuisineChange flag to true 
        'Enable the update button and set the textbox to no text
        cuisineTypeRow.Name = txtCuisineType.Text
        RecipeHolderDatabaseDataSet.CuisineTypes.AddCuisineTypesRow(cuisineTypeRow)
        cuisineChange = True
        btnCuisineTypeUpdate.Enabled = True
        txtCuisineType.Text = ""
    End Sub

    Private Sub dgvCuisineType_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgvCuisineType.CellBeginEdit
        'Make sure that the None row is only readonly
        If CStr(dgvCuisineType.CurrentRow.Cells("colCuisineName").Value) = "None" Then
            e.Cancel = True
        End If
    End Sub

    Private Sub dgvCuisineType_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvCuisineType.CellValueChanged
        If dgvCuisineType.IsCurrentCellDirty Then
            cuisineChange = True
            btnCuisineTypeUpdate.Enabled = True
        End If
    End Sub

    Private Sub dgvCuisineType_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgvCuisineType.UserDeletingRow
        'Make sure the None tag isn't deleted, 
        'if its a different row then set flag to edited and enable update button
        If CStr(dgvCuisineType.CurrentRow.Cells("colCuisineName").Value) = "None" Then
            e.Cancel = True
        Else
            cuisineChange = True
            btnCuisineTypeUpdate.Enabled = True

        End If

    End Sub

    Private Sub btnCuisineTypeUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCuisineTypeUpdate.Click
        Try
            'If the foodtype datagridview has any changes
            'Then go ahead with the update and if the rows get updated
            'Message yes
            'Else the rows didnt get updated
            If cuisineChange = True Then
                If recipeService.updateCuisines(RecipeHolderDatabaseDataSet) = True Then
                    RecipeHolderDatabaseDataSet.CuisineTypes.AcceptChanges()
                    MessageBox.Show("Cuisine Type updated")
                    cuisineChange = False
                    btnCuisineTypeUpdate.Enabled = False
                Else
                    MessageBox.Show("Cuisine Type Not Updated")
                End If
            End If

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

    '************Unit Measurement*****************

    Private Sub btnUnitMeasurementAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnitMeasurementAdd.Click
        Dim unitTypeRow As RecipeHolderDatabaseDataSet.UnitMeasurementsRow = RecipeHolderDatabaseDataSet.UnitMeasurements.NewUnitMeasurementsRow

        For Each unitType As DataGridViewRow In dgvUnitMeasurement.Rows
            If txtUnitMeasurement.Text = unitType.Cells("colUnitName").Value.ToString Then
                MessageBox.Show("Unit Measurement Type already exist")
                Exit Sub
            End If
        Next

        'Add the new text to cuisine row, change the cuisineChange flag to true 
        'Enable the update button and set the textbox to no text
        unitTypeRow.Name = txtUnitMeasurement.Text
        RecipeHolderDatabaseDataSet.UnitMeasurements.AddUnitMeasurementsRow(unitTypeRow)
        unitMeasurementChange = True
        btnUnitMeasurementUpdate.Enabled = True
        txtUnitMeasurement.Text = ""
    End Sub

    Private Sub btnUnitMeasurementUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnitMeasurementUpdate.Click
        Try
            'If the foodtype datagridview has any changes
            'Then go ahead with the update and if the rows get updated
            'Message yes
            'Else the rows didnt get updated
            If unitMeasurementChange = True Then
                If recipeService.updateUnitMeasurements(RecipeHolderDatabaseDataSet) = True Then
                    RecipeHolderDatabaseDataSet.UnitMeasurements.AcceptChanges()
                    MessageBox.Show("Unit Measurement updated")
                    unitMeasurementChange = False
                    btnUnitMeasurementUpdate.Enabled = False
                Else
                    MessageBox.Show("Unit Measurement Not Updated")
                End If
            End If


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

    Private Sub dgvUnitMeasurement_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgvUnitMeasurement.CellBeginEdit
        'Make sure that the None row is only readonly
        If CStr(dgvUnitMeasurement.CurrentRow.Cells("colUnitName").Value) = "None" Then
            e.Cancel = True
        End If
    End Sub

    Private Sub dgvUnitMeasurement_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvUnitMeasurement.CellValueChanged
        If dgvUnitMeasurement.IsCurrentCellDirty Then
            unitMeasurementChange = True
            btnUnitMeasurementUpdate.Enabled = True
        End If
    End Sub

    Private Sub dgvUnitMeasurement_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgvUnitMeasurement.UserDeletingRow
        'Make sure the None tag isn't deleted, 
        'if its a different row then set flag to edited and enable update button
        If CStr(dgvUnitMeasurement.CurrentRow.Cells("colUnitName").Value) = "None" Then
            e.Cancel = True

        Else
            unitMeasurementChange = True
            btnUnitMeasurementUpdate.Enabled = True
        End If
        

    End Sub


    Public Sub dataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvAddCourseType.CellValidating, _
                                                                    dgvAddFoodType.CellValidating, dgvCuisineType.CellValidating, _
                                                                    dgvUnitMeasurement.CellValidating, ViewIngredientsFoodTypesDataGridView.CellValidating
        'if the string that was modified is empty then don't allow edit
        If String.IsNullOrEmpty(CStr(e.FormattedValue)) Then
            e.Cancel = True
        End If
    End Sub
End Class