Imports DevExpress.Xpf.Core

Public Class dxwCalculadoraFecha
    Public NombreFichero As String
    Public FechaCalculada As Date
    Private _Fiestas As List(Of Date)
    Public Sub New(FechaInicio As Date, Optional Fiestas As List(Of Date) = Nothing, Optional Titulo As String = "Introduzca Datos")
        ' Llamada necesaria para el diseñador.
        InitializeComponent()
        Me.Title = Titulo
        Me.deFechaInicial.EditValue = FechaInicio
        Me.deFechaCalculada.EditValue = FechaInicio
        Me.DiasNaturales.EditValue = 0
        Me.DiasHabiles.EditValue = 0
        _Fiestas = Fiestas

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

    End Sub

    Public Sub New()
        ' Llamada necesaria para el diseñador.
        InitializeComponent()
        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

    End Sub
    Private Sub btCancelar_Click(sender As Object, e As RoutedEventArgs)
        Me.DialogResult = False
        Me.Close()
    End Sub

    Private Sub btAceptar_Click(sender As Object, e As RoutedEventArgs)
        Try
            If Me.deFechaCalculada.EditValue Is Nothing Then Throw New Exception("La fecha calculada no es válida")
            Me.FechaCalculada = Me.deFechaCalculada.EditValue
            Me.DialogResult = True
            Me.Close()
        Catch ex As Exception
            DXMessageBox.Show(ex.Message, "Error")
        End Try
    End Sub

    Private Sub btDiasNaturales_Click(sender As Object, e As RoutedEventArgs)
        Dim FechaInicial As Date = Me.deFechaInicial.EditValue
        deFechaCalculada.EditValue = FechaInicial.AddDays(DiasNaturales.EditValue)
    End Sub

    Private Sub btDiasHabiles_Click(sender As Object, e As RoutedEventArgs)
        Dim FechaInicial As Date = Me.deFechaInicial.EditValue
        Dim ndias As Integer = DiasHabiles.EditValue
        Dim FechaCalculada As Date = FechaInicial
        Dim Incremento As Integer = If(ndias > 0, 1, -1)
        Do Until ndias = 0
            If Not (FechaCalculada.DayOfWeek = DayOfWeek.Sunday OrElse FechaCalculada.DayOfWeek = DayOfWeek.Saturday OrElse (_Fiestas IsNot Nothing AndAlso _Fiestas.Contains(FechaCalculada))) Then ndias -= Incremento
            FechaCalculada = FechaCalculada.AddDays(Incremento)
        Loop
        deFechaCalculada.EditValue = FechaCalculada
    End Sub
End Class
