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

  Private _IsExistingCustomer As Boolean
  Private catalog As ICatalog
  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
    If Request.Params(QueryStringName.BundlesOnly) IsNot Nothing AndAlso Request.Params(QueryStringName.BundlesOnly).Equals("true") Then
      _bundlesOnly = 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
    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 item As KeyValuePair(Of String, ICartItem)
    Dim subitem As KeyValuePair(Of String, ISubCartItem)
    Dim _control As Controls_AffiliateOffer
    Dim _controls As ArrayList

    _existingServices = AccountManager.Instance.GetAccount().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 = AffliateCatalogManager.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

      _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("$#.00 /mo")
      _control.OfferPrice = item.Value.Price.ToString("C")
      _control.IsAvailable = item.Value.IsAvailable
      _control.Existing = _existing
      _control.OfferContent = item.Value.MarketingInfo
            _control.IsExistingCustomer = _IsExistingCustomer
            _controls.Add(_control)
    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_AffiliateOffer
    Dim _controls As ArrayList
    Dim noServicePlans As Boolean = True

    'catalog = AffliateCatalogManager.Instance.GetCatalog()

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

    _controls = New ArrayList()

    For Each servicePlan In catalog.ServicePlans
      If servicePlan.Value.Category = _category Then
        _control = LoadControl("../Controls/AffiliateOffer.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.OfferContent = servicePlan.Value.MarketingInfo
                _control.IsExistingCustomer = _IsExistingCustomer
                _controls.Add(_control)
        noServicePlans = False
      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