Imports System.Collections.Generic
Imports CharterDotCom.CommonEntities.Library.Enumerations
' 8/2007 jwg added
Imports System.Reflection
Imports System.text

''' <summary>
''' Feature class is a special class based on Customizations
''' returned in the QueryOffers web service class.
''' </summary>
''' <remarks></remarks>
<Serializable()> _
<DebuggerDisplay("Feature: {DisplayFunc}")> _
Public Class Feature
    Private _id As String
    Private _name As String
    Private _category As GWYCategory
    Private _serviceType As ServiceType
    Private _featureCategory As FeatureCategory
    Private _typeofFeature As GWYFeatureType
    Private _description As String
    Private _price As Decimal
    Private _listPrice As Decimal
    Private _priceFrequency As PriceFrequency
    Private _priceDisplay As Boolean
    Private _minChoiceSelection As Integer
    Private _maxChoiceSelection As Integer
    Private _minSelection As Integer
    Private _maxSelection As Integer
    Private _children As Dictionary(Of String, Feature)
    Private _selected As Boolean
    Private _choiceSelection As List(Of String)
    Private _maxLength As Integer
    Private _text As String
    Private _inputType As GwyCustomizationInputType

    Public ReadOnly Property DisplayFunc() As String
        Get
            Dim sb As StringBuilder = New StringBuilder(200)
            sb.Append("ID " & _id & ", " & _name)
            If (Me._children IsNot Nothing) Then
                sb.Append(" " & Me._children.Count & " Features ")
            End If
            If (Me._choiceSelection IsNot Nothing) Then
                sb.Append(" " & Me._choiceSelection.Count & " Selection Choices ")
            End If

            Return sb.ToString()
        End Get
    End Property
    Public Sub New(ByVal id As String, ByVal name As String, ByVal description As String, ByVal category As GWYCategory, ByVal serviceType As ServiceType, ByVal featureCategory As FeatureCategory, _
          ByVal featureType As GWYFeatureType, ByVal price As Decimal, ByVal listPrice As Decimal, _
          ByVal priceFrequency As PriceFrequency, ByVal priceDisplay As Boolean, _
          ByVal children As Dictionary(Of String, Feature), _
          ByVal minChoiceSelection As Integer, ByVal maxChoiceSelection As Integer, _
          ByVal minSelection As Integer, ByVal maxSelection As Integer, ByVal text As String, _
          ByVal maxlength As Integer, ByVal inputType As GwyCustomizationInputType, Optional ByVal cSelection As List(Of String) = Nothing)

        _id = id
        _name = name
        _category = category
        _serviceType = serviceType
        _featureCategory = featureCategory
        _typeofFeature = featureType
        _description = description
        _price = price
        _listPrice = listPrice
        _priceDisplay = priceDisplay
        _priceFrequency = priceFrequency

        ' rule: if price is zero do not display it
        'If (_price = 0) Then
        '    _priceDisplay = False
        'Else
        '    _priceDisplay = True
        'End If

        If _children Is Nothing Then
            _children = New Dictionary(Of String, Feature)
        End If

        _children = children
        If (_children IsNot Nothing) Then
            For Each tmpFeat As KeyValuePair(Of String, Feature) In _children
                Dim objFeat As Feature = TryCast(tmpFeat.Value, Feature)
                ' until the EPC is modified, make the Price Display value true
                If (objFeat IsNot Nothing) Then
                    If (objFeat.Price = 0) Then
                        objFeat.PriceDisplay = False
                    Else
                        objFeat.PriceDisplay = True
                    End If
                End If
            Next
        End If

        _minChoiceSelection = minChoiceSelection
        _maxChoiceSelection = maxChoiceSelection
        _minSelection = minSelection
        _maxSelection = maxSelection
        _maxLength = maxlength
        _text = text
        _inputType = inputType
        _choiceSelection = New List(Of String)
    End Sub

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

    Public ReadOnly Property Name() As String
        Get
            Return _name
        End Get

    End Property

    Public ReadOnly Property Description() As String
        Get
            Return _description
        End Get
    End Property


    Public ReadOnly Property Category() As GWYCategory
        Get
            Return _category
        End Get
    End Property

    Public ReadOnly Property InputType() As GwyCustomizationInputType
        Get
            Return _inputType
        End Get
    End Property

    Public Property Text() As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
        End Set
    End Property

    Public ReadOnly Property MaxLength() As Integer
        Get
            Return _maxLength
        End Get
    End Property

    Public ReadOnly Property Choices() As Dictionary(Of String, Feature)
        Get
            Return _children
        End Get
    End Property

    Public ReadOnly Property ServiceType() As ServiceType
        Get
            Return _serviceType
        End Get
    End Property

    Public ReadOnly Property FeatureCategory() As FeatureCategory
        Get
            Return _featureCategory
        End Get
    End Property

    Public Property FeatureType() As GWYFeatureType
        Get
            Return _typeofFeature
        End Get
        Set(ByVal value As GWYFeatureType)
            _typeofFeature = value
        End Set
    End Property

    Public Property Price() As Decimal
        Get
            Return _price
        End Get
        Set(ByVal value As Decimal)
            _price = value
        End Set
    End Property

    Public Property ListPrice() As Decimal
        Get
            Return _listPrice
        End Get
        Set(ByVal value As Decimal)
            _listPrice = value
        End Set
    End Property

    Public Property PriceDisplay() As Boolean
        Get
            Return _priceDisplay
        End Get
        Set(ByVal value As Boolean)
            _priceDisplay = value
        End Set
    End Property
    Public Property PriceFrequency() As PriceFrequency
        Get
            Return _priceFrequency
        End Get
        Set(ByVal value As PriceFrequency)
            _priceFrequency = value
        End Set
    End Property

    Public ReadOnly Property MinChoiceSelection() As Integer
        Get
            Return _minChoiceSelection
        End Get
    End Property

    Public ReadOnly Property MaxChoiceSelection() As Integer
        Get
            Return _maxChoiceSelection
        End Get
    End Property

    Public ReadOnly Property MinSelection() As Integer
        Get
            Return _minSelection
        End Get
    End Property

    Public ReadOnly Property MaxSelection() As Integer
        Get
            Return _maxSelection
        End Get
    End Property

    Public Property IsSelected() As Boolean
        Get
            Return _selected
        End Get
        Set(ByVal value As Boolean)
            _selected = value
        End Set
    End Property

    ' 8/2007 jwg added
    Public Overrides Function ToString() As String
        Dim sb As New StringBuilder(1000)
        Dim t As Type
        Dim pi As PropertyInfo
        Dim iProp As Integer

        sb.Append("<BR/><H4>Feature " & _
                    Me.ID & " " & _
                    Me.Name & "</H4>")
        ' use Reflection to get all properties of each Bundle
        t = Me.GetType()
        iProp = 0
        sb.Append("<table border=1><tr>")
        For Each mi As System.Reflection.PropertyInfo In t.GetProperties()
            ' see if we have a plain property or if the property is 
            ' a dictionary.  can't do "toString()" on a dictionary
            ' object
            pi = Me.GetType().GetProperty(mi.Name)
            If ((pi.PropertyType.IsPrimitive OrElse pi.PropertyType.Name = "String") _
              AndAlso _
                 pi.PropertyType.ToString().IndexOf("Dictionary") = -1) Then
                If (iProp < 2) Then
                    sb.Append("<td>")
                Else
                    sb.Append("</tr><tr><td>")
                    iProp = 0
                End If
                sb.Append(mi.Name & " (<i>" & pi.PropertyType.Name & "</i>) : " & _
                        pi.GetValue(Me, Nothing))
                sb.Append("</td>")
                iProp += 1
            Else
                If (pi Is Nothing) Then
                Else
                    If (iProp < 2) Then
                        sb.Append("<td>")
                    Else
                        sb.Append("</tr><tr><td>")
                        iProp = 0
                    End If
                    ' special logic to scan the Features dictionary which
                    ' can be contained in this Features object
                    ' hate the hard-coded use of (Of String, Feature).  Would
                    ' like a more generic way to use Reflection or something
                    ' to determine the data type in the dictionary.
                    If Not (IsNothing(pi.GetValue(Me, Nothing))) Then

                        If (Me.GetType().GetProperty(mi.Name).PropertyType.AssemblyQualifiedName.ToUpper().IndexOf( _
                                "CharterDotCom.CommonEntities.Library.Feature".ToUpper()) > -1) Then

                            For Each kvp As KeyValuePair(Of String, Feature) _
                               In Me.GetType().GetProperty(mi.Name).GetValue(Me, Nothing)
                                If (Not (IsNothing(kvp))) Then
                                    sb.Append(kvp.ToString())
                                End If
                            Next
                        End If
                    End If
                    sb.Append("</td>")
                    iProp += 1

                End If
            End If
        Next mi

        sb.Append("</tr></table><BR> End Feature " + Me.ID + "</BR>")

        Return sb.ToString()

    End Function
End Class
