Imports System.Text.RegularExpressions
Imports System.IO.File
Imports System.IO

Public Class PresentationValidation


    Private _validateAddRecipeForm As ValidateAddRecipeForm

    Public Property ValidateAddRecipeFormInstance() As ValidateAddRecipeForm
        Get
            Return _validateAddRecipeForm
        End Get
        Set(ByVal value As ValidateAddRecipeForm)
            _validateAddRecipeForm = value
        End Set
    End Property

    Sub New()
        _validateAddRecipeForm = New ValidateAddRecipeForm
    End Sub

#Region "Add Recipe Form"

    Public Class ValidateAddRecipeForm

        Private _errorType As ErrorTypes
        Private _errorField As FieldNames
        Private _errorMessage As String


        Public Property CurrentErrorType() As ErrorTypes
            Get
                Return _errorType
            End Get
            Set(ByVal value As ErrorTypes)
                _errorType = value
            End Set
        End Property

        Public Property CurrentErrorField() As FieldNames
            Get
                Return _errorField
            End Get
            Set(ByVal value As FieldNames)
                _errorField = value
            End Set
        End Property

        Public Property CurrentErrorMessage() As String
            Get
                Return _errorMessage
            End Get
            Set(ByVal value As String)
                _errorMessage = value
            End Set
        End Property


        Public Enum ErrorTypes
            Type
            Bounds
            FilePathError
            NoErrors
            Null
            WroungFieldName
        End Enum

        Public Enum FieldNames
            RecipeName
            RecipePreparation
            RecipeDirections
            RecipeCookTime
            RecipePhoto
            RecipeTemperature
            None
            NutritionCalories
            NutritionCaloriesFromFat
            NutritionTotalFat
            NutritionSaturatedFat
            NutritionTransFat
            NutritionCholesteral
            NutritionSodium
            NutritionTotalCarbohydrates
            NutritionDietaryFiber
            NutritionSugar
            NutritionProtein
            NutritionVitaminA
            NutritionVitaminC
            NutritionCalcium
            NutritionIron
            RecipeIngredientAmount
            IngredientsName
            FoodTypesName
            CourseTypesName
            CuisineTypesName
            UnitMeasurementsName
        End Enum

        Public Sub New()
            CurrentErrorType = ErrorTypes.NoErrors
            CurrentErrorField = FieldNames.None
            CurrentErrorMessage = ""
        End Sub


        Public Sub validateIngredientAmount(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

            Dim txtbox As TextBox = CType(sender, TextBox)
            If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = vbBack Or e.KeyChar = "/" Then
                If e.KeyChar = "/" And txtbox.Text.Contains("/") Then  'Check for Duplicate  
                    txtbox.Text.Replace("/", "")
                    e.Handled = True
                End If
            Else
                e.Handled = True
            End If
        End Sub


        Public Sub textboxOnlyDigits(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

            Dim txtbox As TextBox = CType(sender, TextBox)

            If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = vbBack Then
                'Allow these characters

            Else
                'any other characters throw away
                e.Handled = True
            End If



        End Sub


        Public Function validateRecipeData(ByVal fieldName As FieldNames, ByVal valueToCheck As String, ByVal isRequired As Boolean) As Boolean
            Dim typeErrorCode As Integer = 0
            _errorField = fieldName
            _errorType = ErrorTypes.NoErrors

            'Check if a required field is null or empty
            If isRequired = True Then
                If String.IsNullOrEmpty(valueToCheck) Then
                    _errorType = ErrorTypes.Null
                    Return False
                End If
            End If
            'Check Recipe Directions
            If fieldName = FieldNames.RecipeDirections Then
                If valueToCheck.Length >= 1 AndAlso valueToCheck.Length <= 3000 Then
                    Return True
                Else
                    _errorType = ErrorTypes.Bounds
                    Return False
                End If
                'Check Recipe,Ingredient, Food Type, Course Type, Cuisine Type or Unit measurement names
            ElseIf fieldName = FieldNames.RecipeName Or fieldName = FieldNames.IngredientsName _
            Or fieldName = FieldNames.FoodTypesName Or fieldName = FieldNames.CourseTypesName _
            Or fieldName = FieldNames.CuisineTypesName Or fieldName = FieldNames.UnitMeasurementsName Then
                If valueToCheck.Length >= 1 AndAlso valueToCheck.Length <= 50 Then
                    Return True
                Else
                    _errorType = ErrorTypes.Bounds
                    Return False
                End If
                'validate the photo path if the file exists or the string is null then return true.
                'If not null or a valid file path return false.
            ElseIf fieldName = FieldNames.RecipePhoto Then
                If String.IsNullOrEmpty(valueToCheck) OrElse File.Exists(valueToCheck) Then
                    Return True
                Else
                    _errorType = ErrorTypes.FilePathError
                    Return False
                End If

                'Check Preparation time
            ElseIf fieldName = FieldNames.RecipePreparation Then
                'if it is null or empty 
                If String.IsNullOrEmpty(valueToCheck) Then
                    Return True
                End If

                If Integer.TryParse(valueToCheck, typeErrorCode) Then
                    If CInt(valueToCheck) >= 0 AndAlso CInt(valueToCheck) <= 100000 Then
                        Return True
                    Else
                        _errorType = ErrorTypes.Bounds
                        Return False
                    End If
                Else
                    _errorType = ErrorTypes.Type
                    Return False
                End If
                'Check Cook Time
            ElseIf fieldName = FieldNames.RecipeCookTime Then
                If Integer.TryParse(valueToCheck, typeErrorCode) Then
                    If CInt(valueToCheck) >= 0 AndAlso CInt(valueToCheck) <= 100000 Then
                        Return True
                    Else
                        _errorType = ErrorTypes.Bounds
                        Return False
                    End If
                Else
                    _errorType = ErrorTypes.Type
                    Return False
                End If
                'Check Temperature
            ElseIf fieldName = FieldNames.RecipeTemperature Then
                If Integer.TryParse(valueToCheck, typeErrorCode) = True Then
                    If CInt(valueToCheck) >= 0 AndAlso CInt(valueToCheck) <= 2000 Then
                        Return True
                    Else
                        _errorType = ErrorTypes.Bounds
                        Return False
                    End If
                Else
                    _errorType = ErrorTypes.Type
                    Return False
                End If
                'Check Vitamin A, Vitamin C, Calcium, or iron fields
            ElseIf fieldName = FieldNames.NutritionVitaminA Or fieldName = FieldNames.NutritionVitaminC Or _
                    fieldName = FieldNames.NutritionCalcium Or fieldName = FieldNames.NutritionIron Then
                If Integer.TryParse(valueToCheck, typeErrorCode) Then
                    If CInt(valueToCheck) >= 0 AndAlso CInt(valueToCheck) <= 999999 Then

                        Return True
                    Else
                        _errorType = ErrorTypes.Bounds
                        Return False

                    End If
                Else
                    _errorType = ErrorTypes.Type
                    Return False
                End If
                'Check Nutrition Fields
            ElseIf fieldName = FieldNames.NutritionCalories Or fieldName = FieldNames.NutritionCaloriesFromFat _
        Or fieldName = FieldNames.NutritionTotalFat Or fieldName = FieldNames.NutritionSaturatedFat _
        Or fieldName = FieldNames.NutritionTransFat Or fieldName = FieldNames.NutritionCholesteral _
        Or fieldName = FieldNames.NutritionSodium Or fieldName = FieldNames.NutritionTotalCarbohydrates _
        Or fieldName = FieldNames.NutritionDietaryFiber Or fieldName = FieldNames.NutritionSugar _
        Or fieldName = FieldNames.NutritionProtein Then
                Dim errCode As Double = 0
                If Double.TryParse(valueToCheck, errCode) Then

                    If CDbl(valueToCheck) >= 0 AndAlso CDbl(valueToCheck) <= 999999.0 Then
                        Return True
                    Else
                        _errorType = ErrorTypes.Bounds
                        Return False
                    End If
                Else
                    _errorType = ErrorTypes.Type
                    Return False
                End If
                'Check ingredient Amount
            ElseIf fieldName = FieldNames.RecipeIngredientAmount Then
                If CDec(valueToCheck) >= 0 AndAlso CDec(valueToCheck) <= 5000000000.0 Then
                    Return True

                Else
                    _errorType = ErrorTypes.Bounds
                    Return False

                End If
            Else
                _errorType = ErrorTypes.WroungFieldName
                Return False

            End If

        End Function
    End Class

#End Region


#Region "Log In Form"

    Public Shared Function validEMailAddress(ByVal emailAddress As String) As Boolean
        Const VALIDEMAIL As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
        If Regex.IsMatch(emailAddress, VALIDEMAIL) Then
            validEMailAddress = True
        Else
            validEMailAddress = False
        End If
    End Function

    ''' <summary>
    ''' Validates a username.
    '''Assuming a user already has an account and chooses to password protect the account....
    '''If username is not null or empty and is between the length of 1 and 50 chars, return true
    '''ELSE return false, its not a valid username.
    ''' </summary>
    ''' <param name="userName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function validUserName(ByVal userName As String) As Boolean
        If Not String.IsNullOrEmpty(userName) AndAlso userName.Length >= 1 AndAlso userName.Length <= 50 Then
            validUserName = True
        Else
            validUserName = False
        End If
    End Function

    Public Shared Function validPassword(ByVal password As String) As Boolean
        If Not String.IsNullOrEmpty(password) AndAlso password.Length >= 1 AndAlso password.Length <= 32 Then
            Return True
        Else
            Return False
        End If

    End Function

#End Region



  

End Class

