<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -->
<ui:Page
    x:Class="MUXControlsTestApp.Samples.Selection.TreeViewSample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ui="http://schemas.modernwpf.com/2019"
    xmlns:controls="clr-namespace:ModernWpf.Controls;assembly=ModernWpf.Controls"
    xmlns:local="clr-namespace:MUXControlsTestApp.Samples.Selection">

    <Page.Resources>
        <ResourceDictionary>
            <controls:SelectionModel x:Key="selectionModel" />
            <local:BoolToBrushConverter x:Key="boolToBrushConterter" />
            <local:BoolToVisibilityConverter x:Key="boolToVisibilityConterter" />

            <!--<controls:StackLayout x:Name="layout" />-->
            <controls:FlowLayout
                x:Key="layout"
                MinRowSpacing="10"
                MinColumnSpacing="0" />
            <controls:RecyclePool x:Key="recyclePool" />
            <controls:RecyclingElementFactory
                x:Key="elementFactory"
                RecyclePool="{StaticResource recyclePool}"
                SelectTemplateKey="ElementFactory_SelectTemplateKey">
                <controls:RecyclingElementFactory.Templates>
                    <DataTemplate x:Key="item">
                        <local:TreeViewItem
                            x:Name="templateRoot"
                            SelectionModel="{StaticResource selectionModel}">
                            <Border
                                Background="{Binding ElementName=templateRoot, Path=IsSelected, Converter={StaticResource boolToBrushConterter}}"
                                HorizontalAlignment="Stretch"
                                Width="100"
                                Height="100"
                                BorderThickness="1"
                                BorderBrush="LightGray">
                                <Grid>
                                    <ui:SymbolIcon
                                        Symbol="Accept"
                                        Visibility="{Binding ElementName=templateRoot, Path=IsSelected, Converter={StaticResource boolToVisibilityConterter}}"
                                        HorizontalAlignment="Right"
                                        VerticalAlignment="Top"
                                        Width="20"
                                        Height="20"
                                        Margin="5" />
                                    <TextBlock
                                        Text="{Binding}"
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Center" />
                                </Grid>
                            </Border>
                        </local:TreeViewItem>
                    </DataTemplate>
                    <DataTemplate x:Key="group">
                        <local:TreeViewItem
                            x:Name="templateRoot"
                            SelectionModel="{StaticResource selectionModel}">
                            <Border
                                BorderBrush="Gray"
                                Margin="10,0,0,0">
                                <StackPanel>
                                    <StackPanel
                                        Orientation="Horizontal"
                                        Background="{Binding ElementName=templateRoot, Path=IsSelected, Converter={StaticResource boolToBrushConterter}}">
                                        <ui:SymbolIcon Symbol="Play" />
                                        <TextBlock Text="{Binding}" />
                                    </StackPanel>
                                    <controls:ItemsRepeater
                                        Layout="{StaticResource layout}"
                                        ItemTemplate="{DynamicResource elementFactory}"
                                        ItemsSource="{Binding}"
                                        Margin="20,0,0,0"
                                        ElementPrepared="Repeater_ElementPrepared"
                                        ElementIndexChanged="OnElementIndexChanged" />
                                </StackPanel>
                            </Border>
                        </local:TreeViewItem>
                    </DataTemplate>
                </controls:RecyclingElementFactory.Templates>
            </controls:RecyclingElementFactory>
        </ResourceDictionary>
    </Page.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <ToggleButton
                x:Name="multipleSelection"
                IsChecked="False"
                Checked="OnMultipleSelectionClicked"
                Unchecked="OnMultipleSelectionClicked">
                Multiple
            </ToggleButton>
            <Button
                Content="Back"
                Click="OnBackClicked" />
            <Button
                Content="Debug"
                Click="Button_Click" />
            <StackPanel>
                <TextBlock>Collection Changes</TextBlock>
                <TextBox
                    x:Name="indexPath"
                    Text="0.0.0.0" />
                <Button
                    x:Name="insert"
                    Click="insert_Click"
                    Content="Insert" />
                <Button
                    x:Name="remove"
                    Click="remove_Click"
                    Content="Remove" />
                <Button
                    x:Name="clear"
                    Click="clear_Click"
                    Content="Clear" />
            </StackPanel>
            <!-- Details view for selected item -->
            <StackPanel
                Grid.Row="1"
                Orientation="Horizontal">
                <TextBlock Text="SelectedItem:" />
                <!--<TextBlock Text="{Binding ElementName=selectionModel, Path=SelectedItem}"  Margin="0,0,10,0"/>-->
                <TextBlock Text="{Binding SelectedItem, Source={StaticResource selectionModel}, Mode=OneWay}" />
            </StackPanel>
        </StackPanel>
        <controls:ItemsRepeaterScrollHost Grid.Row="1">
            <ScrollViewer>
                <local:SelectionContainer SelectionModel="{StaticResource selectionModel}">
                    <controls:ItemsRepeater
                        x:Name="rootRepeater"
                        Layout="{StaticResource layout}"
                        ElementIndexChanged="OnElementIndexChanged"
                        ItemTemplate="{StaticResource elementFactory}" />
                </local:SelectionContainer>
            </ScrollViewer>
        </controls:ItemsRepeaterScrollHost>
    </Grid>
</ui:Page>
