Option Strict On
Option Explicit On

Imports System
Imports System.Linq

Namespace Nexamas.UI.Visualization

    ''' <summary>
    ''' Immutable readiness evidence for the Chart / Visualization System foundation.
    ''' </summary>
    Friend NotInheritable Class MASChartFoundationSnapshot

        Private ReadOnly _plans As MASChartRenderPlan()

        Friend Sub New(sourceName As String, plans As MASChartRenderPlan())
            Me.SourceName = MASChartDataPoint.NormalizeText(sourceName)
            _plans = If(plans, New MASChartRenderPlan() {}).Where(Function(plan) plan IsNot Nothing).ToArray()
        End Sub

        Friend ReadOnly Property SourceName As String

        Friend ReadOnly Property Plans As MASChartRenderPlan()
            Get
                Return DirectCast(_plans.Clone(), MASChartRenderPlan())
            End Get
        End Property

        Friend ReadOnly Property PlanCount As Integer
            Get
                Return _plans.Length
            End Get
        End Property

        Friend ReadOnly Property ReadyPlanCount As Integer
            Get
                Return _plans.Where(Function(plan) plan.ReadyForDashboardChart).Count()
            End Get
        End Property

        Friend ReadOnly Property PrimitiveCount As Integer
            Get
                Return _plans.Sum(Function(plan) plan.PrimitiveCount)
            End Get
        End Property

        Friend ReadOnly Property Status As MASChartFoundationStatus
            Get
                If PlanCount = 0 Then Return MASChartFoundationStatus.Empty
                If ReadyPlanCount = PlanCount Then Return MASChartFoundationStatus.Ready
                If ReadyPlanCount > 0 Then Return MASChartFoundationStatus.PartiallyReady
                Return MASChartFoundationStatus.InvalidDefinition
            End Get
        End Property

        Friend ReadOnly Property HasNoFailedPlans As Boolean
            Get
                Return PlanCount > 0 AndAlso ReadyPlanCount = PlanCount
            End Get
        End Property

        Friend ReadOnly Property LineChartCount As Integer
            Get
                Return CountKind(MASChartKind.Line)
            End Get
        End Property

        Friend ReadOnly Property BarChartCount As Integer
            Get
                Return CountKind(MASChartKind.Bar)
            End Get
        End Property

        Friend ReadOnly Property PieChartCount As Integer
            Get
                Return CountKind(MASChartKind.Pie)
            End Get
        End Property

        Friend ReadOnly Property DonutChartCount As Integer
            Get
                Return CountKind(MASChartKind.Donut)
            End Get
        End Property

        Friend ReadOnly Property KpiChartCount As Integer
            Get
                Return CountKind(MASChartKind.Kpi)
            End Get
        End Property

        Friend ReadOnly Property SparklineChartCount As Integer
            Get
                Return CountKind(MASChartKind.Sparkline)
            End Get
        End Property

        Friend ReadOnly Property HasRequestedChartCoverage As Boolean
            Get
                Return LineChartCount > 0 AndAlso
                       BarChartCount > 0 AndAlso
                       PieChartCount > 0 AndAlso
                       DonutChartCount > 0 AndAlso
                       KpiChartCount > 0 AndAlso
                       SparklineChartCount > 0
            End Get
        End Property

        Friend ReadOnly Property ReadyForDashboardVisualizationFoundation As Boolean
            Get
                Return Status = MASChartFoundationStatus.Ready AndAlso
                       HasRequestedChartCoverage AndAlso
                       PrimitiveCount > 0 AndAlso
                       SourceName.Length > 0
            End Get
        End Property

        Friend Function CountKind(kind As MASChartKind) As Integer
            Return _plans.Where(Function(plan) plan.ChartKind = kind AndAlso plan.ReadyForDashboardChart).Count()
        End Function

    End Class

End Namespace
