Imports System.Xml
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices

Public Class OSSApiService
    Private _url As String
    Private _credentials As Net.ICredentials
    Private Const GET_ENCR_STR_SOAP_REQ As String = "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" soap:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" + _
  "<soap:Body>" + _
    "<getEncryptStringRequest xmlns=""messaging.ossapi.toolhost.corp.chartercom.com"">" + _
        "<accountNumber>{0}</accountNumber>" + _
        "<auditUser>{1}</auditUser>" + _
        "<clientIP>{2}</clientIP>" + _
        "<source>dotcom</source>" + _
        "<username>{3}</username>" + _
    "</getEncryptStringRequest>" + _
  "</soap:Body>" + _
"</soap:Envelope>"
    Public Property Url() As String
        Get
            Return _url
        End Get
        Set(ByVal value As String)
            _url = value
        End Set
    End Property

    Public Property Credentials() As Net.ICredentials
        Get
            Return _credentials
        End Get
        Set(ByVal value As Net.ICredentials)
            _credentials = value
        End Set
    End Property

    Public Sub New(ByVal url As String)
        _url = url
    End Sub
    Public Function GetEncryptString(ByVal request As GetEncryptStringRequest) As GetEncryptStringResponse

        '   Dim _url As String = "http://toolhostd.corp.chartercom.com:8099"

        Dim soapRequest As String = _
       String.Format(GET_ENCR_STR_SOAP_REQ, request.AccountNumber, request.AuditUser, request.SourceIPAddress, request.UserName)

        Dim response As New GetEncryptStringResponse

        '  Dim response As XDocument = _
        '<?xml version="1.0" encoding="UTF-8"?>
        '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        '    <soap:Body xmlns:th="http://toolhost.corp.chartercom.com/ossapi">
        '        <OSSResponse>
        '            <th:result>53616c7465645f5f7142335f64349a24f2dc13d3b031f27a459b4321866bf5582efb62b7373c5ce321fcd6c0efc3eef69c5db86c6288bf7f50f40906b6b7dabd573a3e240750a4fcbf580b5f9432b91fe92e21ede6713630eaef011cb56ce7c65f7744a9589abdde5df983d84068d0d7b16a257da026eb0e22cdaabc2fef778fae580ce544e4d19e22291b4c202d4233bace9723c5733521525556c5a601c33c953db3efc704a24aa46be199a1819410</th:result>
        '            <th:returnCode>0</th:returnCode>
        '            <th:returnString>Success</th:returnString>
        '            <th:roundTrip>0.386405</th:roundTrip>
        '        </OSSResponse>
        '    </soap:Body>
        '</soap:Envelope>


        Dim soapEnvelopeXml As New Xml.XmlDocument
        soapEnvelopeXml.LoadXml(soapRequest)
        Dim webRequest As Net.HttpWebRequest = CreateWebRequest(Url, "OSSRequest")
        InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest)


        Using webResponse As Net.WebResponse = webRequest.GetResponse
            Using rd As New XmlTextReader(webResponse.GetResponseStream())
                While rd.Read()
                    If rd.NodeType = XmlNodeType.Element Then
                        Select Case rd.Name
                            Case "th:result"
                                response.AuthString = rd.ReadString().Trim
                            Case "th:returnCode"
                                response.ReturnCode = rd.ReadElementContentAsString()
                            Case "th:returnString"
                                response.Status = rd.ReadString().Trim
                            Case "th:roundTrip"
                                response.RoundTrip = rd.ReadElementContentAsDecimal()
                        End Select
                    End If
                End While
            End Using
        End Using
        Return response
    End Function


    Private Shared Function CreateWebRequest(ByVal url As String, ByVal action As String) As System.Net.HttpWebRequest

        Dim webRequest As Net.HttpWebRequest = Net.WebRequest.Create(url)
        webRequest.Headers.Add("SOAPAction", action)
        webRequest.ContentType = "text/xml;charset=""utf-8"""
        webRequest.Accept = "text/xml"
        webRequest.Method = "POST"
        Return webRequest
    End Function
    Private Shared Sub InsertSoapEnvelopeIntoWebRequest(ByVal soapEnvelopeXml As Xml.XmlDocument, ByVal webRequest As Net.HttpWebRequest)

        Using stream As IO.Stream = webRequest.GetRequestStream()
            soapEnvelopeXml.Save(stream)
        End Using
    End Sub

End Class
