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 "Geometry helpers"

        Private Shared Function Inset(rect As SKRect,
                                      insetPx As Single) As SKRect
            Return New SKRect(rect.Left + insetPx,
                              rect.Top + insetPx,
                              rect.Right - insetPx,
                              rect.Bottom - insetPx)
        End Function


        Private Shared Function ResolveSideCardWidth(stage As SKRect,
                                                     dpi As Single,
                                                     plan As MASContentCarouselLayoutPlan) As Single
            If stage.Width <= 1.0F OrElse plan Is Nothing Then Return 0.0F
            Return Math.Max(76.0F * dpi * plan.ResponsiveProfile.ContentScale, Math.Min(164.0F * dpi * plan.ResponsiveProfile.ContentScale, stage.Width * 0.19F))
        End Function


        Private Shared Function ResolveCenterCardWidth(stage As SKRect,
                                                       dpi As Single,
                                                       plan As MASContentCarouselLayoutPlan) As Single
            If stage.Width <= 1.0F OrElse plan Is Nothing Then Return 0.0F
            If Not plan.ShowSideCards Then
                Return Math.Max(Math.Min(plan.CenterCardMinWidthPx, stage.Width * 0.88F), stage.Width - 32.0F * dpi * plan.ResponsiveProfile.GapScale)
            End If
            Dim sideWidth As Single = ResolveSideCardWidth(stage, dpi, plan)
            Dim reserved As Single = (sideWidth + 46.0F * dpi * plan.ResponsiveProfile.GapScale) * 2.0F
            Dim available As Single = stage.Width - reserved
            Dim compactFallback As Single = stage.Width * 0.46F
            Dim desired As Single = Math.Min(plan.CenterCardMaxWidthPx, Math.Max(available, compactFallback))
            Return Math.Max(Math.Min(plan.CenterCardMinWidthPx, stage.Width * 0.72F), Math.Min(desired, stage.Width - 32.0F * dpi * plan.ResponsiveProfile.GapScale))
        End Function


        Private Shared Function Inflate(rect As SKRect,
                                        amountPx As Single) As SKRect
            Return New SKRect(rect.Left - amountPx,
                              rect.Top - amountPx,
                              rect.Right + amountPx,
                              rect.Bottom + amountPx)
        End Function


        Private Shared Function WithOpacity(color As SKColor,
                                            opacity As Single) As SKColor
            Dim clamped As Single = Math.Max(0.0F, Math.Min(1.0F, opacity))
            Dim alpha As Integer = CInt(Math.Round(CDbl(color.Alpha) * CDbl(clamped)))
            If alpha < 0 Then alpha = 0
            If alpha > 255 Then alpha = 255
            Return color.WithAlpha(CByte(alpha))
        End Function


#End Region
    End Class

End Namespace
