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

Partial Class Order_VisitorsUpsell
    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 (acct.AffliateID = CharterDotCom.CommonEntities.Library.Common.m_CHARTER_AFFLIATE_ID) Then
                Response.Redirect("Upsell.aspx")
            End If
    End If
    If Not Page.IsPostBack Then
      Me.Master.OrphanPage = True
    End If

    If SelectedItemID > 0 Then
      AffliateCatalogManager.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 = AffliateCatalogManager.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("AffiliateChooseOptions.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("AffiliateChooseOptions.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_AffiliateOffer
    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 = AffliateCatalogManager.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/AffiliateOffer.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
        _control = LoadControl("../Controls/AffiliateOffer.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
    Next

    If Not upgradesAvailable Then Response.Redirect("AffiliateChooseOptions.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("AffiliateChooseOptions.aspx")
    End If
  End Sub
End Class
