Option Strict On
Option Explicit On

Imports System

Namespace Nexamas.UI.Visualization

    ''' <summary>
    ''' Immutable business data point consumed by the chart foundation.
    ''' </summary>
    Friend NotInheritable Class MASChartDataPoint

        Friend Sub New(key As String,
                       label As String,
                       value As Double,
                       Optional secondaryValue As Double = 0.0R,
                       Optional hasSecondaryValue As Boolean = False)
            Me.Key = NormalizeText(key)
            Me.Label = NormalizeText(label)
            If Me.Label.Length = 0 Then Me.Label = Me.Key
            Me.Value = value
            Me.SecondaryValue = secondaryValue
            Me.HasSecondaryValue = hasSecondaryValue AndAlso IsFinite(secondaryValue)
        End Sub

        Friend ReadOnly Property Key As String
        Friend ReadOnly Property Label As String
        Friend ReadOnly Property Value As Double
        Friend ReadOnly Property SecondaryValue As Double
        Friend ReadOnly Property HasSecondaryValue As Boolean

        Friend ReadOnly Property IsValid As Boolean
            Get
                Return Key.Length > 0 AndAlso Label.Length > 0 AndAlso IsFinite(Value)
            End Get
        End Property

        Friend Shared Function Create(key As String, value As Double) As MASChartDataPoint
            Return New MASChartDataPoint(key, key, value)
        End Function

        Friend Shared Function WithLabel(key As String, label As String, value As Double) As MASChartDataPoint
            Return New MASChartDataPoint(key, label, value)
        End Function

        Friend Shared Function WithTrend(key As String, label As String, value As Double, trendValue As Double) As MASChartDataPoint
            Return New MASChartDataPoint(key, label, value, trendValue, hasSecondaryValue:=True)
        End Function

        Friend Shared Function NormalizeText(value As String) As String
            Return If(value, String.Empty).Trim()
        End Function

        Friend Shared Function IsFinite(value As Double) As Boolean
            Return Not Double.IsNaN(value) AndAlso Not Double.IsInfinity(value)
        End Function

    End Class

End Namespace
