Imports DevExpress.Xpf.Editors.Helpers
Imports DevExpress.Xpf.Editors.Validation
Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.Docking

Public Class ErrorValidacion
    Public id As String
    Public Objeto As Object
    Public ErrorContent As Object
    Public Excepcion As Exception
    Public Tipo As DevExpress.XtraEditors.DXErrorProvider.ErrorType
    Public Sub New(id As String, Objeto As Object, ErrorContent As Object, excepcion As Exception, tipo As DevExpress.XtraEditors.DXErrorProvider.ErrorType)
        Me.id = id
        Me.Objeto = Objeto
        Me.ErrorContent = ErrorContent
        Me.Excepcion = excepcion
        Me.Tipo = tipo
    End Sub

    'Public Sub New(Objeto As Object, ErrorContent As Object, excepcion As Exception, tipo As DevExpress.XtraEditors.DXErrorProvider.ErrorType)
    '    Me.id = Objeto.name
    '    Me.Objeto = Objeto
    '    Me.ErrorContent = ErrorContent
    '    Me.Excepcion = excepcion
    '    Me.Tipo = tipo
    'End Sub
End Class
Public Class ErroresValidacion

    Public Errores As New List(Of ErrorValidacion)
    ' Private ApCabLin As ApCabLin
    Private PanelErrores As LayoutPanel
    Public ReadOnly Property Count As Integer
        Get
            Return Errores.Count
        End Get
    End Property
    'Public Sub New(apcablin As ApCabLin)
    '    Me.ApCabLin = apcablin
    'End Sub
    Public Sub New(PanelErrores As LayoutPanel)
        Me.PanelErrores = PanelErrores
    End Sub


    Public Sub AgregaError(ErrorValidacion As ErrorValidacion, e As DevExpress.Xpf.Editors.ValidationEventArgs)
        Dim ev = (From er In Errores Select er Where er.id = ErrorValidacion.id)
        If ev.Count = 0 Then
            Errores.Add(ErrorValidacion)
        Else
            ev.First.Objeto = ErrorValidacion.Objeto
            ev.First.ErrorContent = ErrorValidacion.ErrorContent
            ev.First.Excepcion = ErrorValidacion.Excepcion
            ev.First.Tipo = ErrorValidacion.Tipo
        End If
        MuestraErrores()
        If (e Is Nothing OrElse e.IsValid) Then
            If ErrorValidacion.Objeto IsNot Nothing Then BaseEditHelper.SetValidationError(ErrorValidacion.Objeto, New BaseValidationError(ErrorValidacion.ErrorContent, ErrorValidacion.Excepcion, ErrorValidacion.Tipo))
        Else
            If Not e.IsValid Then
                e.SetError(ErrorValidacion.ErrorContent, ErrorValidacion.Tipo)
            End If
        End If
    End Sub
    Public Sub EliminaError(id As String)
        Dim ev = (From er In Errores Select er Where er.id = id)
        If ev.Count > 0 Then
            BaseEditHelper.SetValidationError(ev.First.Objeto, Nothing)
            Errores.Remove(ev.First)
            If Errores.Count = 0 Then
                PanelErrores.Visibility = Visibility.Collapsed
            Else
                RellenaErrores()
            End If
        End If
    End Sub

    Public Sub LimpiarErrores(Patron As String)
        Dim i As Integer
        Dim ev As ErrorValidacion
        For i = Errores.Count - 1 To 0 Step -1
            ev = Errores(i)
            If Errores(i).id.ToLower.StartsWith(Patron.ToLower) Then
                If Not ev.Objeto Is Nothing Then
                    BaseEditHelper.SetValidationError(ev.Objeto, Nothing)
                End If
                Errores.Remove(ev)
            End If
        Next
        If Errores.Count = 0 Then
            PanelErrores.Visibility = Visibility.Collapsed
            '           PanelErrores.Visibility = Visibility.Hidden

        End If
    End Sub

    Friend Sub LimpiarErrores(Optional SoloCritical As Boolean = True)
        Dim i As Integer
        Dim ev As ErrorValidacion

        For i = Errores.Count - 1 To 0 Step -1
            If Not SoloCritical Or Errores(i).Tipo = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical Then
                ev = Errores(i)
                If Not ev.Objeto Is Nothing Then
                    BaseEditHelper.SetValidationError(ev.Objeto, Nothing)
                End If
                Errores.Remove(ev)
            End If
        Next
        PanelErrores.Visibility = Visibility.Collapsed
        'PanelErrores.Visibility = Visibility.Hidden

    End Sub
    Sub MuestraErrores()
        Try
            RellenaErrores()
            PanelErrores.Visibility = System.Windows.Visibility.Visible
            Dim dm = PanelErrores.GetDockLayoutManager
            dm.DockController.Dock(PanelErrores)
        Catch ex As Exception
            Console.Write(ex.Message)
        End Try
    End Sub
    Sub RellenaErrores()
        Dim ecvs As New CollectionViewSource
        Dim le = (From er In Errores Select er.ErrorContent).ToList
        PanelErrores.DataContext = le
    End Sub
End Class

