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.Rendering
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASMasterDetailView

#Region "Rendering / Frame"

        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, MASMasterDetailViewLayoutPlan.Thresholds)
            Dim plan As MASMasterDetailViewLayoutPlan = MASMasterDetailViewLayoutPlan.Create(responsiveProfile)

            _primitives.DrawCardSurface(canvas, ctx, bounds, dpi, Me.ArchitectureContract, 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 = MASMasterDetailViewPolicy.ResolveRightToLeft()
            Dim accent As SKColor = MASMasterDetailViewPolicy.ResolveAccentColor(ctx)
            DrawHeader(canvas, ctx, _contentRect, dpi, plan, isRtl)
            ResolvePaneRects(_contentRect, dpi, plan, isRtl)
            DrawMasterPane(canvas, ctx, _masterRect, dpi, plan, accent, isRtl)
            DrawSelectionMotionOverlay(canvas, dpi, plan, accent, isRtl)
            DrawDetailPane(canvas, ctx, _detailRect, dpi, plan, accent, isRtl)
            If Not plan.IsStacked Then DrawSelectionConnector(canvas, _masterRect, _detailRect, dpi, plan, accent, isRtl)
            DrawFooter(canvas, ctx, _contentRect, dpi, plan, isRtl)
        End Sub

        Private Sub DrawHeader(canvas As SKCanvas,
                               ctx As MASThemeContext,
                               content As SKRect,
                               dpi As Single,
                               plan As MASMasterDetailViewLayoutPlan,
                               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)
            TypographyTextPrimitives.DrawSingleLineText(canvas, ctx, _title, header, dpi, MASTypography.MASTextStyle.Title, MASMasterDetailViewPolicy.ResolvePrimaryTextColor(ctx), If(isRtl, TypographyTextPrimitives.TextAlign.Right, TypographyTextPrimitives.TextAlign.Left), False)

            If Not plan.ShowHeaderSummary Then Return
            Dim summaryWidth As Single = Math.Min(plan.SummaryWidthPx, header.Width * 0.45F)
            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, MASMasterDetailViewPolicy.ResolveMutedTextColor(ctx), If(isRtl, TypographyTextPrimitives.TextAlign.Left, TypographyTextPrimitives.TextAlign.Right), False)
        End Sub

#End Region

    End Class

End Namespace
