Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.IO
Imports System.Web
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Enumerations
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
Imports CharterDotCom.CommonEntities.Library.EntityEBSServices
Imports CharterDotCom.CommonEntities.Library.EntitySupport
Imports CharterDotCom.CommonEntities.Library.EntitySearch
Imports CharterDotCom.CommonEntities.Library.EntityWebConfig
Imports CharterDotCom.CommonEntities.Library.EntityG2BServices


Public Class RedirectHandler
    Implements IHttpHandler

    Const OLDURL As String = "currentURL"
    Const NEWURL As String = "activeURL"
    Const PAGE As String = "page"
    Const REDIRECTS As String = "Redirects"
    Const CHARTERHOST As String = "www.Charter.com"

    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
        Get
            Return False
        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 currentURL As String
                Dim host As String
                host = context.Request.Url.Host
                ' TODO: BEFORE GOING TO PRODUCTION JUST TAKE OUT THE ORELSE PART
                If String.Compare(CHARTERHOST, host, True) = 0 OrElse String.Compare("UI", host, True) = 0 OrElse String.Compare("cc2uatx", host, True) = 0 Then
                    currentURL = context.Request.Url.LocalPath.ToString()
                Else
                    currentURL = context.Request.Url.LocalPath.ToString()
                End If
                Dim page As String
                Dim newURL As String = RedirectURL(currentURL, page)
                newURL = "/visitors/Support.aspx"
                page = "Support.aspx"
                If Not newURL Is Nothing Then
                    Redirect301(newURL, page, False)
                End If
            End If
        Catch ex As Exception
            Common.LogException(ex, ExceptionSeverity.High)
        End Try
    End Sub


    Private Function RedirectURL(ByVal currentURL As String, ByVal page As String) As String

        Dim doc As New XmlDocument
        doc.LoadXml(CType(HttpContext.Current.Application("Redirects"), String))
        Dim redirectsection As XmlNode = doc.DocumentElement
        If Not redirectsection Is Nothing Then

            For Each node As XmlNode In redirectsection.ChildNodes
                If String.Compare(node.Attributes(OLDURL).Value, currentURL, True) = 0 Then
                    page = node.Attributes(page).Value
                    Return node.Attributes(NEWURL).Value
                End If
            Next

        End If
        Return Nothing

    End Function

    Private Sub Redirect301(ByVal url As String, ByVal page As String, ByVal endResponse As Boolean)

        Dim context As System.Web.HttpContext = System.Web.HttpContext.Current

        If Not context Is Nothing Then

            If Not url.Equals(String.Empty) Then

                ' If File.Exists(context.Request.MapPath(context.Request.Url.Host)) Then
                context.Response.Status = "301 Moved Permanently"
                context.Response.AddHeader("Location", url)
                'Else
                ' Dim host As String = context.Request.Url.Host
                ' Try
                'context.Response.Redirect("~/Errors/PageNotFound.aspx")
                ' Catch
                ' ' ignore this exception
                ' End Try
                ' End If
                If endResponse Then
                    context.Response.End()
                End If
            End If

        End If

    End Sub

End Class
