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


    Private _IsExistingCustomer 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
        MyBase.SetTemplate(13)
        Dim _productItem As ProductItem

        If Not IsPostBack Then
            If Request.QueryString(QueryStringName.ProductItem) Is Nothing Then
                'TODO: throw an exception
            Else
                _productItem = CType(Request.QueryString(QueryStringName.ProductItem), ProductItem)
            End If

            Select Case _productItem
                Case ProductItem.CableTV, ProductItem.BasicCableTV, ProductItem.DigitalCableTV, ProductItem.HighDefinitionTV, ProductItem.DigitalCableTV
                    lblCatagory.Text = "Prices & Availability"
                    ltrCategoryName.Text = "Cable TV"
                    ltrCategoryDesc.Text = "Select the Charter Digital Cable&reg; package that's right for you. Remember, you get big savings when you combine Digital Cable with one or more other services in a Charter Bundle. Save big when you select one of the <a href=""#bundles"">bundles</a> below."
                    ucLocalNavigation.Item = MenuItem.CableTV
                    ucLocalNavigation.SubItem2 = NonProductItem.CableTVPricing
                    ucContactUs.Item = MenuItem.CableTV
                    ucSupport.Item = ProductItem.CableTV
                Case ProductItem.Internet
                    lblCatagory.Text = "Prices & Availability"
                    ltrCategoryName.Text = "High-Speed Internet"
                    ltrCategoryDesc.Text = "Select the Charter High-Speed Internet&trade; package that's right for you. Remember, you get big savings when you combine Charter High-Speed with one or more other services in a Charter Bundle. Save big when you select one of the <a href=""#bundles"">bundles</a> below."
                    ucLocalNavigation.Item = MenuItem.Internet
                    ucLocalNavigation.SubItem2 = NonProductItem.InternetPricing
                    ucContactUs.Item = MenuItem.Internet
                    ucSupport.Item = ProductItem.Internet
                Case ProductItem.Telephone
                    lblCatagory.Text = "Prices & Availability"
                    ltrCategoryName.Text = "Telephone"
                    ltrCategoryDesc.Text = "Select Charter Telephone&trade; or save even more by combining Charter Telephone with one or more other services in a Charter Bundle. Save big when you select one of the <a href=""#bundles"">bundles</a> below."
                    ucLocalNavigation.Item = MenuItem.Telephone
                    ucLocalNavigation.SubItem2 = NonProductItem.TelephonePricing
                    ucContactUs.Item = MenuItem.Telephone
                    ucSupport.Item = ProductItem.Telephone
                Case Else
                    'TODO: throw an exception
            End Select

            ucSupport.Mode = SupportMode.SingleProduct

        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 _existingServices As List(Of Category)
        Dim _category As Category
        Dim _existing As Boolean

        Me.ucShoppingCart.HBXInfo_PageName = "products and pricing"
        Me.ucShoppingCart.HBXInfo_MLC = "/orderflow/productselection"

        Select Case ucLocalNavigation.Item
            Case MenuItem.CableTV
                _category = Category.Cable
                'ucExisting.ContentKey = "Offers:CableTV"
            Case MenuItem.Internet
                _category = Category.HSD
                'ucExisting.ContentKey = "Offers:Internet"
            Case MenuItem.Telephone
                _category = Category.Telephoney
                'ucExisting.ContentKey = "Offers:Telephone"
        End Select

        If Request.Params(QueryStringName.CustomerType) Is Nothing Then
            _existingServices = AccountManager.Instance.GetAccount().ExistingService

            If _existingServices Is Nothing OrElse _existingServices.Count = 0 Then
                _existing = False
                _IsExistingCustomer = False
            Else
                _existing = _existingServices.Contains(_category)
                _IsExistingCustomer = True
            End If
        Else
            If Request.Params(QueryStringName.CustomerType).Equals("new") Then
                _existing = False
            End If
        End If
        'ucExisting.Visible = _existing

        RenderCatalog(_category, _existing)
    End Sub

    Private Sub RenderCatalog(ByVal _category As Category, ByVal 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
        Dim noBundles As Boolean = True
        Dim noServicePlans As Boolean = True

        catalog = CatalogManager.Instance.GetCatalog()

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

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

        ltrNoService.Visible = False
        _controls = New ArrayList()

        For Each item In catalog.Bundles
            '#####################################################################################
            '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
                        For Each subitem In item.Value.Children
                            If subitem.Value.Category = _category Then
                                _control = LoadControl("../Controls/Offer.ascx")
                                _control.OfferID = item.Value.ID
                                _control.OfferType = OwnerType.Bundle
                                _control.IsAvailable = item.Value.IsAvailable
                                _control.OfferName = item.Value.Name
                                _control.OfferPrice = item.Value.Price.ToString("C")
                                _control.Existing = existing
                                _control.IsExistingCustomer = _IsExistingCustomer
                                _control.OfferContent = item.Value.MarketingInfo
                                _controls.Add(_control)
                                noBundles = False
                                Exit For
                            End If
                        Next
                    End If
                End If
            End If
        Next

        If noBundles Then
            ' no bundles available 
            tblBundle.Visible = False
        Else
            tblBundle.Visible = True
            LoadControls(_controls, tblBundle, 2)
        End If
        _controls = New ArrayList()



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


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

            If item.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 (item.Value Is Nothing) Then
                    If Not (item.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(item.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 item.Value.ID.Contains("229"), item.Value.ID.Contains("248"), item.Value.ID.Contains("250")
                                '    '#######################################################
                                '    'DON'T SHOW THIS SPECIFIC SERVICE PLAN, LIMITED BASIC
                                '    '#######################################################
                                '    blnHideTheSvcPln = True
                                Case item.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 = item.Value.ID
                            _control.OfferType = OwnerType.ServicePlan
                            _control.IsAvailable = item.Value.IsAvailable
                            _control.OfferName = item.Value.Name
                            _control.OfferPrice = item.Value.Price.ToString("C")
                            _control.Existing = existing
                            _control.IsExistingCustomer = _IsExistingCustomer
                            _control.OfferContent = item.Value.MarketingInfo
                            _controls.Add(_control)
                            noServicePlans = False
                        End If
                        '#######################################################################
                    End If
                End If


            End If
        Next
        If noServicePlans Then
            ' serrvice plans are not availabale
            tblServicePlan.Visible = False
            If noBundles Then
                ' both bundles and service plans are not available 
                ltrNoService.Text = "We're sorry, but currently these services are not available in your area.  If you would like additional information or feel that this is incorrect, you may " & "<a href=""../Errors/MissingCatalog.aspx"">request a follow-up</a>" & " or call us at 1-888-GET CHARTER  (1-888-438-2427)"
                ltrNoService.Visible = True
                ltrCategoryDesc.Visible = False
            End If
        Else
            tblServicePlan.Visible = True
            LoadControls(_controls, tblServicePlan, 3)
        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