<UserControl x:Class="Filtration.Views.ItemFilterBlockView"
             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:viewModels="clr-namespace:Filtration.ViewModels"
             xmlns:userControls="clr-namespace:Filtration.UserControls"
             xmlns:views="clr-namespace:Filtration.Views"
             xmlns:converters="clr-namespace:Filtration.Converters"
             xmlns:blockItemBaseTypes="clr-namespace:Filtration.ObjectModel.BlockItemBaseTypes;assembly=Filtration.ObjectModel"
             xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance Type=viewModels:ItemFilterBlockViewModel}"
             d:DesignHeight="200" d:DesignWidth="800">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <views:BindingProxy x:Key="Proxy" Data="{Binding}" />
                    <converters:BlockGroupAdvancedFillColorConverter x:Key="BlockGroupAdvancedFillColorConverter" />
                    <Style TargetType="{x:Type ContentPresenter}" x:Key="BlockItemFadeInStyle">
                        <Setter Property="LayoutTransform">
                            <Setter.Value>
                                <ScaleTransform x:Name="transform" />
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <EventTrigger RoutedEvent="Loaded">
                                <EventTrigger.Actions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.5" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger.Actions>
                            </EventTrigger>
                        </Style.Triggers>
                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid x:Name="TopLevelGrid">
        <Grid x:Name="DisabledBlockOverlay" IsHitTestVisible="False" Panel.ZIndex="1000" Visibility="{Binding BlockEnabled, Converter={StaticResource InverseBooleanVisibilityConverter}}">
            <Grid.Background>
                <SolidColorBrush Color="Gray" Opacity=".5" />
            </Grid.Background>
        </Grid>
        <Border BorderThickness="1" BorderBrush="SlateGray" CornerRadius="2" Background="White">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Rectangle Width="7" Fill="{Binding AdvancedBlockGroup, Converter={StaticResource BlockGroupAdvancedFillColorConverter}}">
                    <Rectangle.ToolTip>
                        <ToolTip Visibility="{Binding AdvancedBlockGroup, Converter={StaticResource BooleanVisibilityConverter}}">Advanced Block Group</ToolTip>
                    </Rectangle.ToolTip>
                </Rectangle>
                <Expander Grid.Column="1"
                          Style="{StaticResource ExpanderRightAlignStyle}"
                          x:Name="BlockExpander"
                          MouseDoubleClick="BlockExpander_OnMouseDoubleClick"
                          ToolTip="{Binding BlockDescription}"
                          ToolTipService.IsEnabled="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsExpanded, Converter={StaticResource BoolInverterConverter}}"
                          IsExpanded="{Binding IsExpanded}">
                    <Expander.ContextMenu>
                        <ContextMenu>
                            <ContextMenu.Items>
                                <MenuItem Header="Copy Block" Command="{Binding CopyBlockCommand}" Icon="{StaticResource CopyIcon}" />
                                <MenuItem Header="Paste Block" Command="{Binding PasteBlockCommand}" Icon="{StaticResource PasteIcon}" />
                                <Separator />
                                <MenuItem Header="Block Style">
                                    <MenuItem Header="Copy Block Style" Command="{Binding CopyBlockStyleCommand}" Icon="{StaticResource CopyIcon}" />
                                    <MenuItem Header="Paste Block Style" Command="{Binding PasteBlockStyleCommand}" Icon="{StaticResource PasteIcon}" />
                                </MenuItem>
                                <MenuItem Header="Add Block" Command="{Binding Data.AddBlockCommand, Source={StaticResource Proxy}}" Icon="{StaticResource AddBlockIcon}" />
                                <MenuItem Header="Add Section" Command="{Binding Data.AddSectionCommand, Source={StaticResource Proxy}}" Icon="{StaticResource AddSectionIcon}" />
                                <Separator />
                                <MenuItem Header="Delete Block" Command="{Binding Data.DeleteBlockCommand, Source={StaticResource Proxy}}" Icon="{StaticResource DeleteIcon}" />
                                <Separator />
                                <MenuItem Header="Move Block To Top" Command="{Binding Data.MoveBlockToTopCommand, Source={StaticResource Proxy}}" Icon="{StaticResource MoveToTopIcon}" />
                                <MenuItem Header="Move Block Up" Command="{Binding Data.MoveBlockUpCommand, Source={StaticResource Proxy}}" Icon="{StaticResource MoveUpIcon}" />
                                <MenuItem Header="Move Block Down" Command="{Binding Data.MoveBlockDownCommand, Source={StaticResource Proxy}}" Icon="{StaticResource MoveDownIcon}" />
                                <MenuItem Header="Move Block To Bottom" Command="{Binding Data.MoveBlockToBottomCommand, Source={StaticResource Proxy}}" Icon="{StaticResource MoveToBottomIcon}" />
                                <Separator />
                                <MenuItem Header="Replace Colors" Command="{Binding Data.ReplaceColorsCommand, Source={StaticResource Proxy}}" Icon="{StaticResource ReplaceColorsIcon}" />
                            </ContextMenu.Items>
                        </ContextMenu>
                    </Expander.ContextMenu>
                    <Expander.Header>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <!-- BlockItems Summary Panel -->
                            <StackPanel Grid.Row="0" Grid.Column="0" VerticalAlignment="Center">
                                <StackPanel.Resources>
                                    <CollectionViewSource Source="{Binding SummaryBlockItems}" x:Key="SummaryBlockItemsCollectionViewSource">
                                        <CollectionViewSource.SortDescriptions>
                                            <componentModel:SortDescription PropertyName="SortOrder"/>
                                        </CollectionViewSource.SortDescriptions>
                                    </CollectionViewSource>
                                </StackPanel.Resources>
                                <ItemsControl ItemsSource="{Binding Source={StaticResource SummaryBlockItemsCollectionViewSource}}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <WrapPanel />
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                    <ItemsControl.Resources>
                                        <DataTemplate DataType="{x:Type blockItemBaseTypes:BlockItemBase}">
                                            <Border BorderBrush="Black" CornerRadius="4" Margin="0,2,2,2" BorderThickness="1" Background="{Binding SummaryBackgroundColor, Converter={StaticResource ColorToSolidColorBrushConverter}}">
                                                <TextBlock Text="{Binding SummaryText}" Margin="5,1,5,1" Foreground="{Binding SummaryTextColor, Converter={StaticResource ColorToSolidColorBrushConverter}}" />
                                            </Border>
                                        </DataTemplate>
                                        <DataTemplate DataType="{x:Type blockItemBaseTypes:ActionBlockItem}">
                                            <Button Command="{Binding ElementName=TopLevelGrid, Path=DataContext.ToggleBlockActionCommand}" Style="{StaticResource ChromelessButton}">
                                            <Border BorderBrush="Black" CornerRadius="4" Margin="0,2,2,2" BorderThickness="1" Background="{Binding SummaryBackgroundColor, Converter={StaticResource ColorToSolidColorBrushConverter}}">
                                                <TextBlock Text="{Binding SummaryText}" Margin="5,1,5,1" Foreground="{Binding SummaryTextColor, Converter={StaticResource ColorToSolidColorBrushConverter}}" />
                                            </Border>
                                            </Button>
                                        </DataTemplate>
                                    </ItemsControl.Resources>
                                </ItemsControl>
                            </StackPanel>
                           
                            <!-- Item Preview Box -->
                            <WrapPanel Grid.Row="0" Grid.Column="2" VerticalAlignment="Center">
                                <Button Command="{Binding PlaySoundCommand}"
                                        Width="25"
                                        Height="25"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Center"
                                        Margin="0,0,3,0"
                                        Visibility="{Binding HasSound, Converter={StaticResource BooleanVisibilityConverter}}"
                                        Background="Transparent"
                                        BorderBrush="Transparent"
                                        ToolTip="Click to preview drop sound">
                                    <Image Source="/Filtration;component/Resources/Icons/speaker_icon.png"  VerticalAlignment="Center" HorizontalAlignment="Center" />
                                </Button>
                                <Button Command="{Binding PlayPositionalSoundCommand}"
                                        Width="25"
                                        Height="25"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Center"
                                        Margin="0,0,3,0"
                                        Visibility="{Binding HasPositionalSound, Converter={StaticResource BooleanVisibilityConverter}}"
                                        Background="Transparent"
                                        BorderBrush="Transparent"
                                        ToolTip="Click to preview drop sound">
                                    <Image Source="/Filtration;component/Resources/Icons/speaker_icon.png"  VerticalAlignment="Center" HorizontalAlignment="Center" />
                                </Button>
                                <ToggleButton Margin="0,2,2,2"
                                              Style="{StaticResource ChromelessToggleButton}"
                                              x:Name="ItemPreviewButton"
                                              IsChecked="{Binding AudioVisualBlockItemsGridVisible}"
                                              ToolTip="Click here to change color and font settings" Cursor="Hand" >
                                    <userControls:ItemPreviewControl TextColor="{Binding DisplayTextColor}"
                                                                     BackgroundColor="{Binding DisplayBackgroundColor}"
                                                                     BorderColor="{Binding DisplayBorderColor}"
                                                                     BlockFontSize="{Binding DisplayFontSize}" />
                                </ToggleButton>
                            </WrapPanel>
                        </Grid>
                    </Expander.Header>
                    <Grid Margin="10,5,10,5" x:Name="BlockItemsGrid">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        
                        <Grid Grid.Row="0" Visibility="{Binding AudioVisualBlockItemsGridVisible, Converter={StaticResource InverseBooleanVisibilityConverter}}">
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Right">
                                <Hyperlink Command="{Binding SwitchBlockItemsViewCommand}">
                                    Switch to Appearance Block Items
                                </Hyperlink>
                            </TextBlock>
                        
                            <!-- Add Block Item Links -->
                            <ItemsControl Grid.Column="0" ItemsSource="{Binding BlockItemTypesAvailable}" Grid.Row="0" Margin="0,0,0,10">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel></WrapPanel>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Margin="0,0,3,0">
                                            <Hyperlink Command="{Binding ElementName=TopLevelGrid, Path=DataContext.AddFilterBlockItemCommand}" CommandParameter="{Binding}">
                                                <TextBlock>+</TextBlock><TextBlock Text="{Binding Path=., Converter={StaticResource BlockItemTypeToStringConverter}}" />
                                            </Hyperlink>
                                        </TextBlock>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>

                            <!-- Enable/Disable Block Button -->
                            <ToggleButton Grid.Row="0"
                                          Grid.Column="1"
                                          Style="{StaticResource ChromelessToggleButton}" 
                                          IsChecked="{Binding BlockEnabled}"
                                          Margin="0,0,5,0"
                                          ToolTip="Enable/Disable Block"
                                          Cursor="Hand"
                                          Width="25"
                                          Height="25">
                                <Image RenderOptions.BitmapScalingMode="HighQuality">
                                    <Image.Style>
                                        <Style TargetType="{x:Type Image}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding BlockEnabled}" Value="true">
                                                    <Setter Property="Source" Value="/Filtration;component/Resources/Icons/standby_enabled_icon.png"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding BlockEnabled}" Value="false">
                                                    <Setter Property="Source" Value="/Filtration;component/Resources/Icons/standby_disabled_icon.png"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Image.Style>
                                </Image>
                            </ToggleButton>
                            
                            <!-- Block Items -->
                            <WrapPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" MaxHeight="200">
                                <WrapPanel.Resources>
                                    <CollectionViewSource Source="{Binding RegularBlockItems}" x:Key="BlockItemsCollectionViewSource">
                                        <CollectionViewSource.SortDescriptions>
                                            <componentModel:SortDescription PropertyName="SortOrder"/>
                                        </CollectionViewSource.SortDescriptions>
                                    </CollectionViewSource>
                                </WrapPanel.Resources>
                                <ItemsControl ItemsSource="{Binding Source={StaticResource BlockItemsCollectionViewSource}}"
                                              ItemContainerStyle="{StaticResource BlockItemFadeInStyle}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <WrapPanel Orientation="Vertical" />
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <userControls:BlockItemControl BlockItem="{Binding}" RemoveItemCommand="{Binding ElementName=TopLevelGrid, Path=DataContext.RemoveFilterBlockItemCommand}" RemoveEnabled="{Binding Path=., Converter={StaticResource BlockItemToRemoveEnabledVisibilityConverter}}" />
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </WrapPanel>
                        </Grid>
                        <Grid Grid.Row="0" Visibility="{Binding AudioVisualBlockItemsGridVisible, Converter={StaticResource BooleanVisibilityConverter}}">
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Right">
                                <Hyperlink Command="{Binding SwitchBlockItemsViewCommand}">
                                    Switch to Regular Block Items
                                </Hyperlink>
                            </TextBlock>
                            <!-- Add AudioVisual Block Item Links -->
                            <ItemsControl ItemsSource="{Binding AudioVisualBlockItemTypesAvailable}" Grid.Row="0" Margin="0,0,0,10">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel></WrapPanel>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Margin="0,0,3,0">
                                            <Hyperlink Command="{Binding ElementName=TopLevelGrid, Path=DataContext.AddFilterBlockItemCommand}" CommandParameter="{Binding}">
                                                <TextBlock>+</TextBlock><TextBlock Text="{Binding Path=., Converter={StaticResource BlockItemTypeToStringConverter}}" />
                                            </Hyperlink>
                                        </TextBlock>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>

                            <TextBlock Grid.Row="1" FontStyle="Italic" HorizontalAlignment="Left" Visibility="{Binding HasAudioVisualBlockItems, Converter={StaticResource InverseBooleanVisibilityConverter}}">To change the appearance of this block, add a Text, Background or Border Block Item above.</TextBlock>
                            
                            <!-- Block Items -->
                            <WrapPanel Grid.Row="1" MaxHeight="200">
                                <WrapPanel.Resources>
                                    <CollectionViewSource Source="{Binding AudioVisualBlockItems}" x:Key="BlockItemsCollectionViewSource">
                                        <CollectionViewSource.SortDescriptions>
                                            <componentModel:SortDescription PropertyName="SortOrder"/>
                                        </CollectionViewSource.SortDescriptions>
                                    </CollectionViewSource>
                                </WrapPanel.Resources>
                                <ItemsControl ItemsSource="{Binding Source={StaticResource BlockItemsCollectionViewSource}}"
                                              ItemContainerStyle="{StaticResource BlockItemFadeInStyle}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <WrapPanel Orientation="Vertical" />
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <userControls:BlockItemControl BlockItem="{Binding}" RemoveItemCommand="{Binding ElementName=TopLevelGrid, Path=DataContext.RemoveFilterBlockItemCommand}" />
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </WrapPanel>
                        </Grid>
                        <Grid Grid.Row="1" Margin="0,5,0,5">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0"  Margin="0,0,5,0" Text="Description:" VerticalAlignment="Center" />
                            <TextBox Grid.Column="1" Text="{Binding BlockDescription}" />
                        </Grid>
                    </Grid>
                </Expander>
            </Grid>
        </Border>
    </Grid>
</UserControl>
