Imports DigitalData.Controls.LookupGrid
Imports DigitalData.GUIs.ZooFlow.Base
Imports DigitalData.GUIs.ZooFlow.Globix.Models
Imports DigitalData.GUIs.ZooFlow.frmGlobix_Index
Imports DigitalData.Modules.EDMI.API
Imports DigitalData.Modules.EDMI.API.EDMIServiceReference
Imports DigitalData.Modules.Logging
Imports DigitalData.GUIs.GlobalIndexer.ControlCreator
Imports DevExpress.XtraEditors

Public Class ClassValidator
    Inherits BaseClass

    Private ReadOnly Client As Client
    Private ReadOnly ManualIndexes As List(Of ManualIndex)

    Public Sub New(pLogConfig As LogConfig, pClient As Client, pManualIndexes As List(Of ManualIndex))
        MyBase.New(pLogConfig)
        Client = pClient
        ManualIndexes = pManualIndexes
    End Sub

    Public Function GetControlMeta(pControl As Control) As ControlMeta
        Dim oMeta As ControlMeta = Nothing
        If TypeOf pControl.Tag Is ControlMeta Then
            oMeta = DirectCast(pControl.Tag, ControlMeta)
        End If
        Return oMeta
    End Function


    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
                Dim oMeta As ControlMeta = GetControlMeta(oControl)

                ' Labels do not need to validated
                If TypeOf oControl Is Label Then
                    Continue For
                End If

                ' ========================= TEXT BOX =========================
                If oMeta.ControlType = "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 oMeta.ControlType = "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
                If oMeta.ControlType = "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 oMeta.ControlType = "cmb" Then
                    Dim cmb As Windows.Forms.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 oMeta.ControlType = "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 oMeta.ControlType = "chk" Then
                    'TODO: Implement Itermediate state and then validation for checkbox
                    Dim chk As CheckEdit = oControl
                    'result = True
                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) As List(Of UserAttributeValue)
        Dim oAttributeValues As New List(Of UserAttributeValue)

        For Each oControl As Control In pPanel.Controls
            Dim oMeta As ControlMeta = GetControlMeta(oControl)

            ' Labels do not have values
            If TypeOf oControl Is Label Then
                Continue For
            End If

            ' ========================= TEXTBOX =========================
            If oMeta.ControlType = "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 {
                    .Name = oIndexName,
                    .Values = WrapIndexValue(oTextBox.Text),
                    .ControlName = oTextBox.Name
                })
            End If

            ' ========================= LOOKUP =========================
            If oMeta.ControlType = "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 {
                    .Name = oIndexName,
                    .Values = WrapIndexValue(oValues),
                    .ControlName = oLookup.Name
                })


            End If

            If oMeta.ControlType = "cmbSingle" Then
                Dim cmbSingle As TextBox = oControl
                Dim oIndexName = GetIndexName(cmbSingle, "cmbSingle")

                If cmbSingle.Text = "" Then
                End If

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

            If oMeta.ControlType = "cmb" Then
                Dim cmb As ComboBox = oControl
                Dim oIndexName = GetIndexName(cmb, "cmb")

                If cmb.Text = "" Then
                End If

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

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

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

            ' ========================= CHECK BOX =========================
            If oMeta.ControlType = "chk" Then
                Dim chk As CheckEdit = oControl
                Dim oIndexName As String = GetIndexName(chk, "chk")


                oAttributeValues.Add(New UserAttributeValue With {
                   .Name = oIndexName,
                   .Values = 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 = ManualIndexes.
            Where(Function(index) index.DocTypeId = pDocType.Guid And index.Name = pIndexName).
            Select(Function(index) index.IsOptional).
            FirstOrDefault()

        Return oIsOptional
    End Function

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