<UserControl x:Class="SpottedZebra.UnitySizeExplorer.WPF.Views.Pages.WorkspacePageView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:SpottedZebra.UnitySizeExplorer.WPF.Views.Pages"
      xmlns:view="clr-namespace:SpottedZebra.UnitySizeExplorer.WPF.Views"
      xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
      mc:Ignorable="d" 
      d:DesignHeight="300" 
      d:DesignWidth="300">

    <!-- Commands -->
    <UserControl.CommandBindings>
        <CommandBinding Command="local:WorkspacePageView.Filter1Action" Executed="OnFilter1" />
        <CommandBinding Command="local:WorkspacePageView.Filter2Action" Executed="OnFilter2" />
        <CommandBinding Command="local:WorkspacePageView.Filter3Action" Executed="OnFilter3" />
        <CommandBinding Command="local:WorkspacePageView.ClearFilterAction" Executed="OnClearFilter" />
        <CommandBinding Command="local:WorkspacePageView.ToggleSelectedAction" Executed="OnToggleSelected" />
        <CommandBinding Command="ApplicationCommands.Close" Executed="OnClose" />
    </UserControl.CommandBindings>
    <!-- Static Resources -->
    <UserControl.Resources>
        <view:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <HierarchicalDataTemplate 
             x:Key="CheckBoxItemTemplate"
             ItemsSource="{Binding Children, Mode=OneTime}">
            <Grid HorizontalAlignment="Stretch">
                <Grid.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Check" Visibility="{Binding IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=TRUE}" Click="OnToggleCheckbox"/>
                        <MenuItem Header="Uncheck" Visibility="{Binding IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}" Click="OnToggleCheckbox"/>
                        <MenuItem Header="Expand" Visibility="{Binding IsExpanded, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=TRUE}" Click="OnToggleExpanded"/>
                        <MenuItem Header="Collapse" Visibility="{Binding IsExpanded, Converter={StaticResource BooleanToVisibilityConverter}}" Click="OnToggleExpanded"/>
                        <MenuItem Header="Hide" Click="OnHideItem"/>
                        <Separator />
                        <MenuItem Header="Open in File Explorer" Click="OnOpenFileExplorer"/>
                    </ContextMenu>
                </Grid.ContextMenu>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <CheckBox 
                    Grid.Column="0"
                    Focusable="False"
                    IsChecked="{Binding IsChecked}"
                    VerticalAlignment="Center" />
                <Button
                    Grid.Column="1"
                    Margin="5 0 5 0"
                    Background="Transparent"
                    BorderThickness="0"
                    Focusable="False"
                    Click="OnHideItem">
                    <TextBlock TextDecorations="Underline">Hide</TextBlock>
                </Button>
                <ContentPresenter
                    Grid.Column="2"
                    Content="{Binding NameAndSize, Mode=OneWay}"
                    Margin="2,0" />
            </Grid>
        </HierarchicalDataTemplate>
        <Style x:Key="TreeViewItemFocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <PathGeometry x:Key="TreeArrow" Figures="M0,0 L0,6 L6,0 z"/>
        <Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="Width" Value="16"/>
            <Setter Property="Height" Value="16"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Background="Transparent" Height="16" Padding="5,5,5,5" Width="16">
                            <Path x:Name="ExpandPath" Data="{StaticResource TreeArrow}" Fill="Transparent" Stroke="#FF989898">
                                <Path.RenderTransform>
                                    <RotateTransform Angle="135" CenterY="3" CenterX="3"/>
                                </Path.RenderTransform>
                            </Path>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF1BBBFA"/>
                                <Setter Property="Fill" TargetName="ExpandPath" Value="Transparent"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="RenderTransform" TargetName="ExpandPath">
                                    <Setter.Value>
                                        <RotateTransform Angle="180" CenterY="3" CenterX="3"/>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="Fill" TargetName="ExpandPath" Value="#FF595959"/>
                                <Setter Property="Stroke" TargetName="ExpandPath" Value="#FF262626"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style 
            x:Key="StretchTreeViewItemStyle"
            TargetType="{x:Type TreeViewItem}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
            <Setter Property="Padding" Value="1,0,0,0"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="Visibility" Value="{Binding Visibility, Mode=OneWay}" />
            <Setter Property="IsEnabled" Value="{Binding IsEnabled, Mode=OneWay}" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="view:TreeViewItemKeyboardShortcut.InputBindings">
                <Setter.Value>
                    <InputBindingCollection>
                        <KeyBinding Key="Space" Command="local:WorkspacePageView.ToggleSelectedAction" />
                        <KeyBinding Key="Return" Command="local:WorkspacePageView.ToggleSelectedAction" />
                    </InputBindingCollection>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition MinWidth="19" Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
                            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.Row="1"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="false">
                                <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="HasItems" Value="false">
                                <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="true"/>
                                    <Condition Property="IsSelectionActive" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="VirtualizingStackPanel.IsVirtualizing" Value="true">
                    <Setter Property="ItemsPanel">
                        <Setter.Value>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel/>
                            </ItemsPanelTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <!-- Main UI -->
    <DockPanel x:Name="Workstation">
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Header="_Exit" Command="ApplicationCommands.Close"/>
            </MenuItem>
            <MenuItem Header="F_ilter">
                <MenuItem Header="_Small: Less than 0.1 mb" Command="local:WorkspacePageView.Filter3Action"/>
                <MenuItem Header="_Medium: Less than 0.5 mb" Command="local:WorkspacePageView.Filter2Action"/>
                <MenuItem Header="_Big: Less than 1 mb" Command="local:WorkspacePageView.Filter1Action"/>
                <Separator />
                <MenuItem Header="_Clear Filter" Command="local:WorkspacePageView.ClearFilterAction"/>
            </MenuItem>
        </Menu>
        <TreeView x:Name="TreeView"
                  DockPanel.Dock="Left"
                  ItemContainerStyle="{StaticResource StretchTreeViewItemStyle}"
                  ItemsSource="{Binding Roots, Mode=OneTime}"
                  ItemTemplate="{StaticResource CheckBoxItemTemplate}">
        </TreeView>
        <Grid DockPanel.Dock="Right" Margin="20">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Orientation="Vertical" Grid.Row="0" Margin="0,0,0,20">
                <TextBlock Text="{Binding OriginalSizeText}" />
                <TextBlock Text="{Binding CurrentSizeText}" />
                <TextBlock Text="{Binding SavingsSizeText}" />
            </StackPanel>
            <lvc:PieChart 
                LegendLocation="Bottom" 
                Grid.Row="1"
                x:Name="PieChart">
            </lvc:PieChart>
        </Grid>
    </DockPanel>
</UserControl>
