Imports System.Net
Imports System.Configuration
Imports System.Data
Imports Microsoft.VisualBasic

Public Class LocationProxyFactory

    Private Shared Function GetLocationPwd() As String
        Dim sPwd As String
        sPwd = ConfigurationManager.AppSettings("Location_Pwd")
        If sPwd = "" Then
            sPwd = "charter!"
        End If
        Return sPwd
    End Function

    Private Shared Function GetLocationDomain() As String
        Dim sDomain As String
        sDomain = ConfigurationManager.AppSettings("Location_Domain")
        If sDomain = "" Then
            sDomain = "chtr"
        End If
        Return sDomain
    End Function

    Private Shared Function GetLocationUser() As String
        Dim sUser As String
        sUser = ConfigurationManager.AppSettings("Location_User")
        If sUser = "" Then
            sUser = "svc_matrixebstest"
        End If
        Return sUser
    End Function

    Private Shared Function GetLocationNetworkCredential(ByVal pURL As String) As ICredentials
        Dim oNetCred As NetworkCredential
        Dim oUri As Uri
        Dim oCred As ICredentials
        Dim sUser As String
        Dim sPwd As String
        Dim sDomain As String

        Dim ds As New DataSet

        Try
            sUser = GetLocationUser()
            sPwd = GetLocationPwd()
            sDomain = GetLocationDomain()
            oNetCred = New NetworkCredential(sUser, sPwd, sDomain)
            oUri = New Uri(pURL)
            oCred = oNetCred.GetCredential(oUri, "Basic")

            Return oCred

        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Shared Function CreateLocationPort() As LocationServiceSchemaPort.LocationServiceSchemaPort
        ' create an object which can call the Location Serviceability web service,
        ' including the credentials needed to access the service.
        Dim oWS As New LocationServiceSchemaPort.LocationServiceSchemaPort

        ' the web service object
        Dim oCred As ICredentials
        Dim ds As New DataSet
        'Dim iSvc As Integer

        Try
            ' set credentials to call web service
            oCred = LocationProxyFactory.GetLocationNetworkCredential(oWS.Url)
            oWS.Credentials = oCred
            Return oWS

        Catch ex As Exception
            Return Nothing
        End Try
    End Function

End Class
