Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Logging

Public Class Validator
    Inherits BaseClass

    Public Sub New(pLogConfig As LogConfig)
        MyBase.New(pLogConfig)
    End Sub

    Public Class ControlRow
        Public Id As Integer
        Public IndexName As String
        Public IsRequired As Boolean
        Public [ReadOnly] As Boolean
        Public ValidationSql As String
        Public RegexMatch As String
        Public RegexMessage As String
        Public OverwriteData As Boolean
        Public SaveChangeEnabled As Boolean
    End Class

    Public Function ConvertToControlRow(pDataRow As DataRow) As ControlRow
        Return New ControlRow With {
            .Id = pDataRow.ItemEx("GUID", 0),
            .IndexName = pDataRow.ItemEx("INDEX_NAME", ""),
            .IsRequired = pDataRow.ItemEx("VALIDATION", False),
            .[ReadOnly] = pDataRow.ItemEx("READ_ONLY", False),
            .ValidationSql = pDataRow.ItemEx("SQL_UEBERPRUEFUNG", ""),
            .RegexMatch = pDataRow.ItemEx("REGEX_MATCH", ""),
            .RegexMessage = pDataRow.ItemEx("REGEX_MESSAGE_DE", ""),
            .OverwriteData = pDataRow.ItemEx("OVERWRITE_DATA", False),
            .SaveChangeEnabled = pDataRow.ItemEx("SAVE_CHANGE_ON_ENABLED", True)
        }
    End Function

    'Public Function CheckTextbox(oControl As Control, pControlRow As DataRow)
    'Try
    '        Dim oMissing = False
    '        Dim oErrMsgMissingInput = ""
    '        Dim oControlRow = ConvertToControlRow(pControlRow)

    '        'Dim oWrongInputMessage = ClassAllgemeineFunktionen.GUI_LANGUAGE_INFO("frmValidator.WrongInputControl")
    '        Dim oWrongInputMessage = S.Falsche_Eingabe

    '        If oControlRow.RegexMatch <> String.Empty AndAlso Not Regex.IsMatch(oControl.Text, oControlRow.RegexMatch) Then
    '            oMissing = True

    '            oErrMsgMissingInput = oWrongInputMessage & " textbox '" & oControl.Name & "'"

    '            Logger.Warn(oErrMsgMissingInput)
    '            If oControlRow.RegexMessage <> String.Empty Then
    '                oErrMsgMissingInput &= ":" & vbCrLf & oControlRow.RegexMessage
    '            End If

    '            oControl.BackColor = Color.Red

    '            'TODO
    '            'Exit For
    '        End If

    '        'as erstes überprüfen ob überhaupt etwas eingetragen worden ist
    '        If Check_Missing_Control_Value(oControl, "txt") = True And oControlRow.IsRequired = True Then 'NICHTS EINGETRAGEN
    '            oMissing = True
    '            oErrMsgMissingInput = oWrongInputMessage & " textbox '" & oControl.Name & "'"
    '            Logger.Warn(oErrMsgMissingInput)
    '            oControl.BackColor = Color.Red

    '            'TODO
    '            'Exit For
    '        Else
    '            Dim oTextEdit As TextEdit = DirectCast(oControl, TextEdit)
    '            oMyInput = ClassFormat.GetStringValue(oTextEdit.EditValue)

    '            'den aktuellen Wert in windream auslesen
    '            Dim oSourceValue = GetVariableValuefromSource(oIndexName, oIDBTyp)


    '            If oIndexName.StartsWith("[%VKT") Then
    '                oSourceValue = ReturnVektor_IndexValue(oIndexName)
    '            Else
    '                'wertWD = CURRENT_WMFILE.GetVariableValue(oIndexName)
    '                If Not IsNothing(oSourceValue) Then
    '                    If oSourceValue.ToString = "System.Object[]" Then
    '                        If oSourceValue.Length = 1 Then
    '                            oSourceValue = oSourceValue(0)
    '                        Else '
    '                            Logger.Info(" >> Vectorfield " & oIndexName & "' contains more then one value - First value will be used")
    '                            oSourceValue = oSourceValue(0)
    '                        End If
    '                    End If
    '                Else
    '                    oSourceValue = ""
    '                End If
    '            End If
    '            Dim oSetValue As Boolean = False
    '            If IsDBNull(oSourceValue) Then
    '                oSetValue = True
    '            End If
    '            If oSetValue = False Then
    '                If IsNothing(oSourceValue) Then
    '                    oSetValue = True
    '                End If
    '            End If
    '            If oSetValue = False Then
    '                Try
    '                    If oSourceValue <> oMyInput Then
    '                        oSetValue = True
    '                    End If
    '                Catch ex As Exception
    '                    oSetValue = True
    '                End Try

    '            End If
    '            'wenn Wert in Windream <> der Eingabe darf indexiert werden
    '            If oSetValue = True Then
    '                'Wenn der Wert in ein Vektorfeld geschrieben wird
    '                If oIndexName.StartsWith("[%VKT") Then
    '                    oMyInput = Return_PM_VEKTOR(oMyInput, oIndexName)
    '                    'Hier muss nun separat as Vektorfeld indexiert werden
    '                    If WMIndexVectofield(oMyInput, PROFIL_VEKTORINDEX) = True Then
    '                        oMissing = True
    '                        oErrMsgMissingInput = "Error while indexing textbox as VEKTOR - ERROR: " & idxerr_message
    '                        Logger.Warn(oErrMsgMissingInput)
    '                        Exit For
    '                    End If
    '                Else
    '                    If IDB_ACTIVE = False Then
    '                        Dim result() As String
    '                        ReDim Preserve result(0)
    '                        result(0) = oMyInput
    '                        If Indexiere_File(CURRENT_WMFILE, oIndexName, result) = False Then
    '                            oMissing = True
    '                            oErrMsgMissingInput = "Error while indexing Textbox - ERROR: " & idxerr_message
    '                            Logger.Warn(oErrMsgMissingInput)
    '                            Exit For
    '                        End If
    '                    Else
    '                        If IDBData.SetVariableValue(oIndexName, oMyInput) = False Then
    '                            Exit For
    '                        End If
    '                    End If
    '                    If IDB_ACTIVE = False Then
    '                        If PROFIL_LOGINDEX <> "" Then
    '                            Dim oMyLogString = Return_LOGString(oMyInput, oSourceValue, oIndexName)
    '                            WMIndexVectofield(oMyLogString, PROFIL_LOGINDEX)
    '                            'Else
    '                            'IDBData.SetVariableValue(PROFIL_LOGINDEX, oMyLogString)


    '                        End If
    '                    End If

    '                End If



    '            End If
    '        End If
    '    Catch ex As Exception
    '        oErrMsgMissingInput = "Unexpected error in Check_UpdateIndexe TextBox '" & oControl.Name & "' - Check the log"
    '        Logger.Error(ex)
    '        Dim st As New StackTrace(True)
    '        st = New StackTrace(ex, True)
    '        LOGGER.Warn("⚠️ Unexpected error in Check_UpdateIndexe  TextBox :" & ex.Message, True)
    '        Return False
    '    End Try
    'End Function
End Class
