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 / Days"

        Private Sub DrawWeekdays(canvas As SKCanvas,
                                 ctx As MASThemeContext,
                                 row As SKRect,
                                 dpi As Single,
                                 color As SKColor)
            Dim info As DateTimeFormatInfo = CultureInfo.CurrentCulture.DateTimeFormat
            Dim names() As String = info.AbbreviatedDayNames
            Dim first As Integer = CInt(info.FirstDayOfWeek)
            Dim cellW As Single = row.Width / 7.0F

            For i As Integer = 0 To 6
                Dim dayIndex As Integer = (first + i) Mod 7
                Dim cell As New SKRect(row.Left + (i * cellW), row.Top, row.Left + ((i + 1) * cellW), row.Bottom)
                TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, names(dayIndex), PixelSnap.SnapRectHalf(cell), dpi, MASTypography.MASTextStyle.Small, color, TypographyTextPrimitives.TextAlign.Center, False)
            Next
        End Sub

        Private Sub DrawDays(canvas As SKCanvas,
                             ctx As MASThemeContext,
                             grid 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)
            Dim info As DateTimeFormatInfo = CultureInfo.CurrentCulture.DateTimeFormat
            Dim firstOfMonth As Date = _displayMonth
            Dim offset As Integer = (7 + CInt(firstOfMonth.DayOfWeek) - CInt(info.FirstDayOfWeek)) Mod 7
            Dim startDate As Date = firstOfMonth.AddDays(-offset)
            Dim gap As Single = Math.Min(plan.CellGapPx, Math.Max(0.0F, Math.Min(grid.Width / 18.0F, grid.Height / 18.0F)))
            Dim cellW As Single = (grid.Width - (gap * 6.0F)) / 7.0F
            Dim cellH As Single = Math.Max(plan.CellMinHeightPx, (grid.Height - (gap * 5.0F)) / 6.0F)
            If cellW <= 1.0F OrElse cellH <= 1.0F OrElse grid.Width <= 1.0F OrElse grid.Height <= 1.0F Then
                ClearDayTargets()
                Return
            End If
            Dim today As Date = DateTime.Today

            For i As Integer = 0 To 41
                Dim row As Integer = i \ 7
                Dim col As Integer = i Mod 7
                Dim d As Date = startDate.AddDays(i)
                Dim cell As New SKRect(grid.Left + (col * (cellW + gap)), grid.Top + (row * (cellH + gap)), grid.Left + (col * (cellW + gap)) + cellW, grid.Top + (row * (cellH + gap)) + cellH)
                cell = PixelSnap.SnapRectHalf(cell)
                _lastDayCells(i) = cell
                _lastDayDates(i) = d

                Dim inMonth As Boolean = (d.Month = _displayMonth.Month AndAlso d.Year = _displayMonth.Year)
                Dim allowed As Boolean = IsDateAllowed(d)
                Dim selected As Boolean = (_selectedDate.HasValue AndAlso _selectedDate.Value.Date = d.Date)
                If selected AndAlso ShouldSuppressCalendarSelectionFill(d) Then selected = False
                Dim isToday As Boolean = (today = d.Date)
                Dim hover As Boolean = IsSameHit(_hoverTarget, HitPart.DayCell, i)
                Dim pressed As Boolean = IsSameHit(_pressedTarget, HitPart.DayCell, i)

                DrawDayCell(canvas, ctx, cell, dpi, d, inMonth, allowed, selected, isToday, hover, pressed, primaryText, secondaryText, mutedText, disabledText, brandPrimary, onBrand, border)
            Next

            DrawCalendarSelectionMotionOverlay(canvas, dpi, brandPrimary, border)
        End Sub

        Private Sub DrawDayCell(canvas As SKCanvas,
                                ctx As MASThemeContext,
                                cell As SKRect,
                                dpi As Single,
                                value As Date,
                                inMonth As Boolean,
                                allowed As Boolean,
                                selected As Boolean,
                                isToday As Boolean,
                                hover As Boolean,
                                pressed As Boolean,
                                primaryText As SKColor,
                                secondaryText As SKColor,
                                mutedText As SKColor,
                                disabledText As SKColor,
                                brandPrimary As SKColor,
                                onBrand As SKColor,
                                border As SKColor)
            Dim radius As Single = Math.Max(4.0F, If(_lastLayoutPlan Is Nothing, DatePickerTokens.CalendarSelectedRadiusDip * dpi, _lastLayoutPlan.SelectedRadiusPx))

            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 isToday AndAlso Not selected Then
                    p.Style = SKPaintStyle.Stroke
                    p.StrokeWidth = Math.Max(1.0F, If(_lastLayoutPlan Is Nothing, DatePickerTokens.CalendarTodayRingDip * dpi, _lastLayoutPlan.RingStrokePx))
                    p.Color = border.WithAlpha(180)
                    canvas.DrawRoundRect(Inset(cell, 1.0F * dpi), radius, radius, p)
                End If
            End Using

            Dim textColor As SKColor
            If Not allowed Then
                textColor = disabledText
            ElseIf selected Then
                textColor = onBrand
            ElseIf Not inMonth Then
                textColor = mutedText.WithAlpha(DatePickerTokens.CalendarOutsideMonthAlpha)
            Else
                textColor = primaryText
            End If

            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, value.Day.ToString(CultureInfo.CurrentCulture), cell, dpi, MASTypography.MASTextStyle.Body, textColor, TypographyTextPrimitives.TextAlign.Center, False)
        End Sub

#End Region

    End Class

End Namespace
