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 System.Security.Permissions
Imports Microsoft.Practices.EnterpriseLibrary.Logging
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Web

Public Class BLCommon

#Region "Manage Session Data"

    'Friend Shared Sub AddToSession(ByVal name As String, ByVal value As Object)
    '  Try
    '    HttpContext.Current.Session.Add(name, value)
    '  Catch ex As Exception
    '    Throw New ApplicationException("Error adding data to session")
    '  End Try
    'End Sub

    'Friend Shared Function RetriveFromSession(ByVal name As String) As Object
    '  Try
    '    Return HttpContext.Current.Session(name)
    '  Catch ex As Exception
    '    Throw New ApplicationException("Error retriving session data")
    '  End Try
    'End Function

    'Friend Shared Sub RemoveFromSession(ByVal name As String)
    '  Try
    '    HttpContext.Current.Session.Remove(name)
    '  Catch ex As Exception
    '    Throw New ApplicationException("Error removing Session data: Specified name couldn't find")
    '  End Try
    'End Sub

#End Region

#Region "Constants"
    Friend Const CatalogKey As String = "Catalog"
    Friend Const FeaturesKey As String = "Features"
    Friend Const ServiceFeatureAssociationKey As String = "ServiceFeatureAssociation"
    Friend Const FTAKey As String = "FTAID"
    Friend Const LocaleKey As String = "LocaleID"
    Friend Const AvailableServicesKey As String = "AvailableServices"
    Friend Const CatalogManagerKey As String = "CatalogManager"
    Friend Const AffiliateCatalogManagerKey As String = "AffiliateCatalogManager"
    Friend Const ContentManagerKey As String = "ContentManager"
    Friend Const AccountManagerKey As String = "AccountManager"
    Friend Const OrderManagerKey As String = "OrderManager"
    Friend Const AffiliateOrderManagerKey As String = "AffiliateOrderManager"
    Friend Const SiteManagerKey As String = "SiteManager"
    Friend Const AddressKey As String = "Address"
    Friend Const CartKey As String = "CartItems"
    Friend Const CookieKey As String = "CookieID"

    Friend Const Serviceablity_Basic As String = "Basic"
    Friend Const Serviceablity_DVR As String = "DVR"
    Friend Const Serviceablity_Digital As String = "Digital"
    Friend Const Serviceablity_HDTV As String = "HDTV"
    Friend Const Serviceablity_VOD As String = "VOD"
    Friend Const Serviceablity_HSD As String = "HSD"
    Friend Const Serviceablity_Telephony As String = "Telephony"
    Friend Const Serviceablity_Data As String = "Data"
    Friend Const Serviceablity_Video As String = "Video"
    Friend Const Serviceablity_Voice As String = "Voice"

    Friend Const AssemblyName As String = "Charter.Website.BL.Services"

    Friend Const PAYMENT_EMAIL_SENDER As String = "PAYMENT_EMAIL_SENDER"
    Friend Const CHARTER_EMAIL_RECEIVER As String = "CHARTER_EMAIL_RECEIVER"
    Friend Const ORDER_EMAIL_SENDER As String = "ORDER_EMAIL_SENDER"
    Friend Const EXCEPTION_MAIL_RECEIVER As String = "EXCEPTION_MAIL_RECEIVER"
    Friend Const EXCEPTION_EMAIL_SENDER As String = "EXCEPTION_EMAIL_SENDER"
    Friend Const SMTP_SERVER As String = "SMTP_SERVER"


#End Region

#Region "Enumerations"

    Public Enum Receivers

        TV_NUMBERS = 10
        TV_1 = 11
        TV_2 = 12
        TV_3 = 13
        TV_4 = 14

        NO_RECEIVER = 19
        INCLUDED = 1
        DVR_1 = 2
        DVR_MULTI = 3
        HD = 4
        HD_1 = 5
        HD_MULTI = 6
        RECEIVER = 7
        EQUIP_2 = 8
        HD_EQUIP_2 = 9
        HDTV_SERVICE = 15
        DVR_SERVICE = 16
        HDTV_OR_DVR_SERVICE = 17

        HDTV_TIER = 18

    End Enum
    Public Enum StatementCode
        Telephone = 1
        Video_Data = 2
        Data = 3
        Misc1 = 4
        Misc = 5
        All = 6
    End Enum

#End Region

