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 / Month-Year Selector"

        Private Sub DrawMonthYearSelector(canvas As SKCanvas,
                                            ctx As MASThemeContext,
                                            selector As SKRect,
                                            dpi As Single,
                                            plan As MASDatePickerCalendarLayoutPlan,
                                            primaryText As SKColor,
                                            secondaryText As SKColor,
                                            mutedText As SKColor,
                                            disabledText As SKColor,
                                            brandPrimary As SKColor,
                                            onBrand As SKColor,
                                            border As SKColor)
            selector = PixelSnap.SnapRectHalf(selector)
            ClearDayTargets()
            ClearSelectorTargets()

            Dim gap As Single = Math.Min(plan.CellGapPx, Math.Max(0.0F, Math.Min(selector.Width / 20.0F, selector.Height / 20.0F)))
            Dim labelHeight As Single = plan.SelectorLabelHeightPx
            Dim monthHeight As Single = plan.MonthSelectorHeightPx
            Dim yearsLabelTop As Single = selector.Top + monthHeight + (gap * 1.8F)
            Dim months As New SKRect(selector.Left, selector.Top, selector.Right, selector.Top + monthHeight)
            Dim years As New SKRect(selector.Left, yearsLabelTop + labelHeight + (gap * 0.7F), selector.Right, selector.Bottom)

            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, "Month", New SKRect(months.Left, months.Top, months.Right, months.Top + labelHeight), dpi, MASTypography.MASTextStyle.Small, mutedText, TypographyTextPrimitives.TextAlign.Left, False)
            Dim monthGrid As New SKRect(months.Left, months.Top + labelHeight + (gap * 0.7F), months.Right, months.Bottom)
            DrawMonthGrid(canvas, ctx, monthGrid, dpi, plan, primaryText, secondaryText, disabledText, brandPrimary, onBrand, border)

            Dim yearLabelRect As New SKRect(selector.Left, yearsLabelTop, selector.Right, yearsLabelTop + labelHeight)
            Dim yearLabel As String = String.Format(CultureInfo.CurrentCulture, "Year {0}-{1}", _yearWindowStart, _yearWindowStart + 19)
            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, yearLabel, yearLabelRect, dpi, MASTypography.MASTextStyle.Small, mutedText, TypographyTextPrimitives.TextAlign.Left, False)

            DrawYearGrid(canvas, ctx, years, dpi, plan, primaryText, secondaryText, disabledText, brandPrimary, onBrand, border)
        End Sub

        Private Sub DrawMonthGrid(canvas As SKCanvas,
                                  ctx As MASThemeContext,
                                  grid As SKRect,
                                  dpi As Single,
                                  plan As MASDatePickerCalendarLayoutPlan,
                                  primaryText As SKColor,
                                  secondaryText As SKColor,
                                  disabledText As SKColor,
                                  brandPrimary As SKColor,
                                  onBrand As SKColor,
                                  border As SKColor)
            Dim info As DateTimeFormatInfo = CultureInfo.CurrentCulture.DateTimeFormat
            Dim gap As Single = Math.Min(plan.CellGapPx, Math.Max(0.0F, Math.Min(grid.Width / 12.0F, grid.Height / 12.0F)))
            Dim cellW As Single = (grid.Width - (gap * 3.0F)) / 4.0F
            Dim cellH As Single = (grid.Height - (gap * 2.0F)) / 3.0F
            If cellW <= 1.0F OrElse cellH <= 1.0F OrElse grid.Width <= 1.0F OrElse grid.Height <= 1.0F Then Return

            For i As Integer = 0 To 11
                Dim row As Integer = i \ 4
                Dim col As Integer = i Mod 4
                Dim monthValue As Integer = i + 1
                Dim cell As SKRect = PixelSnap.SnapRectHalf(New SKRect(grid.Left + (col * (cellW + gap)), grid.Top + (row * (cellH + gap)), grid.Left + (col * (cellW + gap)) + cellW, grid.Top + (row * (cellH + gap)) + cellH))
                _lastMonthCells(i) = cell
                _lastMonthValues(i) = monthValue

                Dim selected As Boolean = (monthValue = _pendingMonth)
                Dim allowed As Boolean = IsMonthAllowed(_displayMonth.Year, monthValue)
                Dim hover As Boolean = IsSameHit(_hoverTarget, HitPart.MonthChip, i)
                Dim pressed As Boolean = IsSameHit(_pressedTarget, HitPart.MonthChip, i)
                Dim name As String = info.AbbreviatedMonthNames(Math.Max(0, Math.Min(info.AbbreviatedMonthNames.Length - 1, i)))
                If String.IsNullOrWhiteSpace(name) Then name = monthValue.ToString("00", CultureInfo.CurrentCulture)
                Dim textColor As SKColor = If(allowed, primaryText, disabledText)

                DrawSelectorChip(canvas, ctx, cell, dpi, name, selected, allowed, hover, pressed, textColor, secondaryText, brandPrimary, onBrand, border)
            Next
        End Sub

        Private Sub DrawYearGrid(canvas As SKCanvas,
                                 ctx As MASThemeContext,
                                 grid As SKRect,
                                 dpi As Single,
                                 plan As MASDatePickerCalendarLayoutPlan,
                                 primaryText As SKColor,
                                 secondaryText As SKColor,
                                 disabledText As SKColor,
                                 brandPrimary As SKColor,
                                 onBrand As SKColor,
                                 border As SKColor)
            Dim gap As Single = Math.Min(plan.CellGapPx, Math.Max(0.0F, Math.Min(grid.Width / 14.0F, grid.Height / 14.0F)))
            Dim cellW As Single = (grid.Width - (gap * 4.0F)) / 5.0F
            Dim cellH As Single = (grid.Height - (gap * 3.0F)) / 4.0F
            If cellW <= 1.0F OrElse cellH <= 1.0F OrElse grid.Width <= 1.0F OrElse grid.Height <= 1.0F Then Return

            For i As Integer = 0 To 19
                Dim row As Integer = i \ 5
                Dim col As Integer = i Mod 5
                Dim yearValue As Integer = _yearWindowStart + i
                Dim cell As SKRect = PixelSnap.SnapRectHalf(New SKRect(grid.Left + (col * (cellW + gap)), grid.Top + (row * (cellH + gap)), grid.Left + (col * (cellW + gap)) + cellW, grid.Top + (row * (cellH + gap)) + cellH))
                _lastYearCells(i) = cell
                _lastYearValues(i) = yearValue

                Dim allowed As Boolean = IsMonthAllowed(yearValue, _pendingMonth)
                Dim selected As Boolean = (yearValue = _displayMonth.Year)
                Dim hover As Boolean = IsSameHit(_hoverTarget, HitPart.YearChip, i)
                Dim pressed As Boolean = IsSameHit(_pressedTarget, HitPart.YearChip, i)
                Dim textColor As SKColor = If(allowed, primaryText, disabledText)

                DrawSelectorChip(canvas, ctx, cell, dpi, yearValue.ToString(CultureInfo.CurrentCulture), selected, allowed, hover, pressed, textColor, secondaryText, brandPrimary, onBrand, border)
            Next
        End Sub

        Private Sub DrawSelectorChip(canvas As SKCanvas,
                                     ctx As MASThemeContext,
                                     cell As SKRect,
                                     dpi As Single,
                                     text As String,
                                     selected As Boolean,
                                     allowed As Boolean,
                                     hover As Boolean,
                                     pressed As Boolean,
                                     textColor As SKColor,
                                     secondaryText As SKColor,
                                     brandPrimary As SKColor,
                                     onBrand As SKColor,
                                     border As SKColor)
            Dim radius As Single = Math.Max(5.0F * dpi, cell.Height * 0.28F)

            Using p As New SKPaint()
                p.IsAntialias = True
                If selected Then
                    p.Style = SKPaintStyle.Fill
                    p.Color = brandPrimary.WithAlpha(DatePickerTokens.CalendarSelectedFillAlpha)
                    canvas.DrawRoundRect(cell, radius, radius, p)
                ElseIf allowed AndAlso pressed Then
                    p.Style = SKPaintStyle.Fill
                    p.Color = brandPrimary.WithAlpha(DatePickerTokens.CalendarPressedAlpha)
                    canvas.DrawRoundRect(cell, radius, radius, p)
                ElseIf allowed AndAlso hover Then
                    p.Style = SKPaintStyle.Fill
                    p.Color = brandPrimary.WithAlpha(DatePickerTokens.CalendarHoverAlpha)
                    canvas.DrawRoundRect(cell, radius, radius, p)
                End If

                If selected Then
                    p.Style = SKPaintStyle.Stroke
                    p.StrokeWidth = Math.Max(1.0F, 1.0F * dpi)
                    p.Color = border.WithAlpha(150)
                    canvas.DrawRoundRect(Inset(cell, 1.0F * dpi), radius, radius, p)
                End If
            End Using

            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, text, cell, dpi, MASTypography.MASTextStyle.Small, If(selected, onBrand, textColor), TypographyTextPrimitives.TextAlign.Center, False)
        End Sub

#End Region

    End Class

End Namespace
