<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:extensions="clr-namespace:Filtration.Extensions"
             xmlns:blockItemBaseTypes="clr-namespace:Filtration.ObjectModel.BlockItemBaseTypes;assembly=Filtration.ObjectModel"
             xmlns:blockItemTypes="clr-namespace:Filtration.ObjectModel.BlockItemTypes;assembly=Filtration.ObjectModel"
             xmlns:enums="clr-namespace:Filtration.ObjectModel.Enums;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 Source="SharedResourcesDictionary.xaml" />
                <ResourceDictionary>
                    <views:BindingProxy x:Key="proxy" Data="{Binding}" />
                    <converters:BlockGroupAdvancedFillColorConverter x:Key="BlockGroupAdvancedFillColorConverter" />
                    
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid x:Name="TopLevelGrid">
        <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" Command="{Binding CopyBlockCommand}" Icon="{StaticResource CopyIcon}" />
                                <MenuItem Header="Paste" 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}">
                                </MenuItem>
                                <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" />
                            </Grid.ColumnDefinitions>
                            <!-- BlockItems Summary Panel -->
                            <StackPanel Grid.Row="0" Grid.Column="0" VerticalAlignment="Center">
                                <ItemsControl ItemsSource="{Binding SummaryBlockItems}">
                                    <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="1" VerticalAlignment="Center">
                                <Image Source="/Filtration;component/Resources/Icons/speaker_icon.png"
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Center"
                                       Height="15" Width="15"
                                       Margin="0,0,3,0"
                                       Visibility="{Binding HasSound, Converter={StaticResource BooleanVisibilityConverter}}" />
                                <ToggleButton Margin="0,2,8,2"
                                              Style="{StaticResource ChromelessToggleButton}"
                                              x:Name="ItemPreviewButton"
                                              IsChecked="{Binding DisplaySettingsPopupOpen, Mode=OneWayToSource}"
                                              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>

                            <!-- AudioVisual BlockItem Settings Popup -->
                            <Popup Grid.Row="0"
                                   Grid.Column="1"
                                   PlacementTarget="{Binding ElementName=ItemPreviewButton}"
                                   Placement="Left"
                                   PopupAnimation="Slide"
                                   StaysOpen="False"
                                   IsOpen="{Binding DisplaySettingsPopupOpen}"
                                   AllowsTransparency="True"
                                   HorizontalOffset="-5"
                                   Width="250">
                                <views:ItemFilterBlockDisplaySettingsView />
                            </Popup>
                        </Grid>
                    </Expander.Header>
                    <Grid Margin="10,5,10,5" x:Name="BlockItemsGrid">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        
                        <!-- Add Block Item Links -->
                        <ItemsControl 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>
                        
                        <!-- Block Items -->
                        <WrapPanel Grid.Row="1" MaxHeight="200">
                            <WrapPanel.Resources>
                                <CollectionViewSource Source="{Binding BlockItems}" x:Key="BlockItemsCollectionViewSource">
                                    <CollectionViewSource.SortDescriptions>
                                        <componentModel:SortDescription PropertyName="SortOrder"/>
                                    </CollectionViewSource.SortDescriptions>
                                </CollectionViewSource>
                            </WrapPanel.Resources>
                            <ItemsControl ItemsSource="{Binding Source={StaticResource BlockItemsCollectionViewSource}}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel Orientation="Vertical" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemContainerStyle>
                                    <Style TargetType="{x:Type ContentPresenter}">
                                        <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:2" />
                                                        </Storyboard>
                                                    </BeginStoryboard>
                                                </EventTrigger.Actions>
                                            </EventTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </ItemsControl.ItemContainerStyle>
                                <ItemsControl.Resources>
                                    <Style TargetType="{x:Type Border}" x:Key="BlockItemBorder">
                                        <Setter Property="BorderThickness" Value="1" />
                                        <Setter Property="BorderBrush" Value="Black" />
                                        <Setter Property="CornerRadius" Value="3" />
                                        <Setter Property="Width" Value="142" />
                                        <Setter Property="Margin" Value="0,0,5,5" />
                                    </Style>
                                    <DataTemplate DataType="{x:Type blockItemBaseTypes:ActionBlockItem}">
                                        <Border Style="{StaticResource BlockItemBorder}">
                                            <WrapPanel VerticalAlignment="Center" Margin="5,5,5,5">
                                                <RadioButton IsChecked="{Binding Action, Converter={StaticResource BooleanToBlockActionConverter}}" Margin="0,0,10,0">Show</RadioButton>
                                                <RadioButton IsChecked="{Binding Action, Converter={StaticResource BooleanToBlockActionInverseConverter}}">Hide</RadioButton>
                                            </WrapPanel>
                                        </Border>
                                    </DataTemplate>
                                    <DataTemplate DataType="{x:Type blockItemBaseTypes:NumericFilterPredicateBlockItem}">
                                        <Border Style="{StaticResource BlockItemBorder}">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition />
                                                    <RowDefinition />
                                                </Grid.RowDefinitions>
                                                <Border Background="PowderBlue" CornerRadius="3,3,0,0">
                                                    <Grid Margin="3,0,3,0">
                                                        <TextBlock Text="{Binding DisplayHeading}" Grid.Row="0" VerticalAlignment="Center" Foreground="Navy" />
                                                        <userControls:CrossButton Grid.Row="0" Height="12" HorizontalAlignment="Right" VerticalAlignment="Center" Command="{Binding ElementName=TopLevelGrid, Path=DataContext.RemoveFilterBlockItemCommand}" CommandParameter="{Binding}"/>
                                                    </Grid>
                                                </Border>
                                                <userControls:NumericFilterPredicateControl Grid.Row="1" Grid.Column="0" Margin="5,5,5,5" NumericFilterPredicate="{Binding FilterPredicate}" Minimum="{Binding Minimum, Mode=OneTime}" Maximum="{Binding Maximum, Mode=OneTime}" />
                                            </Grid>
                                        </Border>
                                    </DataTemplate>
                                    <DataTemplate DataType="{x:Type blockItemTypes:RarityBlockItem}">
                                        <Border Style="{StaticResource BlockItemBorder}">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition />
                                                    <RowDefinition Height="Auto" />
                                                </Grid.RowDefinitions>
                                                <Border Background="PowderBlue" CornerRadius="3,3,0,0">
                                                    <Grid Margin="3,0,3,0">
                                                        <TextBlock Text="{Binding DisplayHeading}" Grid.Row="0" VerticalAlignment="Center" Foreground="Navy" />
                                                        <userControls:CrossButton Grid.Row="0" Height="12" HorizontalAlignment="Right" VerticalAlignment="Center" Command="{Binding ElementName=TopLevelGrid, Path=DataContext.RemoveFilterBlockItemCommand}" CommandParameter="{Binding}"/>
                                                    </Grid>
                                                </Border>
                                                <StackPanel Orientation="Horizontal" Grid.Row="1" Margin="5,5,5,5">
                                                    <ComboBox ItemsSource="{Binding Source={extensions:Enumeration {x:Type enums:FilterPredicateOperator}}}"
                                                              DisplayMemberPath="Description"
                                                              SelectedValue="{Binding FilterPredicate.PredicateOperator}"
                                                              SelectedValuePath="Value" Width="50" Margin="0,0,6,0"  />
                                                    <ComboBox ItemsSource="{Binding Source={extensions:Enumeration {x:Type enums:ItemRarity}}}"
                                                              DisplayMemberPath="Description"
                                                              SelectedValue="{Binding FilterPredicate.PredicateOperand, Converter={StaticResource IntToItemRarityConverter}}"
                                                              SelectedValuePath="Value" />
                                                </StackPanel>
                                            </Grid>
                                        </Border>
                                    </DataTemplate>
                                    <!-- TODO: Sort out these messy duplicated data templates -->
                                    <DataTemplate DataType="{x:Type blockItemTypes:ClassBlockItem}">
                                        <Border Style="{StaticResource BlockItemBorder}">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition />
                                                    <RowDefinition Height="Auto" />
                                                </Grid.RowDefinitions>
                                                <Border Background="PowderBlue" CornerRadius="3,3,0,0">
                                                    <Grid Margin="3,0,3,0">
                                                        <TextBlock Text="{Binding DisplayHeading}" Grid.Row="0" VerticalAlignment="Center" Foreground="Navy" />
                                                        <userControls:CrossButton Grid.Row="0" Height="12" HorizontalAlignment="Right" VerticalAlignment="Center" Command="{Binding ElementName=TopLevelGrid, Path=DataContext.RemoveFilterBlockItemCommand}" CommandParameter="{Binding}"/>
                                                    </Grid>
                                                </Border>
                                                <userControls:EditableListBoxControl Grid.Row="1" Grid.Column="0" Margin="5,5,5,5" ItemsSource="{Binding Items}" AutoCompleteItemsSource="{Binding ElementName=TopLevelGrid, Path=DataContext.AutoCompleteItemClasses}" />
                                            </Grid>
                                        </Border>
                                    </DataTemplate>
                                    <DataTemplate DataType="{x:Type blockItemTypes:BaseTypeBlockItem}">
                                        <Border Style="{StaticResource BlockItemBorder}">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition />
                                                    <RowDefinition Height="Auto" />
                                                </Grid.RowDefinitions>
                                                <Border Background="PowderBlue" CornerRadius="3,3,0,0">
                                                    <Grid Margin="3,0,3,0">
                                                        <TextBlock Text="{Binding DisplayHeading}" Grid.Row="0" VerticalAlignment="Center" Foreground="Navy" />
                                                        <userControls:CrossButton Grid.Row="0" Height="12" HorizontalAlignment="Right" VerticalAlignment="Center" Command="{Binding ElementName=TopLevelGrid, Path=DataContext.RemoveFilterBlockItemCommand}" CommandParameter="{Binding}"/>
                                                    </Grid>
                                                </Border>
                                                <userControls:EditableListBoxControl Grid.Row="1" Grid.Column="0" Margin="5,5,5,5" ItemsSource="{Binding Items}" AutoCompleteItemsSource="{Binding ElementName=TopLevelGrid, Path=DataContext.AutoCompleteItemBaseTypes}" />
                                            </Grid>
                                        </Border>
                                    </DataTemplate>
                                    <DataTemplate DataType="{x:Type blockItemTypes:SocketGroupBlockItem}">
                                        <Border Style="{StaticResource BlockItemBorder}">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition />
                                                    <RowDefinition Height="Auto" />
                                                </Grid.RowDefinitions>
                                                <Border Background="PowderBlue" CornerRadius="3,3,0,0">
                                                    <Grid Margin="3,0,3,0">
                                                        <TextBlock Text="{Binding DisplayHeading}" Grid.Row="0" VerticalAlignment="Center" Foreground="Navy" />
                                                        <userControls:CrossButton Grid.Row="0" Height="12" HorizontalAlignment="Right" VerticalAlignment="Center" Command="{Binding ElementName=TopLevelGrid, Path=DataContext.RemoveFilterBlockItemCommand}" CommandParameter="{Binding}"/>
                                                    </Grid>
                                                </Border>
                                                <userControls:EditableListBoxControl Grid.Row="1" Grid.Column="0" Margin="5,5,5,5" ItemsSource="{Binding Items}" />
                                            </Grid>
                                        </Border>
                                    </DataTemplate>
                                    <DataTemplate DataType="{x:Type blockItemBaseTypes:ColorBlockItem}" />
                                    <DataTemplate DataType="{x:Type blockItemTypes:SoundBlockItem}" />
                                    <DataTemplate DataType="{x:Type blockItemTypes:FontSizeBlockItem}" />
                                </ItemsControl.Resources>
                            </ItemsControl>
                        </WrapPanel>
                        <Grid Grid.Row="2" 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>