#Region "Type Conversions"

    Public Shared Function ConvertToUICategory(ByVal c As GWYCategory) As Category
        Select Case c
            Case GWYCategory.Cable
                Return Category.Cable
            Case GWYCategory.HSD
                Return Category.HSD
            Case GWYCategory.Telephoney
                Return Category.Telephoney
            Case GWYCategory.UnSpecified
                Return Category.UnSpecified
            Case Else
                Throw New ApplicationException("UnKnown Category")
        End Select
    End Function

    Public Shared Function ConvertToGWYCategory(ByVal c As Category) As GWYCategory
        Select Case c
            Case Category.Cable
                Return GWYCategory.Cable
            Case Category.HSD
                Return GWYCategory.HSD
            Case Category.Telephoney
                Return GWYCategory.Telephoney
            Case Category.UnSpecified
                Return GWYCategory.UnSpecified
            Case Else
                Throw New ApplicationException("UnKnown Category")
        End Select
    End Function

    'Public Shared Function ConvertToUIItemType(ByVal type As BLItemType) As ItemType
    '  Select Case type
    '    Case BLItemType.Bundle
    '      Return ItemType.Bundle
    '    Case BLItemType.Feature
    '      Return ItemType.Feature
    '    Case BLItemType.Promotion
    '      Return ItemType.Promotion
    '    Case BLItemType.ServicePlan
    '      Return ItemType.ServicePlan
    '    Case Else
    '      Throw New ApplicationException("UnKnown Item Type")
    '  End Select
    'End Function

    'Public Shared Function ConvertToBLItemType(ByVal type As BLItemType) As ItemType
    '  Select Case type
    '    Case BLItemType.Bundle
    '      Return ItemType.Bundle
    '    Case BLItemType.Feature
    '      Return ItemType.Feature
    '    Case BLItemType.Promotion
    '      Return ItemType.Promotion
    '    Case BLItemType.ServicePlan
    '      Return ItemType.ServicePlan
    '    Case Else
    '      Throw New ApplicationException("UnKnown Item Type")
    '  End Select
    'End Function

    Public Shared Function ConvertTOUIFeatureType(ByVal fType As GWYFeatureType) As FeatureType
        Select Case fType
            Case GWYFeatureType.IsCustomization
                Return FeatureType.IsCustomization
            Case GWYFeatureType.IsIncluded
                Return FeatureType.IsIncluded
            Case GWYFeatureType.IsOptional
                Return FeatureType.IsOptional
            Case GWYFeatureType.IsPlaceholder
                Return FeatureType.IsPlaceholder
            Case GWYFeatureType.NotSpecified
                Return FeatureType.NotSpecified
            Case Else
                Throw New ApplicationException("UnKnown Feature Type")
        End Select
    End Function

    Public Shared Function ConvertTOGWYFeatureType(ByVal fType As FeatureType) As GWYFeatureType
        Select Case fType
            Case FeatureType.IsCustomization
                Return GWYFeatureType.IsCustomization
            Case FeatureType.IsIncluded
                Return GWYFeatureType.IsIncluded
            Case FeatureType.IsOptional
                Return GWYFeatureType.IsOptional
            Case FeatureType.IsPlaceholder
                Return GWYFeatureType.IsPlaceholder
            Case FeatureType.NotSpecified
                Return GWYFeatureType.NotSpecified
            Case Else
                Throw New ApplicationException("UnKnown Feature Type")
        End Select
    End Function

    Public Shared Function ConvertToUIOwnerType(ByVal type As GWYOwnerType) As OwnerType
        Select Case type
            Case GWYOwnerType.Bundle
                Return OwnerType.Bundle
            Case GWYOwnerType.Feature
                Return OwnerType.Feature
            Case GWYOwnerType.Other
                Return OwnerType.Other
            Case GWYOwnerType.ServicePlan
                Return OwnerType.ServicePlan
            Case Else
                Throw New ApplicationException("UnKnown Owner Type")
        End Select

    End Function

    Public Shared Function ConvertToGWYOwnerType(ByVal type As OwnerType) As GWYOwnerType
        Select Case type
            Case OwnerType.Bundle
                Return GWYOwnerType.Bundle
            Case OwnerType.Feature
                Return GWYOwnerType.Feature
            Case OwnerType.Other
                Return GWYOwnerType.Other
            Case OwnerType.ServicePlan
                Return GWYOwnerType.ServicePlan
            Case OwnerType.CharterMessages
                Return GWYOwnerType.CharterMessages
            Case Else
                Throw New ApplicationException("UnKnown Owner Type")
        End Select

    End Function

    Public Shared Function ConvertToOwnerType(ByVal type As String) As OwnerType
        Select Case type
            Case OwnerType.Bundle.ToString
                Return OwnerType.Bundle
            Case OwnerType.Feature.ToString
                Return OwnerType.Feature
            Case OwnerType.Other.ToString
                Return OwnerType.Other
            Case OwnerType.ServicePlan.ToString
                Return OwnerType.ServicePlan
            Case OwnerType.ZipCode.ToString
                Return OwnerType.ZipCode
            Case Else
                Throw New ApplicationException("UnKnown Owner Type")
        End Select

    End Function

    Public Shared Function ConverToUIBillingVendorType(ByVal bv As GWYBillingVendorType) As BillingVendorType
        Select Case bv
            Case GWYBillingVendorType.CSG
                Return BillingVendorType.CSG
            Case GWYBillingVendorType.ICOM
                Return BillingVendorType.ICOM
            Case Else
                Throw New ApplicationException("Unknown Type")
        End Select
    End Function

    Public Shared Function ConverToGWYBillingVendorType(ByVal bv As BillingVendorType) As GWYBillingVendorType
        Select Case bv
            Case BillingVendorType.CSG
                Return GWYBillingVendorType.CSG
            Case BillingVendorType.ICOM
                Return GWYBillingVendorType.ICOM
            Case Else
                Throw New ApplicationException("Unknown Type")
        End Select
    End Function

    'Public Shared Function ConvertTOUIFeatureCategory(ByVal category As FeatureCategory) As FeatureCategory
    '  Select Case category
    '    Case GWYCategory.C
    '  End Select
    'End Function

    Public Shared Function ConvertToGwyCardType(ByVal type As String) As EntityG2BServices.CreditCardEnum
        Select Case type
            Case CreditCardType.Discover.ToString
                Return EntityG2BServices.CreditCardEnum.m_Discover
            Case CreditCardType.Master.ToString
                Return EntityG2BServices.CreditCardEnum.m_MC
            Case CreditCardType.Visa.ToString
                Return EntityG2BServices.CreditCardEnum.m_Visa
            Case Else
                Throw New ApplicationException("Unknown Type")
        End Select
    End Function

    Public Shared Sub ConvertGwyAddressToBLAddress(ByRef BLAddress As BLAddress, ByVal GwyAddress As Address)
        Try
            BLAddress.Street1 = GwyAddress.Street1
            BLAddress.Street2 = GwyAddress.Street2
            BLAddress.State = GwyAddress.State
            BLAddress.City = GwyAddress.City
            BLAddress.Zip = GwyAddress.Zip
            BLAddress.Zip4 = GwyAddress.Zip4
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Shared Function ConvertBLAddressToGwyAddress(ByVal address As BLAddress) As Address
        If Not address Is Nothing Then
            Dim zip4 As String
            If address.Zip4 Is String.Empty Then
                zip4 = Nothing
            Else
                zip4 = address.Zip4
            End If
            Dim gwyAddress As New Address(address.Street1, address.Street2, address.City, address.State, address.Zip, zip4)
            Return gwyAddress
        End If
        Return Nothing
    End Function

    Public Shared Function ConverToGWYLOB(ByVal blLOB As List(Of BLServiceType)) As List(Of ServiceType)
        Dim list As New List(Of ServiceType)
        For Each s As BLServiceType In blLOB
            list.Add(ConverToGWYServiceType(s))
        Next
        Return list
    End Function

    Public Shared Function ConverToGWYServiceType(ByVal service As BLServiceType) As ServiceType
        Select Case service
            Case BLServiceType.Basic
                Return ServiceType.Basic
            Case BLServiceType.Digital
                Return ServiceType.Digital
            Case BLServiceType.DVR
                Return ServiceType.DVR
            Case BLServiceType.HDTV
                Return ServiceType.HDTV
            Case BLServiceType.HSD
                Return ServiceType.HSD
            Case BLServiceType.Telephony
                Return ServiceType.Telephony
            Case BLServiceType.VOD
            Case Else
                Return ServiceType.NotSpecified
        End Select
    End Function

