Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASAgendaView

#Region "Rendering Header"

        Private Sub DrawHeader(canvas As SKCanvas,
                               ctx As MASThemeContext,
                               content As SKRect,
                               dpi As Single,
                               isRtl As Boolean,
                               plan As MASAgendaViewLayoutPlan)
            If plan Is Nothing OrElse Not plan.ShowHeader Then
                _previousRect = SKRect.Empty
                _nextRect = SKRect.Empty
                Return
            End If
            Dim headerHeight As Single = plan.HeaderHeightPx
            Dim header As New SKRect(content.Left, content.Top, content.Right, content.Top + headerHeight)
            Dim buttonSize As Single = Math.Min(plan.NavButtonSizePx, Math.Max(18.0F * dpi, header.Height - 4.0F * dpi))
            Dim buttonTop As Single = header.Top + (header.Height - buttonSize) * 0.5F
            Dim leftButton As New SKRect(header.Left, buttonTop, header.Left + buttonSize, buttonTop + buttonSize)
            Dim rightButton As New SKRect(header.Right - buttonSize, buttonTop, header.Right, buttonTop + buttonSize)
            _previousRect = If(isRtl, rightButton, leftButton)
            _nextRect = If(isRtl, leftButton, rightButton)

            DrawNavButton(canvas, ctx, _previousRect, dpi, True, isRtl)
            DrawNavButton(canvas, ctx, _nextRect, dpi, False, isRtl)

            Dim titleRect As New SKRect(header.Left + buttonSize + plan.RowGapPx,
                                        header.Top,
                                        header.Right - buttonSize - plan.RowGapPx,
                                        If(plan.ShowHeaderSubtitle, header.Top + header.Height * 0.56F, header.Bottom))
            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, _title, titleRect, dpi, MASTypography.MASTextStyle.Body, MASAgendaViewPolicy.ResolvePrimaryTextColor(ctx), TypographyTextPrimitives.TextAlign.Center, False)
            If plan.ShowHeaderSubtitle Then
                Dim dayRect As New SKRect(titleRect.Left,
                                          header.Top + header.Height * 0.52F,
                                          titleRect.Right,
                                          header.Bottom)
                TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, MASAgendaViewPolicy.BuildViewTitle(_day, _viewMode), dayRect, dpi, MASTypography.MASTextStyle.Micro, MASAgendaViewPolicy.ResolveMutedTextColor(ctx), TypographyTextPrimitives.TextAlign.Center, False)
            End If

            Using p As New SKPaint With {
                .IsAntialias = True,
                .Style = SKPaintStyle.Stroke,
                .StrokeWidth = Math.Max(1.0F, dpi),
                .Color = MASAgendaViewPolicy.ResolveMutedTextColor(ctx).WithAlpha(AgendaViewTokens.DividerAlpha)
            }
                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,
                                  isPrevious As Boolean,
                                  isRtl As Boolean)
            Dim activeHover As Boolean = If(isPrevious, _hoverPrevious, _hoverNext)
            Dim activePressed As Boolean = If(isPrevious, _pressedPrevious, _pressedNext)
            Dim accent As SKColor = MASAgendaViewPolicy.ResolveAccentColor(ctx)
            Dim radius As Single = rect.Height * 0.42F
            Dim alpha As Byte = If(activePressed, AgendaViewTokens.NavPressedAlpha, If(activeHover, AgendaViewTokens.NavHoverAlpha, CByte(0)))
            If alpha > 0 Then
                Using fill As New SKPaint With {.IsAntialias = True, .Style = SKPaintStyle.Fill, .Color = accent.WithAlpha(alpha)}
                    canvas.DrawRoundRect(rect, radius, radius, fill)
                End Using
            End If

            Dim drawsLeftArrow As Boolean = If(isPrevious, Not isRtl, isRtl)
            Dim cx As Single = rect.MidX
            Dim cy As Single = rect.MidY
            Dim w As Single = rect.Width * 0.22F
            Dim h As Single = rect.Height * 0.28F
            Using path As New SKPath()
                If drawsLeftArrow Then
                    path.MoveTo(cx + w * 0.45F, cy - h)
                    path.LineTo(cx - w * 0.45F, cy)
                    path.LineTo(cx + w * 0.45F, cy + h)
                Else
                    path.MoveTo(cx - w * 0.45F, cy - h)
                    path.LineTo(cx + w * 0.45F, cy)
                    path.LineTo(cx - w * 0.45F, cy + h)
                End If
                Using paint As New SKPaint With {
                    .IsAntialias = True,
                    .Style = SKPaintStyle.Stroke,
                    .StrokeWidth = Math.Max(1.4F * dpi, 1.0F),
                    .StrokeCap = SKStrokeCap.Round,
                    .StrokeJoin = SKStrokeJoin.Round,
                    .Color = MASAgendaViewPolicy.ResolvePrimaryTextColor(ctx)
                }
                    canvas.DrawPath(path, paint)
                End Using
            End Using
        End Sub

#End Region

    End Class

End Namespace
