Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
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.Motion
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 MASContentCarousel
#Region "Rendering - navigation"

        Private Sub DrawNavigationButtons(canvas As SKCanvas,
                                          ctx As MASThemeContext,
                                          stage As SKRect,
                                          dpi As Single,
                                          plan As MASContentCarouselLayoutPlan,
                                          accent As SKColor,
                                          isRtl As Boolean)
            If plan Is Nothing OrElse plan.NavButtonSizePx <= 1.0F Then Return
            Dim size As Single = plan.NavButtonSizePx
            Dim y As Single = stage.MidY - size / 2.0F
            Dim leftButton As New SKRect(stage.Left + plan.NavButtonInsetPx, y, stage.Left + plan.NavButtonInsetPx + size, y + size)
            Dim rightButton As New SKRect(stage.Right - plan.NavButtonInsetPx - size, y, stage.Right - plan.NavButtonInsetPx, y + size)

            Dim leftKind As CarouselHitKind = If(isRtl, CarouselHitKind.NextButton, CarouselHitKind.PreviousButton)
            Dim rightKind As CarouselHitKind = If(isRtl, CarouselHitKind.PreviousButton, CarouselHitKind.NextButton)
            DrawNavButton(canvas, ctx, leftButton, dpi, plan, accent, leftKind, If(isRtl, "›", "‹"))
            DrawNavButton(canvas, ctx, rightButton, dpi, plan, accent, rightKind, If(isRtl, "‹", "›"))
        End Sub


        Private Sub DrawNavButton(canvas As SKCanvas,
                                  ctx As MASThemeContext,
                                  rect As SKRect,
                                  dpi As Single,
                                  plan As MASContentCarouselLayoutPlan,
                                  accent As SKColor,
                                  kind As CarouselHitKind,
                                  text As String)
            Dim alpha As Byte = ContentCarouselTokens.NavButtonAlpha
            If _pressedKind = kind Then alpha = ContentCarouselTokens.NavButtonPressedAlpha
            If _hoverKind = kind Then alpha = ContentCarouselTokens.NavButtonHoverAlpha
            Using fill As New SKPaint With {.IsAntialias = True, .Style = SKPaintStyle.Fill, .Color = accent.WithAlpha(alpha)}
                canvas.DrawRoundRect(rect, plan.NavButtonRadiusPx, plan.NavButtonRadiusPx, fill)
            End Using
            Using stroke As New SKPaint With {.IsAntialias = True, .Style = SKPaintStyle.Stroke, .StrokeWidth = Math.Max(1.0F, dpi), .Color = accent.WithAlpha(ContentCarouselTokens.CardStrokeAlpha)}
                canvas.DrawRoundRect(rect, plan.NavButtonRadiusPx, plan.NavButtonRadiusPx, stroke)
            End Using
            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, text, rect, dpi, MASTypography.MASTextStyle.Title, MASContentCarouselPolicy.ResolvePrimaryTextColor(ctx), TypographyTextPrimitives.TextAlign.Center, False)
            _hits.Add(New CarouselHit(kind, -1, rect))
        End Sub


        Private Sub DrawFooter(canvas As SKCanvas,
                               ctx As MASThemeContext,
                               content As SKRect,
                               dpi As Single,
                               plan As MASContentCarouselLayoutPlan,
                               accent As SKColor,
                               isRtl As Boolean)
            If plan Is Nothing OrElse Not plan.ShowFooter OrElse plan.FooterHeightPx <= 1.0F Then Return
            Dim footer As New SKRect(content.Left, content.Bottom - plan.FooterHeightPx, content.Right, content.Bottom)
            DrawIndicators(canvas, footer, dpi, plan, accent)
            If Not plan.ShowBoundaryText Then Return
            Dim textRect As SKRect = If(isRtl,
                                        New SKRect(footer.Left, footer.Top, footer.Left + footer.Width * 0.45F, footer.Bottom),
                                        New SKRect(footer.Right - footer.Width * 0.45F, footer.Top, footer.Right, footer.Bottom))
            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, ContentCarouselTokens.BoundaryText, textRect, dpi, MASTypography.MASTextStyle.Micro, MASContentCarouselPolicy.ResolveMutedTextColor(ctx), If(isRtl, TypographyTextPrimitives.TextAlign.Left, TypographyTextPrimitives.TextAlign.Right), False)
        End Sub


        Private Sub DrawIndicators(canvas As SKCanvas,
                                   footer As SKRect,
                                   dpi As Single,
                                   plan As MASContentCarouselLayoutPlan,
                                   accent As SKColor)
            If _items.Count <= 0 OrElse plan Is Nothing Then Return
            Dim visibleCount As Integer = Math.Min(_items.Count, plan.MaxVisibleIndicators)
            Dim totalWidth As Single = 0.0F
            For i As Integer = 0 To visibleCount - 1
                totalWidth += If(i = _selectedIndex, plan.IndicatorSelectedWidthPx, plan.IndicatorSizePx)
                If i < visibleCount - 1 Then totalWidth += plan.IndicatorGapPx
            Next
            Dim x As Single = footer.MidX - totalWidth / 2.0F
            Dim y As Single = footer.MidY - plan.IndicatorSizePx / 2.0F
            For i As Integer = 0 To visibleCount - 1
                Dim selected As Boolean = (i = _selectedIndex)
                Dim width As Single = If(selected, plan.IndicatorSelectedWidthPx, plan.IndicatorSizePx)
                Dim rect As New SKRect(x, y, x + width, y + plan.IndicatorSizePx)
                Using fill As New SKPaint With {.IsAntialias = True, .Style = SKPaintStyle.Fill, .Color = accent.WithAlpha(If(selected, ContentCarouselTokens.IndicatorSelectedAlpha, ContentCarouselTokens.IndicatorAlpha))}
                    canvas.DrawRoundRect(rect, plan.IndicatorSizePx / 2.0F, plan.IndicatorSizePx / 2.0F, fill)
                End Using
                _hits.Add(New CarouselHit(CarouselHitKind.Indicator, i, Inflate(rect, 5.0F * dpi * plan.ResponsiveProfile.GapScale)))
                x += width + plan.IndicatorGapPx
            Next
        End Sub


#End Region
    End Class

End Namespace
