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"

        Protected Overrides Sub Render(canvas As SKCanvas,
                                       ctx As MASThemeContext,
                                       pixelBounds As SKRect)
            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If pixelBounds.Width <= 1.0F OrElse pixelBounds.Height <= 1.0F Then Return

            Dim dpi As Single = PixelSnap.SafeDpi(ctx.Dpi)
            _lastDpi = dpi
            Dim bounds As SKRect = PixelSnap.SnapRect(pixelBounds, dpi)
            If bounds.Width <= 1.0F OrElse bounds.Height <= 1.0F Then Return

            Dim responsiveProfile As MASResponsiveLayoutProfile = MASResponsiveLayoutResolver.FromTheme(ctx, bounds, SizeIntent, MASContentCarouselLayoutPlan.Thresholds)
            Dim plan As MASContentCarouselLayoutPlan = MASContentCarouselLayoutPlan.Create(responsiveProfile)

            _primitives.DrawCardSurface(canvas, ctx, bounds, dpi, _contract, MASCardVisualStyle.Secondary)
            _contentRect = PixelSnap.SnapRect(Inset(bounds, plan.ContentInsetPx), dpi)
            If _contentRect.Width <= 1.0F OrElse _contentRect.Height <= 1.0F Then Return

            _hits.Clear()
            Dim isRtl As Boolean = MASContentCarouselPolicy.ResolveRightToLeft()
            Dim accent As SKColor = MASContentCarouselPolicy.ResolveAccentColor(ctx)
            DrawHeader(canvas, ctx, _contentRect, dpi, plan, accent, isRtl)
            DrawStage(canvas, ctx, _contentRect, dpi, plan, accent, isRtl)
            DrawFooter(canvas, ctx, _contentRect, dpi, plan, accent, isRtl)
        End Sub


        Private Sub DrawHeader(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.ShowHeader OrElse plan.HeaderHeightPx <= 1.0F Then Return
            Dim header As New SKRect(content.Left, content.Top, content.Right, content.Top + plan.HeaderHeightPx)
            Dim align As TypographyTextPrimitives.TextAlign = If(isRtl, TypographyTextPrimitives.TextAlign.Right, TypographyTextPrimitives.TextAlign.Left)
            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, _title, header, dpi, MASTypography.MASTextStyle.Title, MASContentCarouselPolicy.ResolvePrimaryTextColor(ctx), align, False)

            If Not plan.ShowHeaderSummary Then Return
            Dim summaryWidth As Single = Math.Min(250.0F * dpi * plan.ResponsiveProfile.ChromeScale, header.Width * 0.42F)
            If summaryWidth <= 1.0F Then Return
            Dim summaryRect As SKRect = If(isRtl,
                                           New SKRect(header.Left, header.Top, header.Left + summaryWidth, header.Bottom),
                                           New SKRect(header.Right - summaryWidth, header.Top, header.Right, header.Bottom))
            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, SummaryText, summaryRect, dpi, MASTypography.MASTextStyle.Micro, MASContentCarouselPolicy.ResolveMutedTextColor(ctx), If(isRtl, TypographyTextPrimitives.TextAlign.Left, TypographyTextPrimitives.TextAlign.Right), False)
        End Sub


        Private Sub DrawStage(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 Then Return
            Dim top As Single = content.Top + plan.HeaderHeightPx + If(plan.HeaderHeightPx > 0.0F, plan.StageGapPx, 0.0F)
            Dim bottom As Single = content.Bottom - plan.FooterHeightPx - If(plan.FooterHeightPx > 0.0F, plan.StageGapPx, 0.0F)
            _stageRect = PixelSnap.SnapRect(New SKRect(content.Left, top, content.Right, bottom), dpi)
            If _stageRect.Width <= 1.0F OrElse _stageRect.Height <= 1.0F Then Return

            Using fill As New SKPaint With {.IsAntialias = True, .Style = SKPaintStyle.Fill, .Color = accent.WithAlpha(ContentCarouselTokens.StageFillAlpha)}
                canvas.DrawRoundRect(_stageRect, plan.CardRadiusPx, plan.CardRadiusPx, 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(_stageRect, plan.CardRadiusPx, plan.CardRadiusPx, stroke)
            End Using

            If _items.Count = 0 Then
                TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, ContentCarouselTokens.EmptyCarouselText, _stageRect, dpi, MASTypography.MASTextStyle.Body, MASContentCarouselPolicy.ResolveMutedTextColor(ctx).WithAlpha(ContentCarouselTokens.EmptyTextAlpha), TypographyTextPrimitives.TextAlign.Center, False)
                Return
            End If

            NormalizeSelection()
            If plan.ShowSideCards Then DrawSideCards(canvas, ctx, _stageRect, dpi, plan, accent, isRtl)
            If IsSelectionMotionActive() Then
                DrawTransitioningCenterCards(canvas, ctx, _stageRect, dpi, plan, accent, isRtl)
            Else
                DrawCenterCard(canvas, ctx, _stageRect, dpi, plan, accent, isRtl)
            End If
            If plan.ShowNavButtons Then DrawNavigationButtons(canvas, ctx, _stageRect, dpi, plan, accent, isRtl)
        End Sub


#End Region
    End Class

End Namespace
