Imports CharterDotCom.CommonEntities.Library.Enumerations


<Serializable()> _
Public Class Rules

    Private m_conflictingFeatures As Dictionary(Of String, ConflictFeatureRule)
    Private m_servicePlansPriority As Dictionary(Of String, ServicePlanPriority)
    Private m_featureDependencies As Dictionary(Of String, FeatureDependency)
    Private m_conflictingChoices As Dictionary(Of String, ConflictFeatureRule)
    Private m_defaults As List(Of String)
    Private m_exceptionalFeatures As Dictionary(Of String, ExceptionalBehavior)


  Public Sub New(ByVal conflictFeatureList As Dictionary(Of String, ConflictFeatureRule), ByVal priorityList As Dictionary(Of String, ServicePlanPriority))
        m_conflictingFeatures = conflictFeatureList
        m_servicePlansPriority = priorityList
  End Sub

  Public Function GetConflictingFeatures(ByVal featureID As String) As List(Of String)
        If m_conflictingFeatures.ContainsKey(featureID) Then
            Return m_conflictingFeatures(featureID).conflictingFeatures
        End If
    Return Nothing
  End Function

    Public Function PriorityServicePlan(ByVal serviceID1 As String, ByVal serviceID2 As String) As String

        '#########################################################################################
        'MODIFICATION   ROD COPELAND    MARCH 19, 2008
        '#########################################################################################
        'CHANGES TO THIS FUNCTION WERE ADDED TO FORTIFY THE FACT THAT THE CODE WAS INDEXING 
        '_SERVICEPLANSPRIORTIY COLLECTION WITH OUT VERIFYING THE KEY EVEN EXISTED.  THIS PROBLEM 
        'LEAD TO AN EXCEPTION BEING THROWN.
        '#########################################################################################
        'IF EITHER ONE IS MISSING, JUST SEND THE OTHER SINCE NO COMPARISON COULD BE DONE 
        'BETWEEN THE TWO.  IF BOTH ARE MISSING, RETURN EMPTY STRING.
        '#########################################################################################
        If serviceID1 Is Nothing Then
            If Not (serviceID2 Is Nothing) Then
                Return serviceID2
            End If
        End If
        If serviceID2 Is Nothing Then
            If Not (serviceID1 Is Nothing) Then
                Return serviceID1
            End If
        End If

        '#########################################################################################
        'MODIFICATION   ROD COPELAND    MARCH 19, 2008
        '#########################################################################################
        'THE VALUE OF _servicePlansPriority PULLS FROM THE WEB.CONFIG TABLE IN THE DB, THE VALUE 
        'OF 'BUSINESSRULES' UNDER THE 'PROD.REMOTINGHOST'.
        '###########################################################################################
        If (Not (serviceID1 Is Nothing)) And (Not (serviceID2 Is Nothing)) Then
            If Not (m_servicePlansPriority Is Nothing) Then
                If m_servicePlansPriority.ContainsKey(serviceID1) Then
                    If m_servicePlansPriority.ContainsKey(serviceID2) Then
                        If m_servicePlansPriority(serviceID1).SelectionPrioprity < m_servicePlansPriority(serviceID2).SelectionPrioprity Then
                            Return serviceID1
                        Else
                            Return serviceID2
                        End If
                    Else
                        Return serviceID1 'DEFAULT SINCE WE DONT KNOW THE PRIORITY
                    End If
                Else
                    Return serviceID1 'DEFAULT SINCE WE DONT KNOW THE PRIORITY
                End If
            Else
                Return serviceID1 'DEFAULT SINCE WE DONT KNOW THE PRIORITY
            End If
        Else
            Return serviceID1 'DEFAULT SINCE WE DONT KNOW THE PRIORITY
        End If



    End Function

  Public Function PriorityServicePlan(ByVal list As Dictionary(Of String, Integer)) As String
    Dim priorityID As String = String.Empty
    Dim priority As Integer = 9999
    For Each id As String In list.Keys
            If priority > m_servicePlansPriority(id).SelectionPrioprity Then
                priority = m_servicePlansPriority(id).SelectionPrioprity
                priorityID = id
            End If
    Next
    Return priorityID
  End Function


  Public Function GetPriorityServicePlan(ByVal list As List(Of String)) As String
    Dim priorityID As String = String.Empty
    Dim priority As Integer = 9999
        For Each id As String In list
            If m_servicePlansPriority.ContainsKey(id) Then
                If priority > m_servicePlansPriority(id).SelectionPrioprity Then
                    priority = m_servicePlansPriority(id).SelectionPrioprity
                    priorityID = id
                End If
            End If
        Next
    Return priorityID
  End Function

    'returns priority(base) service plan in a given category
  Public Function GetPriorityServicePlan(ByVal category As GWYCategory) As String
    Dim priorityID As String = String.Empty
    Dim priority As Integer = 99999
    Dim spp As ServicePlanPriority
    ' loop through all the entries in service plan priority list
        For Each id As String In m_servicePlansPriority.Keys
            spp = m_servicePlansPriority(id)
            ' category is same and if this is a prioritised service plan update priority Id ans priority
            If spp.Category.Equals(category) AndAlso priority > spp.SelectionPrioprity Then
                priority = spp.SelectionPrioprity
                priorityID = id
            End If
        Next
    Return priorityID
  End Function

  Public Function GetDependentFeatures(ByVal featureID As String) As List(Of FeatureDependency.DependentFeature)
        If Not m_featureDependencies Is Nothing AndAlso m_featureDependencies.ContainsKey(featureID) Then
            Return m_featureDependencies(featureID).DependentFeatures
        End If
    Return Nothing
  End Function

  Public Function GetFeatureDependencies() As Dictionary(Of String, FeatureDependency)
        Return m_featureDependencies
  End Function

  Public Function GetConflictiongChoices(ByVal choiceID As String) As List(Of String)
        If Not m_conflictingChoices Is Nothing AndAlso m_conflictingChoices.ContainsKey(choiceID) Then
            Return m_conflictingChoices(choiceID).conflictingFeatures
        End If
    Return Nothing
  End Function

  Public Function GetDefaultChoices() As List(Of String)
        Return m_defaults
  End Function

  Public Function GetExceptionalFeatures() As Dictionary(Of String, ExceptionalBehavior)
        Return m_exceptionalFeatures
  End Function

  Public Function IsExceptionalFeature(ByVal featureID As String) As Boolean
        If m_exceptionalFeatures.ContainsKey(featureID) Then
            Return True
        End If
    Return False
  End Function

  Public Function GetExceptionalBehavior(ByVal offerID As String) As ExceptionalBehavior
        If m_exceptionalFeatures.ContainsKey(offerID) Then
            Return m_exceptionalFeatures(offerID)
        End If
    Return Nothing
  End Function
  <Serializable()> _
