Imports System.Collections.Generic
Imports System.Text
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.WebSite.Library.BLCommon

Partial Class Order_AffiliateReceipt
    Inherits CharterCMSPage

  Private CategoriesInCart As List(Of Category)


  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Receipt")
        Dim _account As IAccount = Nothing
    Dim _confirmations As Dictionary(Of String, IConfirmation) = Nothing
    Dim _confirmation As KeyValuePair(Of String, IConfirmation) = Nothing
    Dim _appointments As Dictionary(Of ScheduleType, IAffiliateOrder.IAffiliateAppointment) = Nothing
    Dim _appointment As KeyValuePair(Of ScheduleType, IAffiliateOrder.IAffiliateAppointment) = Nothing
    Dim _catalog As ICatalog = Nothing
    Dim _cart As ICart = Nothing
    Dim _items As Dictionary(Of String, ICartItem)
    Dim _item As KeyValuePair(Of String, ICartItem)
    Dim sb As StringBuilder
    Dim dm As DateTime

    If Not Page.IsPostBack Then
      _account = AccountManager.Instance.GetAccount()

      If _account IsNot Nothing Then
        lblLastName.Text = _account.Person.LastName
        lblFirstName.Text = _account.Person.FirstName

        lblServiceStreet1.Text = _account.Address.Street1
        lblServiceStreet2.Text = _account.Address.Street2
        lblServiceCity.Text = _account.Address.City
        lblServiceState.Text = _account.Address.State
        lblServiceZipCode.Text = _account.Address.Zip

        If _account.BillingAddress IsNot Nothing Then
          lblBillingStreet1.Text = _account.BillingAddress.Street1
          lblBillingStreet2.Text = _account.BillingAddress.Street2
          lblBillingCity.Text = _account.BillingAddress.City
          lblBillingState.Text = _account.BillingAddress.State
          lblBillingZipCode.Text = _account.BillingAddress.Zip
        Else
          lblBillingStreet1.Text = _account.Address.Street1
          lblBillingStreet2.Text = _account.Address.Street2
          lblBillingCity.Text = _account.Address.City
          lblBillingState.Text = _account.Address.State
          lblBillingZipCode.Text = _account.Address.Zip
        End If
      End If

      lblOrderDate.Text = DateTime.Now.ToShortDateString

      ' render confirmations

      AffiliateOrderManager.Instance.GetConfirmationInfo(_confirmations)

      If _confirmations IsNot Nothing Then
        sb = New StringBuilder()

        For Each _confirmation In _confirmations
          sb.AppendFormat("{0}: {1}<br />", _confirmation.Key, _confirmation.Value.Number)
        Next

        ltrConfirmations.Text = sb.ToString()
      End If

      ' render appointments

      AffiliateOrderManager.Instance.GetAppointments(_appointments)

      If _appointments IsNot Nothing Then
        sb = New StringBuilder()

        For Each _appointment In _appointments
          Select Case _appointment.Key
            Case ScheduleType.All
              sb.AppendFormat("{0}: ", "Bundle")
            Case ScheduleType.Cable
              sb.AppendFormat("{0}: ", "Cable TV")
            Case ScheduleType.CableAndHSD
              sb.AppendFormat("{0}: ", "Cable TV and High-Speed Internet")
            Case ScheduleType.CableAndTelephone
              sb.AppendFormat("{0}: ", "Cable TV and Telephone")
            Case ScheduleType.HSD
              sb.AppendFormat("{0}: ", "High-Speed Internet")
            Case ScheduleType.HSDAndTelephone
              sb.AppendFormat("{0}: ", "High-Speed Internet and Telephone")
            Case ScheduleType.Telephone
              sb.AppendFormat("{0}: ", "Telephone")
          End Select

          dm = CType(_appointment.Value.InstallDate, DateTime)

          sb.AppendFormat("{0} arriving between {1} and {2}<br />", dm.ToShortDateString(), _appointment.Value.StartTime, _appointment.Value.EndTime)
        Next
        ltrAppointments.Text = sb.ToString()
        'ltrItems.Text = sb.ToString()
      End If

      ' render items and totals

      _catalog = AffliateCatalogManager.Instance.GetCatalog()
      CategoriesInCart = New List(Of Category)

      If _catalog IsNot Nothing Then
        _cart = _catalog.Cart
        _items = _cart.CartItems
        sb = New StringBuilder()

        For Each _item In _items
          sb.AppendFormat("<tr><td>{0}</td><td style='text-align:right'>{1}</td></tr>", _item.Value.Name, String.Format("{0:C}", _item.Value.Price))
          AppendChoices(_item.Value, sb)
        Next

        If sb.Length > 0 Then
          sb.Insert(0, "<table cellpadding='3' cellspacing='0' style='width:100%'>")
          sb.Append("</table>")
          ltrItems.Text = sb.ToString()
        End If

        lblMonthlyTotal.Text = String.Format("{0:c}", _cart.MonthlyTotal)
        lblOneTimeTotal.Text = String.Format("{0:c}", _cart.OneTimeTotal)
        ' TODO: check if the first month charge has been paid
        lblGrandTotal.Text = String.Format("{0:c}", _cart.GrandTotal)
      End If
      'If CategoriesInCart.Count > 1 Then
      'ltrThankYouGift.Text = HttpContext.Current.Application(ApplicationKey.ThankYouGift)
      'ltrThankYouDisclaimer.Text = HttpContext.Current.Application(ApplicationKey.ThankYouDisclaimer)
      'Else
      ltrThankYouGift.Text = ""
      ltrThankYouDisclaimer.Text = ""
      'End If
    End If
  End Sub

  Private Sub AppendChoices(ByVal item As ICartItem, ByVal sb As StringBuilder)
    If item.Type = ItemType.Bundle Then
      'bundle
      For Each sp As ISubCartItem In item.Children.Values
        If Not CategoriesInCart.Contains(sp.Category) Then CategoriesInCart.Add(sp.Category)
        For Each f As ISubCartItem In sp.Children.Values
          If f.Children Is Nothing AndAlso f.Children.Count = 0 Then
            If f.IsSelected AndAlso f.FeatureType = FeatureType.IsCustomization Then
              sb.AppendFormat("<tr><td>{0}</td><td style='text-align:right'>{1}</td></tr>", f.Name, String.Format("{0:C}", f.Price))
            End If
          Else
            For Each c As ISubCartItem In f.Children.Values
              If c.IsSelected AndAlso Not c.PriceFrequency = BLPriceFrequency.OneTime Then
                sb.AppendFormat("<tr><td>{0}</td><td style='text-align:right'>{1}</td></tr>", c.Name, String.Format("{0:C}", c.Price))
              End If
            Next
          End If
        Next
      Next
    Else
      ' service plan
      If Not CategoriesInCart.Contains(item.Category) Then CategoriesInCart.Add(item.Category)
      For Each f As ISubCartItem In item.Children.Values
        If f.Children Is Nothing AndAlso f.Children.Count = 0 Then
          If f.IsSelected AndAlso f.FeatureType = FeatureType.IsCustomization Then
            sb.AppendFormat("<tr><td>{0}</td><td style='text-align:right'>{1}</td></tr>", f.Name, String.Format("{0:C}", f.Price))
          End If
        Else
          For Each c As ISubCartItem In f.Children.Values
            If c.IsSelected AndAlso Not c.PriceFrequency = BLPriceFrequency.OneTime Then
              sb.AppendFormat("<tr><td>{0}</td><td style='text-align:right'>{1}</td></tr>", c.Name, String.Format("{0:C}", c.Price))
            End If
          Next
        End If
      Next
    End If
  End Sub
End Class
