Imports System.Collections.Generic
Imports CharterDotCom.CommonEntities.Library
Imports CharterDotCom.CommonEntities.Library.Common
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
Imports CharterDotCom.WebSite.Library
Imports CharterDotCom.WebSite.Library.BLCommon
Imports Utilities

Partial Class Visitors_Localize
    Inherits CharterCMSPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Localize")
        Dim account As IAccount

        If Not IsPostBack Then

            Me.Master.OrphanPage = True
            If Request.Params(QueryStringName.Mode) Is Nothing OrElse Not String.Compare(Request.Params(QueryStringName.Mode), "Change", True) = 0 Then
                account = AccountManager.Instance.GetAccount()
                If account IsNot Nothing Then
                    '####################################################################
                    'MODIFICATION ROD COPELAND  ISSUE 8640  CLOSE ORDERFLOW LOOP HOLE.
                    '####################################################################
                    If Request.IsAuthenticated AndAlso account.Address IsNot Nothing Then
                        AccountManager.Instance.Localize(account.Address.Street1, account.Address.Street2, account.Address.Zip, "")
                    Else
                        If account.UserStatus = UserStatus.Localized OrElse account.UserStatus = UserStatus.Identified Then
                            If account.AffliateID <> CharterDotCom.CommonEntities.Library.Common.m_CHARTER_AFFLIATE_ID Then
                                Redirect(account.IsExistingUser, account.UserStatus, True)
                            Else
                                Redirect(account.IsExistingUser, account.UserStatus, False)
                            End If
                        End If
                    End If


                End If

            End If
        End If
    End Sub

    Protected Sub ucLocalize_Localized() Handles ucLocalize.Localized

        Dim account As IAccount
        account = AccountManager.Instance.GetAccount()
        If account.AffliateID <> CharterDotCom.CommonEntities.Library.Common.m_CHARTER_AFFLIATE_ID Then
            Redirect(account.IsExistingUser, account.UserStatus, True)
        Else
            Redirect(account.IsExistingUser, account.UserStatus, False)
        End If
    End Sub

    Private Sub Redirect(ByVal isExistingUser As Boolean, ByVal status As UserStatus, ByVal ExternalAffiliate As Boolean)
        Dim availServices As List(Of BLServiceType)

        availServices = AccountManager.Instance.GetAccount().AvailableServices

        If availServices Is Nothing Then
            ' TODO
        ElseIf availServices.Count = 0 Then
            ' TODO
        Else
            Dim houseHitCookie As HttpCookie = New HttpCookie("hhs") 'house has service = hhs
            If isExistingUser Then
                '''''''''''''''''''''''''
                ' add cookie value to indicate that we got a house hit on localization
                ' we don't care if they pass the authentication test, send them on... 
                ' this is HBX related.
                '''''''''''''''''''''''''

                houseHitCookie.Value = 1
                houseHitCookie.Expires = Date.Today.AddDays(40)
                HttpContext.Current.Response.Cookies.Add(houseHitCookie)

                If status = UserStatus.Identified Then
                    Response.Redirect(Server.UrlDecode(Request.QueryString(QueryStringName.TargetPage) & Querystring.BuildFeatureChoiceQueryString(Request.QueryString)))
                Else
                    If ExternalAffiliate Then
                        Response.Redirect("CustomerIdentification.aspx" & "?" & QueryStringName.TargetPage & "=AffiliateAllOffers.aspx")
                    Else
                        Response.Redirect("CustomerIdentification.aspx" & "?" & QueryStringName.TargetPage & "=" & Request.QueryString(QueryStringName.TargetPage) & Querystring.BuildFeatureChoiceQueryString(Request.QueryString))
                    End If
                End If
            Else
                houseHitCookie.Value = 0
                houseHitCookie.Expires = Date.Today.AddDays(40)
                HttpContext.Current.Response.Cookies.Add(houseHitCookie)
                If ExternalAffiliate Then
                    Response.Redirect("Loading.aspx?" & QueryStringName.Destination & "=AffiliateAllOffers.aspx")
                Else
                    If Not (Request.QueryString(QueryStringName.TargetPage) Is Nothing) Then
                        Dim product As String = Request.Params(QueryStringName.ProductItem)
                        Dim url As String = Request.QueryString(QueryStringName.TargetPage) & Querystring.BuildFeatureChoiceQueryString(Request.QueryString)
                        If Not (product Is Nothing OrElse product.Equals(String.Empty)) Then
                            url = "Loading.aspx?" & QueryStringName.Destination & "=" & url & "&" & QueryStringName.ProductItem & "=" & product
                        End If
                        Response.Redirect(Server.UrlDecode("Loading.aspx?" & QueryStringName.Destination & "=" & url))
                    Else
                        Response.Redirect("Loading.aspx?" & QueryStringName.Destination & "=AllOffers.aspx")
                    End If
                End If
            End If

        End If
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
    End Sub

End Class
