Option Strict On
Option Explicit On

Imports System
Imports System.Globalization
Imports System.Windows.Forms
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Controls
Imports Nexamas.UI.General
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Composition
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Partial Friend NotInheritable Class MASDatePickerCalendarView

#Region "Rendering"

        Protected Overrides Sub Render(canvas As SKCanvas,
                                       ctx As MASThemeContext,
                                       pixelBounds As SKRect)
            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If pixelBounds.Width <= 1.0F OrElse pixelBounds.Height <= 1.0F Then Return

            _contract.AssertConsumable()

            Dim dpi As Single = PixelSnap.SafeDpi(ctx.Dpi)
            Dim app As IMASAppTheme = ctx.Theme
            If app Is Nothing Then Return

            Dim surfTheme As IMASFoundationSurfaceTheme = MASAppThemeContracts.Foundation(app).Surface
            If surfTheme Is Nothing Then Return

            Dim card As SKRect = PixelSnap.SnapRectHalf(pixelBounds)
            Dim plan As MASDatePickerCalendarLayoutPlan = MASDatePickerCalendarLayoutPlan.Create(ctx, card, SizeIntent)
            _lastLayoutPlan = plan
            Dim metricDpi As Single = plan.MetricDpi

            _shadowPainter.DrawLayeredShadow(canvas, card, _contract, dpi, surfTheme)
            _primitives.DrawAcrylicSurface(canvas, ctx, card, dpi, _contract)

            Dim inner As SKRect = Inset(card, plan.OuterPadPx)
            If inner.Width <= 1.0F OrElse inner.Height <= 1.0F Then Return

            Dim textTheme As IMASTextColorTheme = ctx.TextColorTheme
            Dim brand As IMASBrandTheme = ctx.BrandTheme
            Dim neutral As IMASNeutralTheme = ctx.NeutralTheme

            Dim primaryText As SKColor = If(textTheme Is Nothing, SKColors.Black, textTheme.PrimaryText)
            Dim secondaryText As SKColor = If(textTheme Is Nothing, SKColors.DimGray, textTheme.SecondaryText)
            Dim mutedText As SKColor = If(textTheme Is Nothing, SKColors.Gray, textTheme.MutedText)
            Dim disabledText As SKColor = If(textTheme Is Nothing, SKColors.Gray.WithAlpha(DatePickerTokens.CalendarDisabledAlpha), textTheme.DisabledText)
            Dim brandPrimary As SKColor = If(brand Is Nothing, SKColors.DodgerBlue, brand.BrandPrimary)
            Dim onBrand As SKColor = If(textTheme Is Nothing, SKColors.White, textTheme.OnBrandPrimaryText)
            Dim border As SKColor = If(neutral Is Nothing, SKColors.Gray.WithAlpha(75), neutral.BorderSubtle)

            Dim header As New SKRect(inner.Left, inner.Top, inner.Right, inner.Top + plan.HeaderHeightPx)
            DrawHeader(canvas, ctx, header, metricDpi, plan, primaryText, secondaryText, border)

            If _calendarMode = CalendarMode.MonthYear Then
                Dim selectorTop As Single = header.Bottom + (plan.InnerPadPx * 0.55F)
                Dim selector As New SKRect(inner.Left, selectorTop, inner.Right, inner.Bottom)
                DrawMonthYearSelector(canvas, ctx, selector, metricDpi, plan, primaryText, secondaryText, mutedText, disabledText, brandPrimary, onBrand, border)
                Return
            End If

            Dim weekdayTop As Single = header.Bottom + (plan.InnerPadPx * 0.35F)
            Dim gridTop As Single = weekdayTop
            If plan.ShowWeekdays Then
                Dim weekday As New SKRect(inner.Left, weekdayTop, inner.Right, weekdayTop + plan.WeekdayHeightPx)
                DrawWeekdays(canvas, ctx, weekday, metricDpi, mutedText)
                gridTop = weekday.Bottom + plan.CellGapPx
            End If

            Dim grid As New SKRect(inner.Left, gridTop, inner.Right, inner.Bottom)
            DrawDays(canvas, ctx, grid, metricDpi, plan, primaryText, secondaryText, mutedText, disabledText, brandPrimary, onBrand, border)
        End Sub

#End Region

    End Class

End Namespace
