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 Rows"

        Private Sub DrawEventRow(canvas As SKCanvas,
                                 ctx As MASThemeContext,
                                 rect As SKRect,
                                 dpi As Single,
                                 isRtl As Boolean,
                                 eventIndex As Integer,
                                 plan As MASAgendaViewLayoutPlan)
            If eventIndex < 0 OrElse eventIndex >= _events.Count Then Return
            Dim item As AgendaEventState = _events(eventIndex)
            Dim selected As Boolean = (_selectedEventIndex = eventIndex)
            Dim hover As Boolean = (_hoverEventIndex = eventIndex)
            If selected AndAlso ShouldSuppressAgendaEventSelectionFill(eventIndex) Then selected = False
            Dim pressed As Boolean = (_pressedEventIndex = eventIndex)
            Dim accent As SKColor = MASAgendaViewPolicy.ResolveAccentColor(ctx)
            Dim alpha As Byte = AgendaViewTokens.EventFillAlpha
            If selected Then alpha = AgendaViewTokens.EventSelectedAlpha
            If hover Then alpha = AgendaViewTokens.EventHoverAlpha
            If pressed Then alpha = AgendaViewTokens.EventPressedAlpha
            Dim radius As Single = If(plan Is Nothing, AgendaViewTokens.EventRadiusDip * dpi, plan.EventRadiusPx)
            Using fill As New SKPaint With {.IsAntialias = True, .Style = SKPaintStyle.Fill, .Color = accent.WithAlpha(alpha)}
                canvas.DrawRoundRect(rect, radius, radius, fill)
            End Using
            Using stroke As New SKPaint With {.IsAntialias = True, .Style = SKPaintStyle.Stroke, .StrokeWidth = Math.Max(1.0F, dpi), .Color = accent.WithAlpha(AgendaViewTokens.EventStrokeAlpha)}
                canvas.DrawRoundRect(Inset(rect, 0.5F * dpi), radius, radius, stroke)
            End Using
            If selected Then
                Dim marker As SKRect = If(isRtl,
                                          New SKRect(rect.Right - 4.0F * dpi, rect.Top + 9.0F * dpi, rect.Right - 2.0F * dpi, rect.Bottom - 9.0F * dpi),
                                          New SKRect(rect.Left + 2.0F * dpi, rect.Top + 9.0F * dpi, rect.Left + 4.0F * dpi, rect.Bottom - 9.0F * dpi))
                Using markerPaint As New SKPaint With {.IsAntialias = True, .Style = SKPaintStyle.Fill, .Color = accent.WithAlpha(230)}
                    canvas.DrawRoundRect(marker, 2.0F * dpi, 2.0F * dpi, markerPaint)
                End Using
            End If

            Dim inner As SKRect = Inset(rect, If(plan Is Nothing, 8.0F * dpi, Math.Max(4.0F * dpi, plan.RowGapPx)))
            Dim pillWidth As Single = If(plan Is Nothing, AgendaViewTokens.TimePillWidthDip * dpi, plan.TimePillWidthPx)
            Dim pillHeight As Single = If(plan Is Nothing, AgendaViewTokens.TimePillHeightDip * dpi, plan.TimePillHeightPx)
            Dim pillRect As SKRect
            If isRtl Then
                pillRect = New SKRect(inner.Right - pillWidth, inner.Top, inner.Right, inner.Top + pillHeight)
            Else
                pillRect = New SKRect(inner.Left, inner.Top, inner.Left + pillWidth, inner.Top + pillHeight)
            End If
            DrawTimePill(canvas, ctx, pillRect, dpi, item)
            Dim textLeft As Single = If(isRtl, inner.Left, pillRect.Right + 8.0F * dpi)
            Dim textRight As Single = If(isRtl, pillRect.Left - 8.0F * dpi, inner.Right)
            Dim titleRect As New SKRect(textLeft, inner.Top - 2.0F * dpi, textRight, inner.Top + 25.0F * dpi)
            Dim detailRect As New SKRect(textLeft, inner.Top + 25.0F * dpi, textRight, inner.Bottom)
            Dim align As TypographyTextPrimitives.TextAlign = If(isRtl, TypographyTextPrimitives.TextAlign.Right, TypographyTextPrimitives.TextAlign.Left)
            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, item.Title, titleRect, dpi, MASTypography.MASTextStyle.Body, MASAgendaViewPolicy.ResolvePrimaryTextColor(ctx), align, False)
            If item.Detail.Length > 0 AndAlso (plan Is Nothing OrElse plan.ShowEventDetail) Then
                TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, item.Detail, detailRect, dpi, MASTypography.MASTextStyle.Micro, MASAgendaViewPolicy.ResolveMutedTextColor(ctx), align, False)
            End If
        End Sub

        Private Sub DrawTimePill(canvas As SKCanvas,
                                 ctx As MASThemeContext,
                                 rect As SKRect,
                                 dpi As Single,
                                 item As AgendaEventState)
            Dim accent As SKColor = MASAgendaViewPolicy.ResolveAccentColor(ctx)
            Using fill As New SKPaint With {.IsAntialias = True, .Style = SKPaintStyle.Fill, .Color = accent.WithAlpha(AgendaViewTokens.TimePillFillAlpha)}
                canvas.DrawRoundRect(rect, AgendaViewTokens.TimePillRadiusDip * dpi, AgendaViewTokens.TimePillRadiusDip * dpi, fill)
            End Using
            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, MASAgendaViewPolicy.BuildTimeText(item.StartMinute, item.EndMinute), Inset(rect, 3.0F * dpi), dpi, MASTypography.MASTextStyle.Micro, MASAgendaViewPolicy.ResolvePrimaryTextColor(ctx), TypographyTextPrimitives.TextAlign.Center, False)
        End Sub

#End Region

    End Class

End Namespace
