Imports System.Text
Imports CommonBLEntities
Imports CharterDotCom.Library
Imports System.Web


Public Class BinaryDocumentHandler
    Implements IHttpHandler

    Const HTMLTYPE As String = "text/HTML"
    Const GIFTYPE As String = "image/GIF"
    Const JPEGTYPE As String = "image/JPEG"
    Const TEXTTYPE As String = "text/plain"
    Const WORDTYPE As String = "Application/msword"
    Const EXCELTYPE As String = "Application/x-msexcel"
    Const PDFTYPE As String = "Application/pdf"


    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
        Try
            If Not context Is Nothing Then
                Dim id As String = context.Request.Params("ID")
                If Not id.Equals(String.Empty) Then
                    Dim result As KeyValuePair(Of String, Byte()) = GetDocument(id)
                    Dim doc As Byte() = result.Value
                    If Not doc Is Nothing Then
                        context.Response.Clear()
                        context.Response.ClearContent()
                        context.Response.Buffer = True
                        context.Response.ContentType = result.Key '"Application/pdf"

                        If context.Response.ContentType.ToLower().Equals("application/pdf") Then
                            context.Response.AddHeader("content-disposition", "attachment;filename=SupportArticle" + id.ToString() + ".pdf")
                        End If

                        context.Response.BinaryWrite(doc)
                        context.Response.Flush()
                        context.Response.End()
                    End If
                Else
                    Common.LogException(New Exception("Need Document ID to retrive Document"), CharterDotCom.CommonEntities.Library.Enumerations.ExceptionSeverity.High)
                End If
            End If
        Catch ex As Exception
            Common.LogException(ex, CharterDotCom.CommonEntities.Library.Enumerations.ExceptionSeverity.High)
        End Try
    End Sub

    Private Function GetDocument(ByVal id As String) As KeyValuePair(Of String, Byte())
        'Dim smProxy As SiteClient
        Dim smProxy As New BLSiteManager
        Dim trackingID As String
        Common.RegisterChannels()
        'smProxy = CType(Activator.GetObject(GetType(SiteClient), CStr(HttpContext.Current.Application("RemoteURL_SiteManager"))), SiteClient)
        trackingID = smProxy.Initialize(Nothing)
        Dim result As KeyValuePair(Of String, Byte()) = smProxy.GetBinaryDocument(CInt(id))
        Return result
    End Function

    Private Function GetMIMEType(ByVal type As String) As String
        Select Case type.ToLower
            Case "pdf"
                Return PDFTYPE
            Case "html"
                Return HTMLTYPE
            Case "gif"
                Return GIFTYPE
            Case "jpeg"
                Return JPEGTYPE
            Case "word"
                Return WORDTYPE
            Case "excel"
                Return EXCELTYPE
            Case Else
                Return TEXTTYPE
        End Select
    End Function

End Class
