Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.EDMI.API
Imports DigitalData.Modules.EDMI.API.EDMIServiceReference
Imports DigitalData.GUIs.ClientSuite.Base
Imports System.ServiceModel.Security

Public Class ClassService
    Inherits BaseClass

    Public Enum ConnectionTestResult
        Successful
        NotFound
        EmptyURI
        Authentication
        Unknown
    End Enum

    Public Sub New(LogConfig As LogConfig)
        MyBase.New(LogConfig)
    End Sub

    Public Function TestConnection() As ConnectionTestResult
        Return TestConnection(My.SysConfig.ServiceConnection)
    End Function

    Public Function TestConnection(EndpointURL As String) As ConnectionTestResult
        Try
            Dim oChannelFactory = GetChannelFactory(EndpointURL)
            Dim oChannel = oChannelFactory.CreateChannel()
            oChannel.OperationTimeout = New TimeSpan(0, 0, ClassConstants.SERVICE_OPEN_TIMEOUT)
            oChannel.Open()
            oChannel.Close()

            Return ConnectionTestResult.Successful
        Catch ex As EndpointNotFoundException
            Logger.Error(ex)
            Return ConnectionTestResult.NotFound
        Catch ex As Exception
            Logger.Error(ex)
            Return ConnectionTestResult.Unknown
        End Try
    End Function

    Public Async Function TestConnectionAsync() As Task(Of ConnectionTestResult)
        Return Await TestConnectionAsync(My.SysConfig.ServiceConnection)
    End Function

    Public Async Function TestConnectionAsync(EndpointURL As String) As Task(Of ConnectionTestResult)
        Return Await Task.Factory.StartNew(Function()
                                               Try
                                                   Dim oChannelFactory = GetChannelFactory(EndpointURL)
                                                   Dim oChannel = oChannelFactory.CreateChannel()
                                                   oChannel.OperationTimeout = New TimeSpan(0, 0, ClassConstants.SERVICE_OPEN_TIMEOUT)
                                                   oChannel.Open()
                                                   oChannel.Close()

                                                   Return ConnectionTestResult.Successful
                                               Catch ex As EndpointNotFoundException
                                                   Logger.Error(ex)
                                                   Return ConnectionTestResult.NotFound
                                               Catch ex As UriFormatException
                                                   Logger.Error(ex)
                                                   Return ConnectionTestResult.EmptyURI
                                               Catch ex As SecurityNegotiationException
                                                   Logger.Error(ex)
                                                   Return ConnectionTestResult.Authentication
                                               Catch ex As Exception
                                                   Logger.Error(ex)
                                                   Return ConnectionTestResult.Unknown
                                               End Try
                                           End Function)
    End Function

    Public Function GetChannelFactory() As IChannelFactory(Of IEDMIServiceChannel)
        Return GetChannelFactory(My.SysConfig.ServiceConnection)
    End Function

    Public Function GetChannelFactory(EndpointURL As String) As ChannelFactory(Of IEDMIServiceChannel)
        Dim oBinding = GetBinding()
        Dim oEndpoint = New EndpointAddress(EndpointURL)

        Dim oFactory As New ChannelFactory(Of IEDMIServiceChannel)(oBinding, oEndpoint)
        Return oFactory
    End Function

    Private Function GetBinding() As NetTcpBinding
        Dim oBinding As New NetTcpBinding()
        oBinding.Security.Mode = SecurityMode.Transport
        oBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows
        oBinding.MaxReceivedMessageSize = ClassConstants.SERVICE_MAX_MESSAGE_SIZE
        oBinding.MaxBufferSize = ClassConstants.SERVICE_MAX_BUFFER_SIZE
        oBinding.MaxBufferPoolSize = ClassConstants.SERVICE_MAX_BUFFER_SIZE
        oBinding.MaxConnections = ClassConstants.SERVICE_MAX_CONNECTIONS
        oBinding.ReaderQuotas.MaxArrayLength = ClassConstants.SERVICE_MAX_ARRAY_LENGTH
        oBinding.ReaderQuotas.MaxStringContentLength = ClassConstants.SERVICE_MAX_STRING_LENGTH
        oBinding.OpenTimeout = New TimeSpan(0, 0, ClassConstants.SERVICE_OPEN_TIMEOUT)

        Return oBinding
    End Function
End Class
