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_Upsell
    Inherits CharterCMSPage


    Public ReadOnly Property SelectedItemID() As Integer
        Get
            If Request.Form("txtAddItem") Is Nothing Or Request.Form("txtAddItem") = String.Empty Then
                Return 0
            Else
                Return CType(Request.Form("txtAddItem"), Integer)
            End If
        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Add & Save")
        Dim acct As IAccount = AccountManager.Instance.GetAccount()
        If acct IsNot Nothing Then
            If Not (acct.AffliateID = CharterDotCom.CommonEntities.Library.Common.m_CHARTER_AFFLIATE_ID) Then
                Response.Redirect("AffiliateUpsell.aspx")
            End If
        End If

        If Not Page.IsPostBack Then
            Me.Master.OrphanPage = True
        End If

        If SelectedItemID > 0 Then
            CatalogManager.Instance.AddCartItem(SelectedItemID, New Notify(AddressOf ucShoppingCart.OnNotification))
        End If
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim categories As List(Of Category)
        Dim catalog As ICatalog
        Dim cart As ICart
        Dim item As KeyValuePair(Of String, ICartItem)
        Dim subitem As KeyValuePair(Of String, ISubCartItem)
        Me.ucShoppingCart.HBXInfo_PageName = Me.Master.HBXPageName
        Me.ucShoppingCart.HBXInfo_MLC = Me.Master.HBXMLC

        catalog = CatalogManager.Instance.GetCatalog()

        If catalog Is Nothing Then
            Throw New MissingCatalogException()
        End If

        cart = catalog.Cart

        ' assign shopping cart's cart property
        ucShoppingCart.Cart = catalog.Cart

        categories = New List(Of Category)()

        categories.Add(Category.Cable)
        categories.Add(Category.HSD)
        categories.Add(Category.Telephoney)

        For Each item In cart.CartItems
            If item.Value.Type = ItemType.Bundle Then
                For Each subitem In item.Value.Children
                    If categories.Contains(subitem.Value.Category) Then
                        categories.Remove(subitem.Value.Category)
                    End If
                Next
            ElseIf categories.Contains(item.Value.Category) Then
                categories.Remove(item.Value.Category)
            End If
        Next

        If categories.Count = 0 Then
            Response.Redirect("ChooseOptions.aspx")
        End If
        RenderCatalog(categories)
    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnNext.Click
        If Page.IsValid Then
            Response.Redirect("ChooseOptions.aspx")
        End If
    End Sub

    Private Sub RenderCatalog(ByVal categories As List(Of Category))
        Dim catalog As ICatalog
        Dim item As KeyValuePair(Of String, ICartItem)
        Dim subitem As KeyValuePair(Of String, ISubCartItem)
        Dim _control As Controls_Offer
        Dim _bundleControls As ArrayList
        Dim upgradesAvailable As Boolean = False
        Dim _cableControls As New ArrayList
        Dim _HSDControls As New ArrayList
        Dim _telephoneControls As New ArrayList

        catalog = CatalogManager.Instance.GetCatalog()

        If catalog Is Nothing Then
            Throw New MissingCatalogException()
        End If

        _bundleControls = New ArrayList()

        For Each item In catalog.Bundles
            For Each subitem In item.Value.Children
                If categories.Contains(subitem.Value.Category) Then
                    _control = LoadControl("../Controls/Offer.ascx")
                    _control.OfferID = item.Value.ID
                    _control.OfferName = item.Value.Name
                    _control.OfferType = OwnerType.Bundle
                    _control.OfferPrice = item.Value.Price.ToString("C")
                    _control.IsAvailable = item.Value.IsAvailable
                    _control.OfferContent = item.Value.MarketingInfo
                    _bundleControls.Add(_control)
                    upgradesAvailable = True
                    Exit For
                End If
            Next
        Next


        For Each item In catalog.ServicePlans
            If categories.Contains(item.Value.Category) Then
                '#####################################################################################
                'CHANGE BY ROD COPELAND - 4/12/2007 - MARKETING REQUEST CHANGE
                '#####################################################################################
                'TO REMOVE THE LIMITED BASIC SERVICE PLAN (EXTERNAL ID OF 29) WAS REQUEST 
                'BY JIM GALLAGHER.  WITH ADDING THE Q2 OFFERS, THE LIMITED BASIC PLAN IS OFFERED AS 
                'PART OF A BUNDLE ONLY, NOT AS A STAND ALONE SERVICE PLAN.  
                'THE FOLLOWING CONDITIONS THAT CHECK FOR EXTERNAL ID 29 ARE INTENED TO SKIP OVER THE 
                'LIMITED BASIC SERVICE PLAN AND NOT ADD IT TO A SERVICE PLAN LIST.  
                'THE END STATE FOR THIS CHANGE WILL ALLOW THE LIMITED BASIC SERVICE 
                'PLAN TO SHOW UP IN THE BUNDLES, JUST NOT AS A STAND ALONE SERVICE PLAN.
                '#####################################################################################
                If Not (item.Value Is Nothing) Then
                    If Not (item.Value.ID Is Nothing) Then
                        'If Not (item.Value.ID.Contains("229")) Then
                        '#######################################################################
                        'CHANGE BY Jonathan Parks - 10/23/2008
                        '#######################################################################
                        'Added the following code to allow us to dynamically modify the list of 
                        'external ID's that should be suppressed on the Offers/AllOffers/Upsell pages
                        'using the "SUPPRESS_EXTERNAL_IDS" value in the Admin.WebConfig table
                        '#######################################################################
                        If ExternalIDs.Suppress(item.Value.ID) Then
                            '#######################################################
                            'DON'T SHOW THIS SPECIFIC SERVICE PLAN
                            '#######################################################
                        Else
                            _control = LoadControl("../Controls/Offer.ascx")
                            _control.OfferID = item.Value.ID
                            _control.OfferName = item.Value.Name
                            _control.OfferType = OwnerType.ServicePlan
                            _control.OfferPrice = item.Value.Price.ToString("C")
                            _control.IsAvailable = item.Value.IsAvailable
                            _control.OfferContent = item.Value.MarketingInfo
                            ' _controls.Add(_control)
                            upgradesAvailable = True
                            Select Case item.Value.Category
                                Case Category.Cable
                                    _cableControls.Add(_control)
                                Case Category.HSD
                                    _HSDControls.Add(_control)
                                Case Category.Telephoney
                                    _telephoneControls.Add(_control)
                            End Select
                        End If
                    End If
                End If




            End If
        Next

        If Not upgradesAvailable Then Response.Redirect("ChooseOptions.aspx")

        LoadControls(_bundleControls, tblBundle, 2)
        LoadControls(_cableControls, tblCableTV, 3)
        LoadControls(_HSDControls, tblInternet, 3)
        LoadControls(_telephoneControls, tblTelephone, 3)

    End Sub

    Private Sub LoadControls(ByVal _controls As ArrayList, ByVal _table As Table, ByVal _columns As Integer)
        Dim row As TableRow
        Dim cell As TableCell
        Dim rowCount As Integer
        Dim i As Integer
        Dim j As Integer

        rowCount = (_controls.Count - _controls.Count Mod _columns) / _columns

        For i = 0 To rowCount
            row = New TableRow()

            For j = 0 To _columns - 1
                cell = New TableCell()
                cell.Style("width") = String.Format("{0}%", (100 / _columns))

                If (i * _columns + j) < _controls.Count Then
                    cell.Controls.Add(_controls(i * _columns + j))
                End If

                row.Cells.Add(cell)
            Next

            _table.Rows.Add(row)
        Next
        If _controls.Count = 0 Then _table.Visible = False
    End Sub

    Protected Sub btnNextTop_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnNextTop.Click
        If Page.IsValid Then
            Response.Redirect("ChooseOptions.aspx")
        End If
    End Sub
End Class
