<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:userControls="clr-namespace:Filtration.UserControls"
             xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
             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"
             xmlns:designTime="clr-namespace:Filtration.ViewModels.DesignTime"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance Type=designTime:DesignTimeItemFilterBlockViewModel, IsDesignTimeCreatable=True}"
             d:DesignHeight="400" d:DesignWidth="817">
    <UserControl.Resources>
        <ResourceDictionary>
            <views:BindingProxy x:Key="Proxy" Data="{Binding}" />
            <converters:BlockGroupAdvancedFillColorConverter x:Key="BlockGroupAdvancedFillColorConverter" />
            <converters:MinimapIconToCroppedBitmapConverter x:Key="MinimapIconToCroppedBitmapConverter"/>
            <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>
    </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" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <!-- BlockItems Summary Panel -->
                            <StackPanel Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="0,5">
                                <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">
                                <Image Height="40" Width="40" Margin="0,0,10,0" Stretch="Fill">
                                    <Image.Source>
                                        <MultiBinding Converter="{StaticResource MinimapIconToCroppedBitmapConverter}">
                                            <Binding Path="DisplayIconSize"/>
                                            <Binding Path="DisplayIconColor"/>
                                            <Binding Path="DisplayIconShape"/>
                                        </MultiBinding>
                                    </Image.Source>
                                </Image>
                                <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>
                                <Button Command="{Binding PlayCustomSoundCommand}"
                                        Width="25"
                                        Height="25"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Center"
                                        Margin="0,0,3,0"
                                        Visibility="{Binding HasCustomSound, 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>
                                <Line Y1="5" Y2="38" StrokeThickness="2" Panel.ZIndex="999"
                                      Stroke="{Binding DisplayEffectColor, Converter={StaticResource ColorToSolidColorBrushConverter}, Mode=OneWay}"
                                      Margin="10,2,0,0" >
                                    <Line.Effect>
                                        <DropShadowEffect BlurRadius="5" ShadowDepth="0" Color="{Binding DisplayEffectColor, Mode=OneWay}" Direction="0"/>
                                    </Line.Effect>
                                </Line>
                                <ToggleButton Margin="-10,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 Height="Auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <WrapPanel Grid.Row="0">
                            <TextBlock Text="Group" VerticalAlignment="Center" FontWeight="Bold" />
                            <ItemsControl ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding BlockGroups}"
                                     BorderThickness="0">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel IsItemsHost="True" Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <WrapPanel VerticalAlignment="Center">
                                            <TextBlock Text=" > " FontWeight="ExtraBold" />
                                            <TextBlock Text="{Binding GroupName}" />
                                        </WrapPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                            <Button Height="10" Command="{Binding DeleteBlockGroupCommand}" Content="{StaticResource DeleteIcon}" ToolTip="Delete"
                                    Background="Transparent" BorderThickness="0" Margin="2,3,0,0" VerticalAlignment="Top">
                                <Button.Style>
                                    <Style TargetType="{x:Type Button}" >
                                        <Style.Triggers>
                                            <Trigger Property="IsEnabled" Value="False">
                                                <Setter Property="Visibility" Value="Collapsed" />
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </Button.Style>
                            </Button>
                            <toolkit:AutoCompleteBox ItemsSource="{Binding BlockGroupSuggestions}" Text="{Binding BlockGroupSearch, Mode=TwoWay}"
                                                     MinWidth="100" Margin="5,0,0,0" BorderThickness="0, 0, 0, 1" FilterMode="Contains"
                                                     PreviewKeyDown="AutoCompleteBox_PreviewKeyDown" />
                            <Button Height="12" Command="{Binding AddBlockGroupCommand}" Content="{StaticResource AddIcon}"
                                    ToolTip="Add" Background="Transparent" BorderThickness="0" Margin="3,0,0,0" />
                        </WrapPanel>

                        <!-- Regular Block Items-->
                        <Grid Grid.Row="1" Visibility="{Binding AudioVisualBlockItemsGridVisible, Converter={StaticResource InverseBooleanVisibilityConverter}}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <!-- Add Block Item Links -->
                            <ItemsControl Grid.Column="0" ItemsSource="{Binding BlockItemTypesAvailable}" Grid.Row="0" Margin="0,0,0,10">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <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 -->
                            <userControls:EnableDisableToggleButton Grid.Row="0" Grid.Column="1" VerticalAlignment="Top" />

                            <!-- Block Items -->
                            <Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" MaxHeight="200">
                                <Grid.Resources>
                                    <CollectionViewSource Source="{Binding RegularBlockItems}" x:Key="BlockItemsCollectionViewSource">
                                        <CollectionViewSource.SortDescriptions>
                                            <componentModel:SortDescription PropertyName="SortOrder"/>
                                        </CollectionViewSource.SortDescriptions>
                                    </CollectionViewSource>
                                </Grid.Resources>
                                <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" PreviewMouseWheel="UIElement_OnPreviewMouseWheel">
                                    <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>
                                </ScrollViewer>
                            </Grid>
                        </Grid>

                        <!-- Audio/Visual Block Items-->
                        <Grid Grid.Row="1" Visibility="{Binding AudioVisualBlockItemsGridVisible, Converter={StaticResource BooleanVisibilityConverter}}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <!-- Add AudioVisual Block Item Links -->
                            <ItemsControl Grid.Column="0" 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>

                            <!-- Enable/Disable Block Button -->
                            <userControls:EnableDisableToggleButton Grid.Row="0" Grid.Column="1" VerticalAlignment="Top" />

                            <TextBlock Grid.Column="0" Grid.ColumnSpan="2" 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 -->
                            <Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" MaxHeight="200">
                                <Grid.Resources>
                                    <CollectionViewSource Source="{Binding AudioVisualBlockItems}" x:Key="BlockItemsCollectionViewSource">
                                        <CollectionViewSource.SortDescriptions>
                                            <componentModel:SortDescription PropertyName="SortOrder"/>
                                        </CollectionViewSource.SortDescriptions>
                                    </CollectionViewSource>
                                </Grid.Resources>
                                <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" PreviewMouseWheel="UIElement_OnPreviewMouseWheel">
                                    <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>
                                </ScrollViewer>
                            </Grid>
                        </Grid>
                        <Grid Grid.Row="2" Margin="0,5,0,5">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,0,8">
                                <Hyperlink Command="{Binding SwitchBlockItemsViewCommand}">
                                    <Grid>
                                        <TextBlock Text="Switch to Regular Block Items" Visibility="{Binding AudioVisualBlockItemsGridVisible, Converter={StaticResource BooleanVisibilityConverter}}" />
                                        <TextBlock Text="Switch to Appearance Block Items" Visibility="{Binding AudioVisualBlockItemsGridVisible, Converter={StaticResource InverseBooleanVisibilityConverter}}" />
                                    </Grid>
                                </Hyperlink>
                            </TextBlock>
                            <TextBlock Grid.Row="1" Grid.Column="0" Margin="0,0,5,0" Text="Description:" VerticalAlignment="Center" />
                            <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding BlockDescription}" />
                        </Grid>
                    </Grid>
                </Expander>
            </Grid>
        </Border>
    </Grid>
</UserControl>
