Imports DigitalData.Controls.LookupGrid
Imports DigitalData.GUIs.ZooFlow.Base
Imports DigitalData.GUIs.ZooFlow.frmGlobix_Index
Imports DigitalData.Modules.EDMI.API
Imports DigitalData.Modules.EDMI.API.EDMIServiceReference
Imports DigitalData.Modules.Logging

Public Class ClassValidator
    Inherits BaseClass

    Private ReadOnly Client As Client

    Public Sub New(pLogConfig As LogConfig, pClient As Client)
        MyBase.New(pLogConfig)
        Client = pClient
    End Sub

    Function ValidateControls(pPanel As Panel, pDocType As DocType) As Boolean
        Try
            Logger.Debug("Starting [ValidateControls]")
            Dim result As Boolean = True

            For Each oControl As Control In pPanel.Controls

                ' ========================= TEXT BOX =========================
                If oControl.Name.StartsWith("txt") Then
                    Dim oTextBox As DevExpress.XtraEditors.TextEdit = oControl
                    If oTextBox.Text = "" Then
                        Dim oIndexName = GetIndexName(oTextBox, "txt")
                        Dim oOptional = TestIsIndexOptional(pDocType, oIndexName)

                        If oOptional = False Then
                            ShowValidationMessage()
                            oTextBox.Focus()
                            Return False
                        End If
                    End If
                End If

                ' ========================= LOOKUP =========================
                If oControl.Name.StartsWith("cmbMulti") Then
                    Dim oLookup = DirectCast(oControl, LookupControl3)
                    Dim oValues As List(Of String) = oLookup.Properties.SelectedValues

                    If oValues.Count = 0 Then
                        Dim oIndexName = GetIndexName(oLookup, "cmbMulti")
                        Dim oOptional = TestIsIndexOptional(pDocType, oIndexName)

                        If oOptional = False Then
                            ShowValidationMessage()
                            oLookup.Focus()
                            Return False
                        End If
                    End If
                End If

                ' ========================= COMBO BOX =========================
                If oControl.Name.StartsWith("cmbSingle") Then
                    Dim cmbSingle As TextBox = oControl

                    If cmbSingle.Text = "" Then
                        Dim oIndexName = GetIndexName(cmbSingle, "cmbSingle")
                        Dim oOptional = TestIsIndexOptional(pDocType, oIndexName)

                        If oOptional = False Then
                            ShowValidationMessage()
                            cmbSingle.Focus()
                            Return False
                        End If
                    End If
                End If

                If oControl.Name.StartsWith("cmb") Then
                    Dim cmb As ComboBox = oControl
                    If cmb.Text = "" Then
                        Dim oIndexName = GetIndexName(cmb, "cmb")
                        Dim oOptional = TestIsIndexOptional(pDocType, oIndexName)

                        If oOptional = False Then
                            ShowValidationMessage()
                            cmb.Focus()
                            Return False
                        End If
                    End If
                End If

                ' ========================= DATE PICKER =========================
                If oControl.Name.StartsWith("dtp") Then
                    Dim dtp As DevExpress.XtraEditors.DateEdit = oControl
                    Dim oIndexName As String = GetIndexName(dtp, "dtp")

                    If dtp.Text = String.Empty Then
                        Dim oOptional = TestIsIndexOptional(pDocType, oIndexName)

                        If oOptional = False Then
                            ShowValidationMessage()
                            dtp.Focus()
                            Return False
                        End If
                    End If
                End If

                ' ========================= CHECK BOX =========================
                If oControl.Name.StartsWith("chk") Then
                    Dim chk As CheckBox = oControl
                    'result = True
                End If

                'If TypeOf (oControl) Is Button Then
                '    Continue For
                'End If

                'If oControl.Name.StartsWith("lbl") = False And result = False Then
                '    Logger.Info("Die Überprüfung der manuellen Indices ist fehlerhaft. Bitte informieren Sie den Systembetreuer")
                '    Return False
                'End If
            Next

            Return True
        Catch ex As Exception
            Logger.Warn("Unvorhergesehener Fehler in ValidateControls")
            Logger.Error(ex.Message)
            Return False
        End Try
    End Function

    Function GetControlValues(pPanel As Panel)
        Dim oAttributeValues As New List(Of UserAttributeValue)

        For Each oControl As Control In pPanel.Controls

            ' ========================= TEXTBOX =========================
            If oControl.Name.StartsWith("txt") Then
                Dim oTextBox As DevExpress.XtraEditors.TextEdit = oControl
                Dim oIndexName = GetIndexName(oTextBox, "txt")

                ' TODO: What to do when value is emmpty? send an empty value or skip the control?         
                'If oTextBox.Text = "" Then
                'End If

                oAttributeValues.Add(New UserAttributeValue With {
                    .AttributeName = oIndexName,
                    .AttributeValues = WrapIndexValue(oTextBox.Text),
                    .ControlName = oTextBox.Name
                })
            End If

            ' ========================= LOOKUP =========================
            If oControl.Name.StartsWith("cmbMulti") Then
                Dim oLookup = DirectCast(oControl, LookupControl3)
                Dim oValues As List(Of String) = oLookup.Properties.SelectedValues
                Dim oIndexName = GetIndexName(oLookup, "cmbMulti")

                If oValues.Count = 0 Then
                End If

                oAttributeValues.Add(New UserAttributeValue With {
                    .AttributeName = oIndexName,
                    .AttributeValues = WrapIndexValue(oValues),
                    .ControlName = oLookup.Name
                })
            End If

            ' ========================= COMBO BOX =========================
            If oControl.Name.StartsWith("cmbSingle") Then
                Dim cmbSingle As TextBox = oControl
                Dim oIndexName = GetIndexName(cmbSingle, "cmbSingle")

                If cmbSingle.Text = "" Then
                End If

                oAttributeValues.Add(New UserAttributeValue With {
                    .AttributeName = oIndexName,
                    .AttributeValues = WrapIndexValue(cmbSingle.Text),
                    .ControlName = cmbSingle.Name
                })
            End If

            If oControl.Name.StartsWith("cmb") Then
                Dim cmb As ComboBox = oControl
                Dim oIndexName = GetIndexName(cmb, "cmb")

                If cmb.Text = "" Then
                End If

                oAttributeValues.Add(New UserAttributeValue With {
                    .AttributeName = oIndexName,
                    .AttributeValues = WrapIndexValue(cmb.Text),
                    .ControlName = cmb.Name
                })
            End If

            ' ========================= DATE PICKER =========================
            If oControl.Name.StartsWith("dtp") Then
                Dim dtp As DevExpress.XtraEditors.DateEdit = oControl
                Dim oIndexName As String = GetIndexName(dtp, "dtp")

                oAttributeValues.Add(New UserAttributeValue With {
                   .AttributeName = oIndexName,
                   .AttributeValues = WrapIndexValue(dtp.EditValue.ToString),
                   .ControlName = dtp.Name
                })
            End If

            ' ========================= CHECK BOX =========================
            If oControl.Name.StartsWith("chk") Then
                Dim chk As CheckBox = oControl
                Dim oIndexName As String = GetIndexName(chk, "chk")


                oAttributeValues.Add(New UserAttributeValue With {
                   .AttributeName = oIndexName,
                   .AttributeValues = WrapIndexValue(chk.Checked.ToString),
                   .ControlName = chk.Name
                })
            End If
        Next

        Return oAttributeValues
    End Function

    Private Function GetIndexName(pControl As Control, pPrefix As String) As String
        Dim oIndexName = Replace(pControl.Name, pPrefix, "")
        Return oIndexName
    End Function

    Private Function WrapIndexValue(pValue As String) As String()
        Return New List(Of String) From {pValue}.ToArray
    End Function

    Private Function WrapIndexValue(pValues As List(Of String)) As String()
        Return pValues.ToArray
    End Function

    Private Function TestIsIndexOptional(pDocType As DocType, pIndexName As String) As Boolean
        Dim oIsOptional As Boolean = My.Helpers.GetValueFromDatatable(
            My.Application.Globix.CURR_DT_MAN_INDEXE,
            $"DOK_ID = {pDocType.Guid} AND INDEXNAME = '{pIndexName}'", "OPTIONAL", "")

        Return oIsOptional
    End Function

    Private Sub ShowValidationMessage()
        MsgBox(ClassStrings.TEXT_MISSING_INPUT, MsgBoxStyle.Exclamation, ClassStrings.TITLE_MISSING_INPUT)
    End Sub
End Class
