Imports System.Collections.Generic
Imports System.Xml
Imports CharterDotCom.CommonEntities.Library
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.CommonEntities.Library.Common
Imports System.Net

Partial Class Members_Upgrades
    Inherits CharterCMSPage

    Private Categories As List(Of Category)
    Private _document As XmlDocument

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Upgrades")
        _document = New XmlDocument()
        _document.LoadXml(Application("BuildYourOwnXml"))

        Categories = New List(Of Category)
        Categories.Add(Category.Cable)
        Categories.Add(Category.Telephoney)

        If SelectedFeatureID > 0 Then
            CatalogManager.Instance.AddCartItem(SelectedFeatureID, 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 _catalog As ICatalog
        Dim _bundle As ICartItem

        Dim _item As KeyValuePair(Of String, ICartItem)
        Dim _features As Dictionary(Of String, ISubCartItem)
        Dim _subitem As KeyValuePair(Of String, ISubCartItem)
        Dim _control As Controls_Offer
        Dim _bundleControls As ArrayList
        Dim _row As TableRow
        Dim _cell As TableCell
        Dim _rowCount As Integer
        Dim i As Integer
        Dim j As Integer
        Dim sb(6) As StringBuilder
        Dim _found As Boolean

        _existingServices = AccountManager.Instance.GetAccount().ExistingService

        _catalog = CatalogManager.Instance.GetCatalog()

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

        ' Load bundles
        ucShoppingCart.Cart = _catalog.Cart

        _bundleControls = New ArrayList()

        For Each _item In _catalog.Bundles
            _bundle = _item.Value

            For Each _subitem In _bundle.Children
                _control = LoadControl("../Controls/Offer.ascx")
                _control.OfferID = _bundle.ID
                _control.IsAvailable = _bundle.IsAvailable
                _control.OfferName = _bundle.Name
                _control.Existing = True ' TODO: modify 
                _control.OfferType = OwnerType.Bundle
                _bundleControls.Add(_control)
                Exit For
            Next
        Next

        _rowCount = (_bundleControls.Count - _bundleControls.Count Mod 2) / 2

        For i = 0 To _rowCount
            _row = New TableRow()

            For j = 0 To 1
                _cell = New TableCell()
                _cell.Style("width") = "50%"

                If (i * 2 + j) < _bundleControls.Count Then
                    _cell.Controls.Add(_bundleControls(i * 2 + j))
                    _row.Cells.Add(_cell)
                Else
                    If _rowCount > 0 Then
                        _row.Cells.Add(_cell)
                    End If
                End If
            Next

            tblBundle.Rows.Add(_row)
        Next

        For i = 0 To 5
            sb(i) = New StringBuilder()
        Next

        For Each _item In _catalog.ServicePlans
            Select Case _item.Value.Category
                Case Category.Cable
                    If _existingServices Is Nothing Then
                        RenderServicePlans(sb(0), _item.Value, False)
                    Else
                        RenderServicePlans(sb(0), _item.Value, _existingServices.Contains(Category.Cable))
                    End If
                Case Category.HSD
                    If _existingServices Is Nothing Then
                        RenderServicePlans(sb(1), _item.Value, False)
                    Else
                        RenderServicePlans(sb(1), _item.Value, _existingServices.Contains(Category.HSD))
                    End If
                Case Category.Telephoney
                    If _existingServices Is Nothing Then
                        RenderServicePlans(sb(2), _item.Value, False)
                    Else
                        RenderServicePlans(sb(2), _item.Value, _existingServices.Contains(Category.Telephoney))
                    End If
            End Select
        Next

        _features = CatalogManager.Instance.GetFeatures()

        For Each _subitem In _features
            If _document.SelectSingleNode(String.Format("/buildYourOwn/features[@hidden='true']/feature[@id='{0}']", _subitem.Value.ID)) IsNot Nothing Then
                Continue For
            End If

            _found = Categories.IndexOf(_subitem.Value.Category) >= 0
            Select Case _subitem.Value.Category
                Case Category.Cable
                    If _existingServices Is Nothing Then
                        RenderFeature(sb(3), _subitem.Value, _found, False)
                    Else
                        RenderFeature(sb(3), _subitem.Value, _found, _existingServices.Contains(Category.Cable))
                    End If
                Case Category.HSD
                    If _existingServices Is Nothing Then
                        RenderFeature(sb(4), _subitem.Value, _found, False)
                    Else
                        RenderFeature(sb(4), _subitem.Value, _found, _existingServices.Contains(Category.HSD))
                    End If
                Case Category.Telephoney
                    If _existingServices Is Nothing Then
                        RenderFeature(sb(5), _subitem.Value, _found, False)
                    Else
                        RenderFeature(sb(5), _subitem.Value, _found, _existingServices.Contains(Category.Telephoney))
                    End If
            End Select
        Next

        For i = 0 To 5
            If sb(i).Length > 0 Then
                sb(i).Insert(0, "<table width='100%'>")
                sb(i).Append("</table>")
            End If
        Next i

        ltrServicePlanCableTV.Text = sb(0).ToString()
        ltrCableTV.Text = sb(3).ToString()
        ltrServicePlanInternet.Text = sb(1).ToString()
        ltrInternet.Text = sb(4).ToString()
        ltrServicePlanTelephone.Text = sb(2).ToString()
        ltrTelephone.Text = sb(5).ToString()
    End Sub

    Private Sub RenderServicePlans(ByVal sb As StringBuilder, ByVal _servicePlan As ICartItem, ByVal _existing As Boolean)
        sb.Append("<tr>")
        sb.AppendFormat("<td style='width:25%'>{0}</td>", _servicePlan.Name)
        sb.AppendFormat("<td style='width:25%'><a href='{0}'>Learn More</a></td>", _servicePlan.ID)
        sb.AppendFormat("<td style='width:25%'>${0}/mo</td>", _servicePlan.ListPrice)

        If _servicePlan.IsSelected Then
            sb.Append("<td style='text-align:right'>Already In Your Cart</td>")
        ElseIf _existing Then
            sb.Append("<td style='text-align:right'>")
            sb.Append(BuildChatLink())
            sb.Append("</td>")
        ElseIf _servicePlan.IsAvailable Then
            sb.AppendFormat("<td style='text-align:right'><input type='button' onclick='javascript:addFeature({0})' value='Add to Cart'></td>", _servicePlan.ID)
        Else
            sb.Append("<td style='text-align:right'>Not Available</td>")
        End If

        sb.AppendFormat("</tr>")
    End Sub

    Private Sub RenderFeature(ByVal sb As StringBuilder, ByVal feature As ISubCartItem, ByVal _found As Boolean, ByVal _existing As Boolean)
        sb.Append("<tr>")
        sb.AppendFormat("<td style='width:25%'>{0}</td>", feature.Name)
        sb.AppendFormat("<td style='width:25%'><a href='{0}'>Learn More</a></td>", feature.ID)
        sb.AppendFormat("<td style='width:25%'>${0}/mo</td>", feature.ListPrice)

        If feature.IsSelected Then
            sb.Append("<td style='text-align:right'>Already In Your Cart</td>")
        ElseIf _existing Then
            sb.Append("<td style='text-align:right'><a href='../Visitors/chatlive.aspx' target='_charter_com_chat'><img src='../Images/but_chatNow.gif' alt='Chat Live Now' style='padding-top:9px'/></a></td>")
        ElseIf feature.IsAvailable Then
            If _found Then
                sb.Append("<td style='text-align:right'><a id='aChat' href='../Visitors/chatlive.aspx' target='_charter_com_chat'>Chat to Order</td>")
            Else
                sb.AppendFormat("<td style='text-align:right'><input type='button' onclick='javascript:addFeature({0})' value='Add to Cart'></td>", feature.ID)
            End If
        Else
            sb.Append("<td style='text-align:right'>Not Available</td>")
        End If

        sb.AppendFormat("</tr>")
    End Sub

    Protected Function BuildChatLink() As String

        Dim chatHTMLBuilder As New StringBuilder("<img alt='Chat To Oder' src='../Images/but_chatToOrder.gif' height='18' width='90' class='buttonSpacing' onclick=""")
        chatHTMLBuilder.Append("javascript:window.open('../Visitors/chatlive.aspx?Chattype=SALES','_charter_chat','width=550,height=660,left=50,top=15')")

        chatHTMLBuilder.Append(""" />")
        Dim chatHTML As String = chatHTMLBuilder.ToString()

        Return chatHTML

    End Function

End Class