Public Class ServicePlanPriority
        Private m_serviceID As String
        Private m_category As GWYCategory
        Private m_priority As Integer

    Public Sub New(ByVal serviceID As String, ByVal category As GWYCategory, ByVal priority As Integer)
            m_serviceID = GwyItemType.ServicePlan & serviceID
            m_category = category
            m_priority = priority
    End Sub

    Public Property ServiceID() As String
      Get
                Return m_serviceID
      End Get
      Set(ByVal value As String)
                m_serviceID = value
      End Set
    End Property

    Public Property Category() As GWYCategory
      Get
                Return m_category
      End Get
      Set(ByVal value As GWYCategory)
                m_category = value
      End Set
    End Property

    Public Property SelectionPrioprity() As Integer
      Get
                Return m_priority
      End Get
      Set(ByVal value As Integer)
                m_priority = value
      End Set
    End Property

  End Class

  <Serializable()> _
Public Class ConflictFeatureRule
        Private m_featureID As String
        Private m_conflictingFeatures As List(Of String)

    Public Sub New(ByVal featureID As String)
            m_featureID = GwyItemType.Feature & featureID
            m_conflictingFeatures = New List(Of String)
    End Sub

    Public Property FeatureID() As String
      Get
                Return m_featureID
      End Get
      Set(ByVal value As String)
                m_featureID = value
      End Set
    End Property

    Public Property conflictingFeatures() As List(Of String)
      Get
                Return m_conflictingFeatures
      End Get
      Set(ByVal value As List(Of String))
                m_conflictingFeatures = value
      End Set
    End Property

    Public Sub AddToConflictingFeatures(ByVal featureId As String)
            m_conflictingFeatures.Add(GwyItemType.Feature & featureId)
    End Sub

  End Class

  <Serializable()> _
Public Class FeatureDependency

        Private m_featureID As String
        Private m_dependentFeatures As List(Of DependentFeature)
        Private m_type As G2BFeatureType

    Public Sub New(ByVal id As String, ByVal dependentFeatures As List(Of DependentFeature), ByVal type As G2BFeatureType)
            m_featureID = id
            m_dependentFeatures = dependentFeatures
            m_type = type
    End Sub

    Public ReadOnly Property FeatureID() As String
      Get
                Return m_featureID
      End Get
    End Property

    Public ReadOnly Property DependentFeatures() As List(Of DependentFeature)
      Get
                Return m_dependentFeatures
      End Get
    End Property

    Public ReadOnly Property Type() As G2BFeatureType
      Get
                Return m_type
      End Get
    End Property
    <Serializable()> _
    Public Class DependentFeature

            Private m_featureID As String
            Private m_min As Integer
            Private m_max As Integer
            Private m_selectionType As G2BFeatureType
            Private m_type As DepedencyType

      Public Sub New(ByVal id As String, ByVal choiceType As DepedencyType, ByVal selectType As G2BFeatureType, ByVal min As Integer, ByVal max As Integer)
                m_featureID = GwyItemType.Feature & id
                m_type = choiceType
                m_selectionType = selectType
                m_min = min
                m_max = max
      End Sub

      Public ReadOnly Property FeatureID() As String
        Get
                    Return m_featureID
        End Get
      End Property

      Public ReadOnly Property SelectType() As G2BFeatureType
        Get
                    Return m_selectionType
        End Get
      End Property

      Public ReadOnly Property Type() As DepedencyType
        Get
                    Return m_type
        End Get
      End Property

      Public Property MinAllowed() As Integer
        Get
                    Return m_min
        End Get
        Set(ByVal value As Integer)
                    m_min = value
        End Set
      End Property

      Public Property MaxAllowed() As Integer
        Get
                    Return m_max
        End Get
        Set(ByVal value As Integer)
                    m_max = value
        End Set
      End Property

    End Class

  End Class

  <Serializable()> _
Public Class ExceptionalBehavior
        Private m_id As String
        Private m_type As ExceptionalItemType
        Private m_display As DisplayType
        Private m_action As ExceptionalAction

    Public Sub New(ByVal id As String, ByVal type As ExceptionalItemType, ByVal display As DisplayType, ByVal action As ExceptionalAction)
            m_id = GwyItemType.Feature & id
            m_type = type
            m_display = display
            m_action = action
    End Sub

    Public ReadOnly Property ID() As String
      Get
                Return m_id
      End Get
    End Property

    Public ReadOnly Property Type() As ExceptionalItemType
      Get
                Return m_type
      End Get
    End Property

    Public ReadOnly Property DisplayType() As DisplayType
      Get
                Return m_display
      End Get
    End Property

    Public ReadOnly Property Action() As ExceptionalAction
      Get
                Return m_action
      End Get
    End Property

  End Class


End Class
