<?xml version="1.0" encoding="utf-8" ?>
<Page
    x:Class="Microsoft.CmdPal.UI.ContentPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Interactions="using:Microsoft.Xaml.Interactions.Core"
    xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:cmdPalControls="using:Microsoft.CmdPal.UI.Controls"
    xmlns:cmdpalUI="using:Microsoft.CmdPal.UI"
    xmlns:controls="using:CommunityToolkit.WinUI.Controls"
    xmlns:converters="using:CommunityToolkit.WinUI.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:extViews="using:Microsoft.CmdPal.UI.ExtViews.Controls"
    xmlns:help="using:Microsoft.CmdPal.UI.Helpers"
    xmlns:local="using:Microsoft.CmdPal.UI"
    xmlns:markdownImageProviders="using:Microsoft.CmdPal.UI.Helpers.MarkdownImageProviders"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:tkcontrols="using:CommunityToolkit.WinUI.Controls"
    xmlns:viewModels="using:Microsoft.CmdPal.UI.ViewModels"
    Background="Transparent"
    mc:Ignorable="d">

    <Page.Resources>
        <ResourceDictionary>
            <tkcontrols:MarkdownThemes
                x:Key="DefaultMarkdownThemeConfig"
                H3FontSize="12"
                H3FontWeight="Normal"
                InlineCodeBackground="{StaticResource ControlFillColorDefaultBrush}"
                InlineCodeBorderBrush="{StaticResource ControlElevationBorderBrush}"
                InlineCodeCornerRadius="2"
                InlineCodeForeground="{StaticResource TextFillColorSecondaryBrush}"
                InlineCodePadding="2,0,2,1" />
            <markdownImageProviders:ImageProvider x:Key="ImageProvider" />
            <tkcontrols:MarkdownConfig
                x:Key="DefaultMarkdownConfig"
                ImageProvider="{StaticResource ImageProvider}"
                Themes="{StaticResource DefaultMarkdownThemeConfig}" />

            <StackLayout
                x:Name="VerticalStackLayout"
                Orientation="Vertical"
                Spacing="8" />

            <cmdpalUI:ContentTemplateSelector
                x:Key="ContentTemplateSelector"
                FormTemplate="{StaticResource FormContentTemplate}"
                ImageTemplate="{StaticResource ImageContentTemplate}"
                MarkdownTemplate="{StaticResource MarkdownContentTemplate}"
                PlainTextTemplate="{StaticResource PlainTextContentTemplate}"
                TreeTemplate="{StaticResource TreeContentTemplate}" />

            <cmdpalUI:ContentTemplateSelector
                x:Key="NestedContentTemplateSelector"
                FormTemplate="{StaticResource NestedFormContentTemplate}"
                ImageTemplate="{StaticResource ImageContentTemplate}"
                MarkdownTemplate="{StaticResource NestedMarkdownContentTemplate}"
                PlainTextTemplate="{StaticResource PlainTextContentTemplate}"
                TreeTemplate="{StaticResource TreeContentTemplate}" />

            <DataTemplate x:Key="FormContentTemplate" x:DataType="viewModels:ContentFormViewModel">
                <Grid Margin="0,4,4,4" Padding="12,8,8,8">
                    <cmdPalControls:ContentFormControl ViewModel="{x:Bind}" />
                </Grid>
            </DataTemplate>

            <DataTemplate x:Key="MarkdownContentTemplate" x:DataType="viewModels:ContentMarkdownViewModel">
                <Grid Margin="0,4,4,4" Padding="12,8,8,8">
                    <tkcontrols:MarkdownTextBlock
                        Config="{StaticResource DefaultMarkdownConfig}"
                        Text="{x:Bind Body, Mode=OneWay}"
                        UseEmphasisExtras="True"
                        UsePipeTables="True" />
                </Grid>
            </DataTemplate>

            <DataTemplate x:Key="PlainTextContentTemplate" x:DataType="viewModels:ContentPlainTextViewModel">
                <Grid Margin="0,4,4,4" Padding="12,8,8,8">
                    <extViews:PlainTextContentViewer
                        Text="{x:Bind Text, Mode=OneWay}"
                        UseMonospace="{x:Bind UseMonospace, Mode=OneWay}"
                        WordWrapEnabled="{x:Bind WordWrapEnabled, Mode=OneWay}" />
                </Grid>
            </DataTemplate>

            <DataTemplate x:Key="ImageContentTemplate" x:DataType="viewModels:ContentImageViewModel">
                <Grid Margin="0,4,4,4" Padding="12,8,8,8">
                    <extViews:ImageContentViewer DataContext="{x:Bind}" />
                </Grid>
            </DataTemplate>

            <DataTemplate x:Key="NestedFormContentTemplate" x:DataType="viewModels:ContentFormViewModel">
                <Grid>
                    <cmdPalControls:ContentFormControl ViewModel="{x:Bind}" />
                </Grid>
            </DataTemplate>

            <DataTemplate x:Key="NestedMarkdownContentTemplate" x:DataType="viewModels:ContentMarkdownViewModel">
                <Grid>
                    <tkcontrols:MarkdownTextBlock Config="{StaticResource DefaultMarkdownConfig}" Text="{x:Bind Body, Mode=OneWay}" />
                </Grid>
            </DataTemplate>

            <DataTemplate x:Key="TreeContentTemplate" x:DataType="viewModels:ContentTreeViewModel">
                <StackPanel
                    Margin="0,4,4,4"
                    Padding="12,8,8,8"
                    Orientation="Vertical">
                    <ItemsRepeater
                        Margin="8"
                        VerticalAlignment="Stretch"
                        ItemsSource="{x:Bind Root, Mode=OneWay}"
                        Layout="{StaticResource VerticalStackLayout}">
                        <ItemsRepeater.ItemTemplate>
                            <cmdpalUI:ContentTemplateSelector
                                FormTemplate="{StaticResource NestedFormContentTemplate}"
                                MarkdownTemplate="{StaticResource NestedMarkdownContentTemplate}"
                                TreeTemplate="{StaticResource TreeContentTemplate}" />
                        </ItemsRepeater.ItemTemplate>
                    </ItemsRepeater>

                    <ItemsRepeater
                        Margin="8"
                        VerticalAlignment="Stretch"
                        ItemsSource="{x:Bind Children, Mode=OneWay}"
                        Layout="{StaticResource VerticalStackLayout}"
                        Visibility="{x:Bind HasChildren, Mode=OneWay}">
                        <ItemsRepeater.ItemTemplate>
                            <cmdpalUI:ContentTemplateSelector
                                FormTemplate="{StaticResource FormContentTemplate}"
                                MarkdownTemplate="{StaticResource MarkdownContentTemplate}"
                                TreeTemplate="{StaticResource TreeContentTemplate}" />
                        </ItemsRepeater.ItemTemplate>
                    </ItemsRepeater>

                </StackPanel>
            </DataTemplate>

        </ResourceDictionary>
    </Page.Resources>
    <Grid>
        <ScrollView VerticalAlignment="Top" VerticalScrollMode="Enabled">
            <ItemsRepeater
                VerticalAlignment="Stretch"
                ItemTemplate="{StaticResource ContentTemplateSelector}"
                ItemsSource="{x:Bind ViewModel.Content, Mode=OneWay}"
                Layout="{StaticResource VerticalStackLayout}" />
        </ScrollView>
    </Grid>

</Page>