#End Region

#Region "Common methods Accross Managers"

    Friend Shared Function PopulateGWYShoppingCart(ByVal blSC As ICart) As ShoppingCart
        'Dim cm As CatalogManager = CatalogManager.Instance
        'Dim blSC As ICart = cm.ProductCatalog.Cart
        Dim gwySc As ShoppingCart
        Dim bItem, spItem As Items
        Dim offers As New Dictionary(Of String, Items)

        For Each i As ICartItem In blSC.CartItems.Values
            If i.Type = ItemType.Bundle Then
                bItem = New Items(i.ID, i.Description, GwyItemType.Bundle, , , , "BL")
                For Each si As BLServicePlan In i.Children.Values
                    spItem = PopulateFromServicePlan(si)
                    bItem.SubItems.Add(spItem.ID, spItem)
                Next
                offers.Add(bItem.ID, bItem)
            Else
                spItem = PopulateFromServicePlan(DirectCast(i, BLServicePlan))
                offers.Add(spItem.ID, spItem)
            End If
        Next
        gwySc = New ShoppingCart(offers)
        Return gwySc
    End Function

    Private Shared Function PopulateFromServicePlan(ByVal sp As BLServicePlan) As Items
        Dim spItem, czItem, cItem As Items

        spItem = New Items(sp.ID, sp.Description, GwyItemType.ServicePlan, , , , "BL")
        For Each f As BLFeature In sp.Features.Values
            If f.Choices.Count > 0 AndAlso f.ISSelected Then
                czItem = New Items(f.ID, f.Description, GwyItemType.Customization, , , , "BL")
                For Each c As BLFeature In f.Choices.Values
                    If c.ISSelected Then
                        cItem = New Items(c.ID, c.Description, GwyItemType.Choice, , , , "BL")
                        czItem.SubItems.Add(cItem.ID, cItem)
                    End If
                Next
                spItem.SubItems.Add(czItem.ID, czItem)
            End If
        Next
        Return spItem
    End Function

    Friend Shared Function GetKey(ByVal value As Receivers, ByVal dict As Dictionary(Of String, Receivers)) As String
        For Each temp As String In dict.Keys
            If dict(temp) = value Then
                Return temp
            End If
        Next
        Return String.Empty
    End Function

    Friend Shared Sub LogException(ByVal ex As Exception, ByVal severity As ExceptionSeverity)
        Try
            '###########################################
            'LOGG THE ERROR MESSAGE TO THE ERROR QUEUE
            '###########################################
            Dim ErrorQueueLocation As String = Environment.MachineName & CType(HttpContext.Current.Application("ErrorQueueLocation"), String)
            Dim lMessageQComponent As New MessageQComponent
            lMessageQComponent.AddAMessage(ErrorQueueLocation, ex.Message)
        Catch ex2 As Exception
            '##########################################
            'IF THIS FAILS, JUST SKIP THE LOGGING PART
            '##########################################
        End Try

        'Try
        '  Try
        '    Logger.Write(ex.Message, "Charter.Com Remoting Host")
        '  Catch

        '  End Try
        '  Try
        '    If CType(HttpContext.Current.Application("LogDB"), String) = "true" Then
        '      'LogDatabase(ex, severity)
        '    End If
        '  Catch
        '    Logger.Write(New ApplicationException("Cannot Log to Database"), "Charter.Com Remoting Host")
        '  End Try

        '  If severity <= ExceptionSeverity.High Then
        '    Dim toMail As String = CType(HttpContext.Current.Application(EXCEPTION_MAIL_RECEIVER), String)
        '    Dim fromMail As String = CType(HttpContext.Current.Application(EXCEPTION_EMAIL_SENDER), String)
        '    'SendEMail(fromMail, toMail, ex.Message, ex.Message & ControlChars.NewLine & ex.StackTrace)
        '  End If
        'Catch exx As Exception

        'End Try
    End Sub

    Private Shared Sub LogDatabase(ByVal ex As Exception, ByVal severity As ExceptionSeverity)
        Dim CMD As Data.Common.DbCommand
        Dim DB As Database
        Dim strSQL As String

        Try
            DB = DatabaseFactory.CreateDatabase("Charter.Website.WS.CommerceEngineConnectionString")
            strSQL = "INSERT INTO SESSIONSTATE.Log VALUES('" & Replace(ex.Message, "'", "''") & "','" & Replace(ex.StackTrace, "'", "''") & "'," & severity & ",'" & Now.ToString & "')"

            CMD = DB.GetSqlStringCommand(strSQL)

            Dim retCode As Integer
            retCode = DB.ExecuteNonQuery(CMD)

        Catch exDB As Exception
            Throw exDB
        Finally
            If Not CMD Is Nothing Then
                CMD.Dispose()
            End If
        End Try
    End Sub



#End Region


End Class
