Imports System.Web
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.BLCommon
Imports System.Configuration
Imports System.Runtime.Remoting





Public Class InternalCatalogManager

    Private _trackingID As String
    'Private _cm As CatalogClient
    Private _cm As BLCatalogManager

    Private Sub New()

        Dim cmSession As Object = HttpContext.Current.Session(Common.CatalogManagerKey)
        If Not cmSession Is Nothing Then
            _cm = New BLCatalogManager(DirectCast(cmSession, Dictionary(Of String, Object)))
        Else
            _cm = New BLCatalogManager
        End If
        _trackingID = HttpContext.Current.Session.SessionID

        'Try

        '  Common.RegisterChannels()
        '  CatalogManagerProxy = CType(Activator.GetObject(GetType(CatalogClient), CStr(HttpContext.Current.Application("RemoteURL_CatalogManager"))), CatalogClient)
        '  If Not HttpContext.Current.Request.Cookies(CookieName.SessionID) Is Nothing Then
        '    _trackingID = CatalogManagerProxy.Initialize(HttpContext.Current.Request.Cookies(CookieName.SessionID).Value)
        '  Else
        '    _trackingID = CatalogManagerProxy.Initialize(Nothing)
        '    HttpContext.Current.Response.Cookies.Add(New HttpCookie(CookieName.SessionID, TrackingID))
        '  End If
        'Catch ex As Exception
        '  Throw ex
        'End Try
    End Sub

    Public Sub Destory()
        Try
          '  CatalogManagerProxy.Destroy(TrackingID)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Friend ReadOnly Property TrackingID() As String
        Get
            Return _trackingID
        End Get
    End Property

    Friend Property CatalogManagerProxy() As BLCatalogManager
        Get
            Return _cm
        End Get
        Set(ByVal value As BLCatalogManager)
            _cm = value
        End Set
    End Property

    ' returns an instance of Account Manager
    Public Shared Function Instance() As InternalCatalogManager
        Try
            'If Common.RetriveFromSession(Common.CatalogManagerKey) Is Nothing Then
            '  'If user instance does not exist, create one
            '  Common.AddToSession((Common.CatalogManagerKey), New CatalogManager())
            '  Return CType(Common.RetriveFromSession(Common.CatalogManagerKey), CatalogManager)
            'Else
            '  'If user instance already exists, return that
            '  Return CType(Common.RetriveFromSession(Common.CatalogManagerKey), CatalogManager)
            'End If
            Return New InternalCatalogManager()
        Catch ex As Exception
            Throw ex
        End Try
    End Function

