Imports ApSLConverTemps.RefServWCFConverTemps

Partial Public Class MainPage
  Inherits UserControl
  Private cliente As ServWebWCFConverTempsClient = Nothing
  Private nGrados As Double

  Public Sub New()
    InitializeComponent()
    etError.Content = ""
    cliente = New ServWebWCFConverTempsClient()
    AddHandler cliente.ConvCentAFahrCompleted, AddressOf cliente_ConvCentAFahrCompleted
    AddHandler cliente.ConvFahrACentCompleted, AddressOf cliente_ConvFahrACentCompleted
    AddHandler cliente.ResultadoDetalladoCompleted, AddressOf cliente_ResultadoDetalladoCompleted
  End Sub

  Private Sub cliente_ConvCentAFahrCompleted(sender As Object, e As ConvCentAFahrCompletedEventArgs)
    If e.Error IsNot Nothing Then
      etError.Content = "Error en la conversión de grados C a F"
    Else
      ctGrados.Text = Convert.ToString(e.Result)
    End If
  End Sub

  Private Sub cliente_ConvFahrACentCompleted(sender As Object, e As ConvFahrACentCompletedEventArgs)
    If e.Error IsNot Nothing Then
      etError.Content = "Error en la conversión de grados F a C"
    Else
      ctGrados.Text = Convert.ToString(e.Result)
    End If
  End Sub

  Private Sub cliente_ResultadoDetalladoCompleted(sender As Object, e As ResultadoDetalladoCompletedEventArgs)
    If e.Error IsNot Nothing Then
      etError.Content = "Error al obtener detalles del resultado"
    Else
      ctGrados.Text = e.Result.Literal
    End If
  End Sub

  Private Sub btConvertir_Click(sender As Object, e As RoutedEventArgs)
    etError.Content = ""
    ' Obtener el valor escrito en la caja de texto
    Try
      nGrados = Convert.ToDouble(ctGrados.Text)
      ' Realizar la conversión invocando al método correspondiente
      If boConvCF.IsChecked = True Then
        cliente.ConvCentAFahrAsync(nGrados)
      End If
      If boConvFC.IsChecked = True Then
        cliente.ConvFahrACentAsync(nGrados)
      End If
    Catch exc As Exception
      etError.Content = exc.Message
    End Try
  End Sub

  Private Sub btDetalles_Click(sender As Object, e As RoutedEventArgs)
    etError.Content = ""
    Try
      nGrados = Convert.ToDouble(ctGrados.Text)
      Dim detalle As New Detalles()
      detalle.Grados = nGrados
      detalle.SonCentigrados = CBool(boConvCF.IsChecked)
      cliente.ResultadoDetalladoAsync(detalle)
    Catch exc As Exception
      etError.Content = exc.Message
    End Try
  End Sub
End Class
