Imports System.Net
Imports System.Net.Http
Imports System.Net.Security
Imports System.Threading.Tasks
Imports Newtonsoft.Json
Imports WebIntranet.Models

Public Class datosPersonales
    Inherits System.Web.UI.Page
    'Shared sessionUsuario As String
    'Shared usuariosSeleccionados As List(Of String)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("usuario") = "" Then
            Response.Redirect("default.aspx")
        End If
        'sessionUsuario = Session("usuario")

    End Sub
    <System.Web.Services.WebMethod()>
    Public Shared Async Function cargarDatosPersonales() As Task(Of String)

        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)

        Dim objetoConsulta As New Models.NifPersona

        Dim sessionUsuario As String = HttpContext.Current.Session("usuario")
        objetoConsulta.Nif = sessionUsuario
        Dim jsonConsulta As String = JsonConvert.SerializeObject(objetoConsulta)
        Dim url As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/ObtenerDatosPersonales"

        Dim clienteHTTP As New HttpClient
        Dim contenidoConsultaHTTP As New StringContent(jsonConsulta, Encoding.UTF8, "application/json")
        Debug.WriteLine("CargarListadodePicadasAsync:1===" + Now.ToLongTimeString)
        Dim respuestaHTTP As HttpResponseMessage = Await clienteHTTP.PostAsync(url, contenidoConsultaHTTP).ConfigureAwait(False)
        Debug.WriteLine("CargarListadodePicadasAsync:2===" + Now.ToLongTimeString)
        Dim sRespuesta As String = Await respuestaHTTP.Content.ReadAsStringAsync().ConfigureAwait(False)
        Debug.WriteLine("CargarListadodePicadasAsync:3===" + Now.ToLongTimeString)

        Dim settings As New Newtonsoft.Json.JsonSerializerSettings()
        settings.CheckAdditionalContent = True
        Dim objetoDatos As ResultadoObtenerDatosPersonales =
            Newtonsoft.Json.JsonConvert.DeserializeObject(Of ResultadoObtenerDatosPersonales)(sRespuesta, settings)

        'txtDireccionDP.Value = objetoDatos.DatosPersonales.Direccion
        'txtCPDP.Value = objetoDatos.DatosPersonales.CodigoPostal
        'txtPoblacionDP.Value = objetoDatos.DatosPersonales.Poblacion
        'txtProvinciaDP.Value = objetoDatos.DatosPersonales.Provincia
        'txtTelefono1DP.Value = objetoDatos.DatosPersonales.Telefono
        'txtTelefono2DP.Value = objetoDatos.DatosPersonales.Movil
        'txtIbanDP.Value = objetoDatos.DatosPersonales.IBAN

        Dim html = "{""direccion"":""" + objetoDatos.DatosPersonales.Direccion + """,""CPDP"":""" + objetoDatos.DatosPersonales.CodigoPostal + """,""poblacion"":""" +
            objetoDatos.DatosPersonales.Poblacion + """,""provincia"":""" + objetoDatos.DatosPersonales.Provincia + """,""telefono1"":""" + objetoDatos.DatosPersonales.Telefono +
            """,""telefono2"":""" + objetoDatos.DatosPersonales.Movil + """,""iban"":""" + objetoDatos.DatosPersonales.IBAN + """}"

        Return html

    End Function
    <System.Web.Services.WebMethod()>
    Public Shared Async Function actualizarDatosPersonales(direccion As String, codigopostal As String, provincia As String, poblacion As String, telefono As String, movil As String, iban As String) As Task(Of String)

        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim sessionUsuario As String = HttpContext.Current.Session("usuario")
        '// Aquí preparamos el objeto para la consulta.
        Dim objetoConsulta As New Models.DatosPersonas
        objetoConsulta.Nif = sessionUsuario
        objetoConsulta.Direccion = direccion
        objetoConsulta.CodigoPostal = codigopostal
        objetoConsulta.Provincia = provincia
        objetoConsulta.Poblacion = poblacion
        objetoConsulta.Telefono = telefono
        objetoConsulta.Movil = movil
        objetoConsulta.IBAN = ""


        Dim jsonConsulta As String = JsonConvert.SerializeObject(objetoConsulta)
        Dim url As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/CambiarDatosPersonales"

        Dim clienteHTTP As New HttpClient
        Dim contenidoConsultaHTTP As New StringContent(jsonConsulta, Encoding.UTF8, "application/json")
        Debug.WriteLine("CargarListadodePicadasAsync:1===" + Now.ToLongTimeString)
        Dim respuestaHTTP As HttpResponseMessage = Await clienteHTTP.PostAsync(url, contenidoConsultaHTTP).ConfigureAwait(False)
        Debug.WriteLine("CargarListadodePicadasAsync:2===" + Now.ToLongTimeString)
        Dim sRespuesta As String = Await respuestaHTTP.Content.ReadAsStringAsync().ConfigureAwait(False)
        Debug.WriteLine("CargarListadodePicadasAsync:3===" + Now.ToLongTimeString)

        Dim settings As New Newtonsoft.Json.JsonSerializerSettings()
        settings.CheckAdditionalContent = True
        Dim objetoDatos As ResultadoObtenerDatosPersonales =
                Newtonsoft.Json.JsonConvert.DeserializeObject(Of ResultadoObtenerDatosPersonales)(sRespuesta, settings)

        Return sRespuesta
    End Function

    <System.Web.Services.WebMethod()>
    Public Shared Async Function SolicitudCambioIBAN(SWIFT As String, BIC As String, Ent As String, IBAN As String) As Task(Of String)
        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Dim sessionUsuario As String = HttpContext.Current.Session("usuario")
        '// Aquí preparamos el objeto para la consulta.
        Dim objetoConsulta As New Models.GenerarForaTer
        objetoConsulta.nif = sessionUsuario
        objetoConsulta.Swift = SWIFT
        objetoConsulta.Bic = BIC
        objetoConsulta.Entidad = Ent
        objetoConsulta.IBAN = IBAN

        Dim jsonConsulta As String = JsonConvert.SerializeObject(objetoConsulta)
        Dim url As String = ConfigurationManager.AppSettings("RutaRes").ToString + "/api/GenerarForaTer"
        Dim clienteHTTP As New HttpClient
        Dim contenidoConsultaHTTP As New StringContent(jsonConsulta, Encoding.UTF8, "application/json")
        Debug.WriteLine("CargarListadodePicadasAsync:1===" + Now.ToLongTimeString)
        Dim respuestaHTTP As HttpResponseMessage = Await clienteHTTP.PostAsync(url, contenidoConsultaHTTP).ConfigureAwait(False)
        Debug.WriteLine("CargarListadodePicadasAsync:2===" + Now.ToLongTimeString)
        Dim sRespuesta As String = Await respuestaHTTP.Content.ReadAsStringAsync().ConfigureAwait(False)
        Debug.WriteLine("CargarListadodePicadasAsync:3===" + Now.ToLongTimeString)
        Dim settings As New Newtonsoft.Json.JsonSerializerSettings()
        settings.CheckAdditionalContent = True
        Dim objetoDatos As ResultadoGenerarForaTer =
                Newtonsoft.Json.JsonConvert.DeserializeObject(Of ResultadoGenerarForaTer)(sRespuesta, settings)

        Return sRespuesta

    End Function
End Class