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

        Private Sub DrawHeader(canvas As SKCanvas,
                               ctx As MASThemeContext,
                               header As SKRect,
                               dpi As Single,
                               plan As MASDatePickerCalendarLayoutPlan,
                               primaryText As SKColor,
                               secondaryText As SKColor,
                               border As SKColor)
            Dim buttonSize As Single = plan.NavButtonSizePx
            _lastPreviousRect = PixelSnap.SnapRectHalf(New SKRect(header.Left, header.MidY - (buttonSize * 0.5F), header.Left + buttonSize, header.MidY + (buttonSize * 0.5F)))
            _lastNextRect = PixelSnap.SnapRectHalf(New SKRect(header.Right - buttonSize, header.MidY - (buttonSize * 0.5F), header.Right, header.MidY + (buttonSize * 0.5F)))

            DrawNavButton(canvas, ctx, _lastPreviousRect, dpi, "‹", secondaryText, IsSameHit(_hoverTarget, HitPart.PreviousMonth, -1), IsSameHit(_pressedTarget, HitPart.PreviousMonth, -1))
            DrawNavButton(canvas, ctx, _lastNextRect, dpi, "›", secondaryText, IsSameHit(_hoverTarget, HitPart.NextMonth, -1), IsSameHit(_pressedTarget, HitPart.NextMonth, -1))

            _lastTitleRect = PixelSnap.SnapRectHalf(New SKRect(_lastPreviousRect.Right + plan.InnerPadPx, header.Top + (2.0F * dpi), _lastNextRect.Left - plan.InnerPadPx, header.Bottom - (2.0F * dpi)))
            Dim title As String = If(_calendarMode = CalendarMode.MonthYear,
                                     "Select month and year",
                                     _displayMonth.ToString("MMMM yyyy", CultureInfo.CurrentCulture))
            Dim titleHover As Boolean = IsSameHit(_hoverTarget, HitPart.MonthYearTitle, -1)
            Dim titlePressed As Boolean = IsSameHit(_pressedTarget, HitPart.MonthYearTitle, -1)

            Using pTitle As New SKPaint()
                pTitle.IsAntialias = True
                pTitle.Style = SKPaintStyle.Fill
                If titlePressed Then
                    pTitle.Color = secondaryText.WithAlpha(DatePickerTokens.CalendarPressedAlpha)
                    canvas.DrawRoundRect(_lastTitleRect, _lastTitleRect.Height * 0.36F, _lastTitleRect.Height * 0.36F, pTitle)
                ElseIf titleHover OrElse _calendarMode = CalendarMode.MonthYear Then
                    pTitle.Color = secondaryText.WithAlpha(DatePickerTokens.CalendarHoverAlpha)
                    canvas.DrawRoundRect(_lastTitleRect, _lastTitleRect.Height * 0.36F, _lastTitleRect.Height * 0.36F, pTitle)
                End If
            End Using

            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, title, _lastTitleRect, dpi, MASTypography.MASTextStyle.Body, primaryText, TypographyTextPrimitives.TextAlign.Center, False)

            Using p As New SKPaint()
                p.IsAntialias = True
                p.Style = SKPaintStyle.Stroke
                p.StrokeWidth = Math.Max(1.0F, 1.0F * dpi)
                p.Color = border.WithAlpha(90)
                canvas.DrawLine(header.Left, header.Bottom, header.Right, header.Bottom, p)
            End Using
        End Sub

        Private Sub DrawNavButton(canvas As SKCanvas,
                                  ctx As MASThemeContext,
                                  rect As SKRect,
                                  dpi As Single,
                                  glyph As String,
                                  color As SKColor,
                                  hover As Boolean,
                                  pressed As Boolean)
            Using p As New SKPaint()
                p.IsAntialias = True
                p.Style = SKPaintStyle.Fill
                If pressed Then
                    p.Color = color.WithAlpha(DatePickerTokens.CalendarPressedAlpha)
                    canvas.DrawRoundRect(rect, rect.Height * 0.35F, rect.Height * 0.35F, p)
                ElseIf hover Then
                    p.Color = color.WithAlpha(DatePickerTokens.CalendarHoverAlpha)
                    canvas.DrawRoundRect(rect, rect.Height * 0.35F, rect.Height * 0.35F, p)
                End If
            End Using

            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, glyph, rect, dpi, MASTypography.MASTextStyle.Title, color, TypographyTextPrimitives.TextAlign.Center, False)
        End Sub

#End Region

    End Class

End Namespace