#Region "Populate catalog functions"

    ''' <summary>
    ''' Returns an Object of type ICatalog which has Bundles and ServivcePlans.
    ''' If a user is Returning User(cookie information is in Database) populates Cart also.
    ''' </summary>
    ''' <returns>An object of type Icatalog which has Bundles and Service plans</returns>
    ''' <remarks>
    '''  Calls QueryOffersProvider's QueryOffers method, which populates catalog object.
    ''' set's Removable Property to all the features in Serviceplans.
    ''' Feature List is created to use in Grid mode.
    ''' If user is Returning user( already visited site) cart is retrived from Database if persisted.
    ''' Rules will be also populated in provider.
    ''' Before calling this method Account Mangager's Localization or SinIn should be called.
    ''' </remarks>
    Public Function GetCatalog() As ICatalog

        Try
            Return CatalogManagerProxy.GetCatalog(TrackingID)
        Catch ex As Exception
            Throw ex
        End Try

    End Function

    ''' <summary>
    ''' Returns a Dictionary with key as ID and all available features.
    ''' </summary>
    ''' <returns>Retrns the list of Features </returns>
    ''' <remarks>Loops through all the service plans in catalog and 
    ''' adds features from each service plan to the features list if its not already added. 
    ''' Also creates ServiceFeatureList which is required to find a service plan when a feature is added in Grid mode.
    ''' </remarks>
    Public Function GetFeatures() As Dictionary(Of String, ISubCartItem)
        'Dim catalog As Catalog
        Try
            Return (CatalogManagerProxy.GetFeatures(TrackingID))
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function GetChooseOptionsXml() As String
        Try
            Return (CatalogManagerProxy.GetChooseOptionsXml(TrackingID))
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function GetBuildYourOwnXml() As String
        Try
            Return (CatalogManagerProxy.GetBuildYourOwnXml(TrackingID))
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function GetShowPriceList() As List(Of String)
        Try
            Return (CatalogManagerProxy.GetShowPriceList(TrackingID))
        Catch ex As Exception
            Throw ex
        End Try
    End Function

#End Region

#Region "Cart related functions"

    ''' <summary>
    '''  Adds Bundles and Service Plans to the shopping cart.
    ''' </summary>
    ''' <param name="itemID"> ID of BUndle/ Service Plan that should be added to cart</param>
    ''' <param name="notice">This is an Instance of Notify Delegate that is used to notify UI about any changes happend to Shopping Cart. </param>
    ''' <remarks> 
    ''' From given ItemID checks to see if it a bundle or service plan, and calls AddItemToCart with appropriate item.
    ''' Dynamic Bundling will be done.
    ''' UndoStack will be cleared.
    ''' Refreshes FeatureList.
    ''' Invokes Notify delegate about any unexpected changes to ShoppingCart.
    ''' Updates Shopping cart price.
    ''' After all the Chages are done adds CatalogManager to session.
    ''' </remarks>
    Public Sub AddCartItem(ByVal itemID As String, ByVal notice As CharterDotCom.CommonEntities.Library.Common.Notify)
        Try
            Dim message As CharterDotCom.CommonEntities.Library.INotification
            message = CatalogManagerProxy.AddCartItem(TrackingID, itemID)
            notice(message)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' Adds a Feature to Shopping Cart.
    ''' </summary>
    ''' <param name="itemID">ID of feature that should be added to cart.</param>
    ''' <param name="notice">This is an Instance of Notify Delegate that is used to notify UI about any changes happend to Shopping Cart. </param>
    ''' <remarks>
    ''' Finds appropriate service plan based on the current selection and the existing items in the cart by calling FindServicePlan method.
    ''' Adds the ServicePlan found in above method to cart. 
    ''' selected item will be added as sub item to the service plan.
    ''' If there is a change in Service plan with current selections Carries any customization selections to new ServicePlan.
    ''' Applies Dynamic Bundling.
    ''' UndoStack will be cleared.
    ''' Refreshes FeatureList.
    ''' Invokes Notify delegate about any unexpected changes to ShoppingCart.
    ''' Updates Shopping cart price.
    ''' After all the Chages are done adds CatalogManager to session.
    ''' </remarks>
    Public Sub AddSubCartItemWithParent(ByVal itemID As String, ByVal parentid As String, ByVal notice As CharterDotCom.CommonEntities.Library.Common.Notify)
        Try
            Dim message As CharterDotCom.CommonEntities.Library.INotification
            message = CatalogManagerProxy.AddSubCartItemWithParent(TrackingID, itemID, parentid)
            notice(message)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub AddSubCartItem(ByVal itemID As String, ByVal Quantity As Integer, ByVal notice As CharterDotCom.CommonEntities.Library.Common.Notify)
        Try
            Dim message As CharterDotCom.CommonEntities.Library.INotification
            message = CatalogManagerProxy.AddSubCartItem(TrackingID, itemID, Quantity)
            notice(message)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub AddSubCartItem(ByVal itemID As String, ByVal TextEntry As String, ByVal notice As CharterDotCom.CommonEntities.Library.Common.Notify)
        Try
            Dim message As CharterDotCom.CommonEntities.Library.INotification
            message = CatalogManagerProxy.AddSubCartItem(TrackingID, itemID, TextEntry)
            notice(message)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' this method provides functionality to remove Bundle / Service Plan / Promotion from the shopping cart.
    ''' </summary>
    ''' <param name="itemID">ID of BUndle/ Service Plan that should be removed from cart</param>
    ''' <param name="notice">This is an Instance of Notify Delegate that is used to notify UI about any changes happend to Shopping Cart. </param>
    ''' <remarks>
    ''' From given ItemID finds the item, and calls RemoveItemFromCart.
    ''' Dynamic Bundling will be done.
    ''' Updates UndoStack with the new item.
    ''' Refreshes FeatureList.
    ''' Invokes Notify delegate about any unexpected changes to ShoppingCart.
    ''' Updates Shopping cart price.
    ''' After all the Chages are done adds CatalogManager to session.
    ''' </remarks>
    Public Sub RemoveCartItem(ByVal itemID As String, ByVal notice As CharterDotCom.CommonEntities.Library.Common.Notify)
        Try
            Dim message As CharterDotCom.CommonEntities.Library.INotification
            message = CatalogManagerProxy.RemoveCartItem(TrackingID, itemID)
            notice(message)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="itemID">ID of feature that should be removed from cart</param>
    ''' <param name="notice">This is an Instance of Notify Delegate that is used to notify UI about any changes happend to Shopping Cart. </param>
    ''' <remarks>
    ''' From given ItemID finds the feature.
    ''' Dynamic Bundling will be done.
    ''' Updates UndoStack with the new item, and removes that feature from UserSelectedList.
    ''' Finds service plan with the new UserSelectionList, by calling FindServiceplan.
    ''' Adds the ServicePlan found in above method to cart. 
    ''' If there is a change in Service plan with current selections Carries any customization selections to new ServicePlan.
    ''' Refreshes Catalog,FeatureList.
    ''' Invokes Notify delegate about any unexpected changes to ShoppingCart.
    ''' Updates Shopping cart price.
    ''' After all the Chages are done adds CatalogManager to session.
    ''' </remarks>
    Public Sub RemoveSubCartItem(ByVal itemID As String, ByVal parentid As String, ByVal notice As CharterDotCom.CommonEntities.Library.Common.Notify)
        Try
            Dim message As CharterDotCom.CommonEntities.Library.INotification
            message = CatalogManagerProxy.RemoveSubCartItem(TrackingID, itemID, parentid)
            notice(message)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' Stores Shopping Cart Information in Charter.com database, with cookie id.
    ''' </summary>
    ''' <remarks> </remarks>
    Public Sub PersistShoppingcart()
        Try
            CatalogManagerProxy.PersistShoppingcart(TrackingID)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' Undo a Previous Remove.
    ''' </summary>
    ''' <param name="notice">This is an Instance of Notify Delegate that is used to notify UI about any changes happend to Shopping Cart. </param>
    ''' <remarks>
    ''' Adds the last added item from stack to Shopping Cart by using AddCartItem.
    ''' Updates UnDoRemove Flag based on Stack count.
    ''' </remarks>
    ''' 
    Public Sub UndoRemove(ByVal notice As CharterDotCom.CommonEntities.Library.Common.Notify)
        Try
            Dim message As CharterDotCom.CommonEntities.Library.INotification
            message = CatalogManagerProxy.UndoRemove(TrackingID)
            notice(message)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' Adds a choice to Option of a ServicePlan in the Shopping Cart.
    ''' </summary>
    ''' <param name="itemID">Id of the Choice to be Added.</param>
    ''' <param name="Quantity">Number of above feature to be added.</param>
    ''' <remarks>
    ''' Finds Feature(choice) with the given ID,Adds this feature to UserSelectionList.
    ''' Refreshes the Catalog with this new selection to option.
    ''' Updates Cart Price.
    ''' After all the changes adds CatalogManager to session.
    ''' </remarks>
    Public Sub AddOption(ByVal itemID As String, Optional ByVal Quantity As Integer = Nothing)
        Try
            CatalogManagerProxy.AddOption(TrackingID, itemID, Quantity)
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    ''' <summary>
    ''' Removes a choice from Option of a ServicePlan in the Shopping Cart.
    ''' </summary>
    ''' <param name="itemID">Id of the Choice to be Removed.</param>
    ''' <remarks>
    ''' Finds Feature(choice) with the given ID,Removes this feature from UserSelectionList.
    ''' Refreshes the Catalog with this UserSelectionList.
    ''' Updates Cart Price.
    ''' After all these changes, adds CatalogManager to session.
    ''' </remarks>
    Public Sub RemoveOption(ByVal itemID As String)
        Try
            CatalogManagerProxy.RemoveOption(TrackingID, itemID)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region

End Class

