Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.WebSite.Library

Public Class SyncHandler

	Shared Sub CheckSyncRedirectURL(ByVal sOldLink As String, ByVal oAccount As CharterDotCom.CommonEntities.Library.IAccount)

		If HttpContext.Current.Application(ApplicationKey.SyncProjectURLRedirectsEnabled) IsNot Nothing AndAlso _
		  HttpContext.Current.Application(ApplicationKey.SyncProjectURLRedirectsEnabled).ToString().Trim().ToUpper() = "T" Then

			Dim strApplicationVariable As String = HttpContext.Current.Application(ApplicationKey.SyncProjectURLRedirects)	'"/Visitors/Products.aspx?menuitem=2 ~http://www.Google.com?MenuItem=2*/Visitors/Products.aspx?menuitem=3~http://www.Google.com?MenuItem=3*/Visitors/Products.aspx?menuitem=4~http://www.Google.com?MenuItem=4*/visitors/home.aspx~http://www.Google.com?MenuItem=HomePage"

			If Not String.IsNullOrEmpty(strApplicationVariable) Then

				Dim SynchronossRedirectURLCollection As New Dictionary(Of String, String)
				Dim TokenizedRedirectArray() As String
				Const intSourceURL As Integer = 0
				Const intDestinationURL As Integer = 1

				If HttpContext.Current.Application(ApplicationKey.SyncProjectURLRedirectURLCollection) Is Nothing Then
                    Try
                        TokenizedRedirectArray = strApplicationVariable.Split("*")
                        '###############################################################################################
                        'BUILD THE REDIRECT COLLECTION INITIALLY, THEN PUT IN APPLICATION OBJECT FOR SUBSEQUENT REQUESTS
                        '###############################################################################################
                        If TokenizedRedirectArray IsNot Nothing AndAlso TokenizedRedirectArray.Length() > 0 Then
                            For i As Integer = 0 To TokenizedRedirectArray.Length() - 1
                                If Not String.IsNullOrEmpty(TokenizedRedirectArray(i)) Then
                                    Dim strRedirect() As String = TokenizedRedirectArray(i).Split("~")
                                    If strRedirect IsNot Nothing AndAlso strRedirect.Length() > 1 Then
                                        If Not String.IsNullOrEmpty(strRedirect(intSourceURL)) AndAlso Not String.IsNullOrEmpty(strRedirect(intDestinationURL)) Then
                                            If Not SynchronossRedirectURLCollection.ContainsKey(strRedirect(intSourceURL).Trim().ToLower()) Then
                                                SynchronossRedirectURLCollection.Add(strRedirect(intSourceURL).Trim().ToLower(), strRedirect(intDestinationURL).Trim().ToLower())
                                            End If
                                        End If
                                    End If
                                End If

                            Next
                        End If

                    Catch ex As Exception
                        'do nothing and use what is in place for the redirection collection.
                    Finally
                        HttpContext.Current.Application(ApplicationKey.SyncProjectURLRedirectURLCollection) = SynchronossRedirectURLCollection
                    End Try



                Else
                    '######################################################
                    'USE THE REDIRECT COLLECTION IN APPLICATION VARIABLE 
                    '######################################################
                    SynchronossRedirectURLCollection = HttpContext.Current.Application(ApplicationKey.SyncProjectURLRedirectURLCollection)
                End If


				If Not (SynchronossRedirectURLCollection Is Nothing) AndAlso SynchronossRedirectURLCollection.ContainsKey(sOldLink) _
				 AndAlso Not String.IsNullOrEmpty(SynchronossRedirectURLCollection(sOldLink)) Then
					'###########################################
					'REDIRECT THE USER TO THE DESTINATION URL
					'###########################################

					'JPIRKEY 4-2010
					'Now we have the destination URL, we also must add, if the option is set, the 
					'Address of the visitor - if they were logged into the MyAccount pages when they
					'clicked the link.

					'the following sequence of code relies on the current request coming
					'from an authenticated user, we must get the account object before continuing

                    If Not oAccount Is Nothing AndAlso oAccount.LoginID IsNot Nothing AndAlso oAccount.LoginID.Length > 0 Then

                        If Not oAccount.Address Is Nothing Then

                            If HttpContext.Current.Application(ApplicationKey.SyncProjectURLRedirectAddAddress) IsNot Nothing AndAlso _
                             HttpContext.Current.Application(ApplicationKey.SyncProjectURLRedirectAddAddress).ToString().Trim().ToUpper() = "T" Then
                                'TODO: 1) get the address param names from the settings
                                '2) Get the Account object from session 
                                '3) build the address querystring
                                '4) append the address qs onto the redirect url

                                'the form stored in memory/database will look like
                                'streetaddress={StreetAddress}|zip={ZipCode}
                                Dim sForm As String = HttpContext.Current.Application(ApplicationKey.SyncProjectURLRedirectsAddressParams)
                                Dim sParams() As String = sForm.Split("|")
                                Dim sRedirectURL As String = SynchronossRedirectURLCollection(sOldLink)
                                'the items in {} are a defined list of variables the user can select for inclusion
                                'into the URL.  the phrases in front of the = are whatever querystring params
                                'are required by the syncronoss application.

                                'replace the known keys, if they exist, and build the final URL
                                For Each s As String In sParams
                                    Select Case True
                                        Case s.Contains("{ZipCode}")
                                            sRedirectURL &= "&" & s.Replace("{ZipCode}", oAccount.Address.Zip)
                                        Case s.Contains("{StreetAddress1}")
                                            sRedirectURL &= "&" & s.Replace("{StreetAddress1}", oAccount.Address.Street1)
                                        Case s.Contains("{StreetAddress2}")
                                            sRedirectURL &= "&" & s.Replace("{StreetAddress2}", oAccount.Address.Street2)
                                    End Select
                                Next

                                'we have the fully quantified destination url - redirect the user:
                                HttpContext.Current.Response.Redirect(sRedirectURL, True)

                            Else
                                'no address addition necessary, just redirect them:
                                HttpContext.Current.Response.Redirect(SynchronossRedirectURLCollection(sOldLink), True)
                            End If 'application flag for address appending is true

                        End If 'address is nothing
                    Else
                        'no address addition necessary, just redirect them:
                        HttpContext.Current.Response.Redirect(SynchronossRedirectURLCollection(sOldLink), True)
                    End If

				End If

			End If
		End If
		'######################################################################################################
		'######################################################################################################
	End Sub

End Class
