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


    Private _IsExistingCustomer As Boolean
    Private _bundlesOnly As Boolean

    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("All Offers")
        If Not Page.IsPostBack Then
            Me.Master.OrphanPage = True
        End If
        _bundlesOnly = False
        If Request.Params(QueryStringName.BundlesOnly) IsNot Nothing AndAlso Request.Params(QueryStringName.BundlesOnly).Equals("true") Then
            _bundlesOnly = 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
        Me.ucShoppingCart.HBXInfo_PageName = Me.Master.HBXPageName
        Me.ucShoppingCart.HBXInfo_MLC = Me.Master.HBXMLC
        RenderCatalog()
    End Sub

    Private Sub RenderCatalog()
        Dim _existingServices As List(Of Category)
        Dim _existing As Boolean
        Dim catalog As ICatalog
        Dim item As KeyValuePair(Of String, ICartItem)
        Dim subitem As KeyValuePair(Of String, ISubCartItem)
        Dim _control As Controls_Offer
        Dim _controls As ArrayList

        '#####################################################################
        'JAN 18 07, ROD COPELAND,
        'THIS WAS ADDED FOR MYACCOUNT TO POPULATE THE SERVICEABILITY DETAILS 
        'SO THE USER CAN GO FROM THE MYACCOUNT AREA DIRECTLY INTO THE ORDER 
        'FLOW WITHOUT HAVING TO LOCALIZE. 
        '#####################################################################
        If Request.IsAuthenticated Then
            Dim lHasServiceability As Boolean = AccountManager.Instance.MyAccount_GetServiceability()
        End If

        Dim lAccount As IAccount = AccountManager.Instance.GetAccount()

        _existingServices = lAccount.ExistingService()

        If _existingServices Is Nothing Then
            _existingServices = New List(Of Category)

            ucExisting.Visible = False
            _IsExistingCustomer = False
        Else
            ucExisting.ContentKey = "Offers:All"
            ucExisting.Visible = True
            _IsExistingCustomer = True
        End If

        catalog = CatalogManager.Instance.GetCatalog()

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

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

        _controls = New ArrayList()

        For Each item In catalog.Bundles
            _existing = False

            For Each subitem In item.Value.Children
                If _existingServices.Contains(subitem.Value.Category) Then
                    _existing = True
                    Exit For
                End If
            Next

            '#####################################################################################
            'CHANGE BY Jonathan Parks - 7/22/2008
            '#####################################################################################
            'ADDED EXTERNAL ID 49 TO THIS LOGIC SO IT IS SUPPRESSED AS A BUNDLE
            '#####################################################################################
            If Not (item.Value Is Nothing) Then
                If Not (item.Value.ID Is Nothing) Then
                    'If (item.Value.ID.Contains("149")) Then
                    '#######################################################################
                    'CHANGE BY Jonathan Parks - 9/17/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 pages
                    'using the "SUPPRESS_EXTERNAL_IDS" value in the Admin.WebConfig table
                    '#######################################################################
                    If ExternalIDs.Suppress(item.Value.ID) Then
                        '#######################################################
                        'DON'T SHOW THIS SPECIFIC BUNDLE
                        '#######################################################
                    Else

                        _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("$#.00 /mo")
                        _control.OfferPrice = item.Value.Price.ToString("C")
                        _control.IsAvailable = item.Value.IsAvailable
                        _control.Existing = _existing
                        _control.IsExistingCustomer = _IsExistingCustomer
                        _control.OfferContent = item.Value.MarketingInfo
                        _controls.Add(_control)
                    End If
                End If
            End If
        Next

        'bundles
        If _controls.Count > 0 Then
            LoadControls(_controls, tblBundle, 2)
            tblBundle.Visible = True
        Else
            tblBundle.Visible = False
        End If

        If _bundlesOnly Then
            tblCableTV.Visible = False
            tblInternet.Visible = False
            tblTelephone.Visible = False
        Else
            RenderServicePlans(Category.Cable, tblCableTV, _existingServices.Contains(Category.Cable))
            RenderServicePlans(Category.HSD, tblInternet, _existingServices.Contains(Category.HSD))
            RenderServicePlans(Category.Telephoney, tblTelephone, _existingServices.Contains(Category.Telephoney))
        End If

    End Sub

    Private Sub RenderServicePlans(ByVal _category As Category, ByVal _table As Table, ByVal _existing As Boolean)
        Dim catalog As ICatalog
        Dim servicePlan As KeyValuePair(Of String, ICartItem)
        Dim _control As Controls_Offer
        Dim _controls As ArrayList
        Dim noServicePlans As Boolean = True

        catalog = CatalogManager.Instance.GetCatalog()

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

        _controls = New ArrayList()



        '#######################################################################
        'MODIFICATION   ROD COPELAND    DIGITAL REPACKAGE PROJECT   AUG 29, 2008
        '#######################################################################
        Dim blnHideTheSvcPln As Boolean
        '#######################################################################

        For Each servicePlan In catalog.ServicePlans
            '#######################################################################
            'MODIFICATION   ROD COPELAND    DIGITAL REPACKAGE PROJECT   AUG 29, 2008
            '#######################################################################
            blnHideTheSvcPln = False
            '#######################################################################

            If servicePlan.Value.Category = _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.
                '#####################################################################################
                'CHANGE BY Jonathan Parks - 7/22/2008
                '#####################################################################################
                'ADDED EXTERNAL ID's 48 AND 50 TO THIS LOGIC SO THEY ARE SUPPRESSED AS STAND ALONE
                'SERVICE PLANS.
                '#####################################################################################
                If Not (servicePlan.Value Is Nothing) Then
                    If Not (servicePlan.Value.ID Is Nothing) Then

                        '#######################################################################
                        'CHANGE BY Jonathan Parks - 9/17/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 pages
                        'using the "SUPPRESS_EXTERNAL_IDS" value in the Admin.WebConfig table
                        '#######################################################################
                        If ExternalIDs.Suppress(servicePlan.Value.ID) Then
                            '#######################################################
                            'DON'T SHOW THIS SPECIFIC SERVICE PLAN, LIMITED BASIC
                            '#######################################################
                            blnHideTheSvcPln = True
                        Else

                            '#######################################################################
                            'MODIFICATION   ROD COPELAND    DIGITAL REPACKAGE PROJECT   AUG 29, 2008
                            '#######################################################################
                            Select Case True
                                'Case servicePlan.Value.ID.Contains("229"), servicePlan.Value.ID.Contains("248"), servicePlan.Value.ID.Contains("250")
                                '    '#######################################################
                                '    'DON'T SHOW THIS SPECIFIC SERVICE PLAN, LIMITED BASIC
                                '    '#######################################################
                                '    blnHideTheSvcPln = True
                                Case servicePlan.Value.ID.Contains("263")
                                    '#######################################################################
                                    'MODIFICATION   ROD COPELAND    DIGITAL REPACKAGE PROJECT   AUG 29, 2008
                                    'MODIFICATION   Jonathan Parks  DIGITAL REPACKAGE PROJECT   OCT 13, 2008
                                    '#######################################################################
                                    'IF 263 IS PRESENT, ONLY SHOW THE HERO BANNER - DON'T SHOW THE SERVICE PLAN 
                                    '#######################################################################
                                    blnHideTheSvcPln = True
                                    ucHeroOffer.Visible = True
                                    ucHeroOffer.ContentKey = "Offer:Cable:HeroOffer"

                                    '#########################################################
                                    'MODIFICATION   ROD COPELAND    AUG 28, 2008
                                    'CHANGE MADE FOR DIGITAL REPACKAGING.
                                    '#########################################################
                            End Select
                        End If

                        '#######################################################################
                        'MODIFICATION   ROD COPELAND    DIGITAL REPACKAGE PROJECT   AUG 29, 2008
                        '#######################################################################
                        If Not blnHideTheSvcPln Then
                            _control = LoadControl("../Controls/Offer.ascx")
                            _control.OfferID = servicePlan.Value.ID
                            _control.OfferName = servicePlan.Value.Name
                            _control.OfferType = OwnerType.ServicePlan
                            _control.OfferPrice = servicePlan.Value.Price.ToString("C")
                            _control.IsAvailable = servicePlan.Value.IsAvailable
                            _control.Existing = _existing
                            _control.IsExistingCustomer = _IsExistingCustomer
                            _control.OfferContent = servicePlan.Value.MarketingInfo
                            _controls.Add(_control)
                            noServicePlans = False
                        End If
                        '#######################################################################
                    End If
                End If



            End If
        Next
        If noServicePlans Then
            _table.Visible = False
        Else
            LoadControls(_controls, _table, 3)
            _table.Visible = True
        End If
    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
    End Sub


End Class