<!--  Copyright (c) Microsoft Corporation. All rights reserved.  -->
<!--  Licensed under the MIT License. See LICENSE in the project root for license information.  -->

<UserControl
    x:Class="Peek.FilePreviewer.Controls.ArchiveControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:converters="using:Peek.Common.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:Peek.FilePreviewer.Controls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:models="using:Peek.FilePreviewer.Previewers.Archives.Models"
    mc:Ignorable="d">
    <UserControl.Resources>
        <DataTemplate x:Key="DirectoryTemplate" x:DataType="models:ArchiveItem">
            <TreeViewItem
                AutomationProperties.Name="{x:Bind Name}"
                IsExpanded="{x:Bind IsExpanded}"
                ItemsSource="{x:Bind Children}">
                <Grid ColumnSpacing="8">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>
                    <Image
                        Grid.Column="0"
                        Width="16"
                        Height="16"
                        Source="{x:Bind Icon}" />
                    <TextBlock Grid.Column="1" Text="{x:Bind Name}" />
                </Grid>
            </TreeViewItem>
        </DataTemplate>

        <DataTemplate x:Key="FileTemplate" x:DataType="models:ArchiveItem">
            <TreeViewItem AutomationProperties.Name="{x:Bind Name}">
                <Grid ColumnSpacing="8">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto" />
                        <ColumnDefinition Width="auto" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>
                    <Image
                        Grid.Column="0"
                        Width="16"
                        Height="16"
                        Source="{x:Bind Icon}" />
                    <TextBlock Grid.Column="1" Text="{x:Bind Name}" />
                    <TextBlock
                        Grid.Column="2"
                        Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                        Text="{Binding Size, Converter={StaticResource BytesToStringConverter}}" />
                </Grid>
            </TreeViewItem>
        </DataTemplate>

        <models:ArchiveItemTemplateSelector
            x:Key="ArchiveItemTemplateSelector"
            DirectoryTemplate="{StaticResource DirectoryTemplate}"
            FileTemplate="{StaticResource FileTemplate}" />

        <converters:BytesToStringConverter x:Key="BytesToStringConverter" />
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <ScrollViewer Grid.Row="0" HorizontalScrollBarVisibility="Auto">
            <TreeView
                x:Name="ArchivePreview"
                CanDragItems="False"
                CanReorderItems="False"
                ItemTemplateSelector="{StaticResource ArchiveItemTemplateSelector}"
                ItemsSource="{x:Bind Source, Mode=OneWay}"
                SelectionMode="None" />
        </ScrollViewer>
        <Border
            Grid.Row="1"
            Margin="16"
            HorizontalAlignment="Center"
            Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
            BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
            BorderThickness="1"
            CornerRadius="8">
            <Grid Padding="16" ColumnSpacing="16">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock
                    Grid.Column="0"
                    VerticalAlignment="Center"
                    IsTextSelectionEnabled="True"
                    Text="{x:Bind DirectoryCount, Mode=OneWay}"
                    TextWrapping="Wrap" />
                <Border
                    Grid.Column="1"
                    BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
                    BorderThickness="0,0,1,0" />
                <TextBlock
                    Grid.Column="2"
                    VerticalAlignment="Center"
                    IsTextSelectionEnabled="True"
                    Text="{x:Bind FileCount, Mode=OneWay}"
                    TextWrapping="Wrap" />
                <Border
                    Grid.Column="3"
                    BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
                    BorderThickness="0,0,1,0" />
                <TextBlock
                    Grid.Column="4"
                    VerticalAlignment="Center"
                    IsTextSelectionEnabled="True"
                    Text="{x:Bind Size, Mode=OneWay}"
                    TextWrapping="Wrap" />
            </Grid>
        </Border>
    </Grid>
</UserControl>
