Imports DevExpress.Xpf.Editors
Imports DevExpress.Mvvm.UI.Interactivity
Namespace Plantillas
    Public Class SendEmailButton
        Inherits Button
        Public Sub New()
            Me.SetDefaultStyleKey(GetType(SendEmailButton))
        End Sub

        Private Sub SetDefaultStyleKey(type As Type)

        End Sub

    End Class
    Public Class Comun
        ' Public Shared ColorFondo As SolidColorBrush = New SolidColorBrush(Colors.DarkGray)
        Public Shared ColorTexto As SolidColorBrush = New SolidColorBrush(Colors.DarkGray)
    End Class

    Public Class Impar
        Implements IValueConverter

        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Dim rh As DevExpress.Xpf.Data.RowHandle = value
            Dim valor As Integer = If(rh Is Nothing, 0, rh.Value)
            Return (valor Mod 2 > 0)
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Return value
        End Function
    End Class
    Public Class DateTimeToTimeSpanConverter
        Implements IValueConverter

        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            If value Is Nothing Then
                Return Nothing
            Else
                Dim ts As TimeSpan = value
                Dim ahora As DateTime = Now
                Return New DateTime(ahora.Year, ahora.Month, ahora.Day, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds)
            End If
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            If value Is Nothing Then
                Return Nothing
            Else
                Dim dt As DateTime = value
                Return dt.TimeOfDay
            End If
        End Function
    End Class
    Public Class TimeSpanConverter
        Implements IValueConverter

        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            '  Try
            Dim Valor As TimeSpan? = value
            If Valor.HasValue Then
                Dim Valors As String = Fix(Math.Abs(Valor.Value.TotalHours)).ToString.PadLeft(2, "0") & ":" & Math.Abs(CInt(Valor.Value.Minutes)).ToString.PadLeft(2, "0")
                If Valor.Value.Ticks < 0 Then
                    Valors = "-" & Valors
                End If
                Return Valors
            Else
                Return Nothing
            End If
            '    Catch ex As Exception
            'Return "error"
            '      End Try
        End Function

        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Try
                If value Is Nothing Then
                    Return Nothing
                Else
                    Dim Valor As String = value
                    If Valor = "" Then
                        Return Nothing
                    Else
                        If Valor.StartsWith("-") Then
                            Dim tsn = New TimeSpan(Math.Abs(CInt(Valor.Split(":")(0))), CInt(Valor.Split(":")(1)), 0)
                            Return -tsn
                        Else
                            Return New TimeSpan(CInt(Valor.Split(":")(0)), CInt(Valor.Split(":")(1)), 0)
                        End If
                    End If
                End If
            Catch ex As Exception
                Return DependencyProperty.UnsetValue  '  Return New TimeSpan(0)
            End Try
        End Function
    End Class
    Public Class TrimTextOnLostFocusBehavior
        Inherits Behavior(Of TextEdit)

        Protected Overrides Sub OnAttached()
            MyBase.OnAttached()
            AddHandler AssociatedObject.IsKeyboardFocusWithinChanged, AddressOf AssociatedObject_IsKeyboardFocusWithinChanged
            '    AssociatedObject.IsKeyboardFocusWithinChanged += AddressOf AssociatedObject_IsKeyboardFocusWithinChanged
        End Sub

        Protected Overrides Sub OnDetaching()
            MyBase.OnDetaching()
            RemoveHandler AssociatedObject.IsKeyboardFocusWithinChanged, AddressOf AssociatedObject_IsKeyboardFocusWithinChanged
        End Sub

        Private Sub AssociatedObject_IsKeyboardFocusWithinChanged(ByVal sender As Object, ByVal e As DependencyPropertyChangedEventArgs)
            Dim currentValue As String = TryCast(AssociatedObject.EditValue, String)

            If Not CBool(e.NewValue) AndAlso currentValue IsNot Nothing Then
                AssociatedObject.EditValue = currentValue.Trim()
            End If
        End Sub
    End Class
End Namespace