<UserControl x:Class="_1RM.View.ServerList.TagsPanelView"
             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:_1RM.View.ServerList"
             xmlns:model="clr-namespace:_1RM.Model"
             xmlns:attachProperty="clr-namespace:Shawn.Utils.WpfResources.Theme.AttachProperty;assembly=Shawn.Utils.WpfResources"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance local:TagsPanelViewModel}"
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <CollectionViewSource x:Key="TagItemSource" 
                              Source="{Binding GlobalData.TagList}"
                              Filter="TagItemSource_OnFilter">
        </CollectionViewSource>
        <local:ConverterTagNameCount x:Key="ConverterTagNameCount" />
    </UserControl.Resources>

    <Grid Margin="0 21 0 0" Background="{DynamicResource BackgroundBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>

        <Grid Row="1" Margin="5">
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                <TextBlock Text="{Binding GlobalData.TagList.Count, StringFormat={}{0}tags}" 
                           Foreground="{DynamicResource BackgroundTextBrush}"
                           FontSize="18"
                           ></TextBlock>
            </StackPanel>
            <TextBox Grid.Column="0" Name="TbFilter"
                     attachProperty:FocusExtension.IsFocused="{Binding FilterIsFocused}"
                     Height="40"
                     Text="{Binding FilterString, UpdateSourceTrigger=PropertyChanged}" 
                     Tag="{DynamicResource 'Search (Ctrl+F)'}"
                     PreviewKeyUp="TbFilter_OnKeyUp"
                     Width="300"
            ></TextBox>
        </Grid>

        <Grid Row="2">
            <TextBlock Foreground="{DynamicResource BackgroundTextBrush}" 
                       Opacity="0.5"
                       HorizontalAlignment="Center"
                       Text="{DynamicResource 'This is your list of tags. Right click for a context menu to pin them on header.'}"></TextBlock>
        </Grid>

        <!-- tag list: the page to config which tag should be display. -->
        <Grid Grid.Row="3">
            <ListBox Name="ListBoxTags" Margin="0 10 0 0"
                     ItemsSource="{Binding Source={StaticResource TagItemSource}}" 
                     ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                     >
                <ListBox.Template>
                    <ControlTemplate TargetType="ListBox">
                        <ScrollViewer>
                            <ItemsPresenter></ItemsPresenter>
                        </ScrollViewer>
                    </ControlTemplate>
                </ListBox.Template>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel></WrapPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="{DynamicResource Unpin}" Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem.IsPinned, Converter={StaticResource ConverterBool2Visible}}"
                                  Command="{Binding CmdTagPin}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}"/>
                        <MenuItem Header="{DynamicResource Pin}" Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem.IsPinned, Converter={StaticResource ConverterBool2VisibleInv}}"
                                  Command="{Binding CmdTagPin}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}"/>
                        <MenuItem Header="{DynamicResource Rename}" Command="{Binding CmdTagRename}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}"/>
                        <MenuItem Header="{DynamicResource Connect}" Command="{Binding CmdTagConnect}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}"/>
                        <!--<MenuItem Header="{DynamicResource 'Connect (New window)'}" Command="{Binding CmdTagConnectToNewTab}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}"/>-->
                        <MenuItem Header="{DynamicResource Delete}" Command="{Binding CmdTagDelete}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItem}"/>
                    </ContextMenu>
                </ItemsControl.ContextMenu>
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="ListBoxItem">
                                    <ContentPresenter />
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="model:Tag">
                        <Grid Margin="5">
                            <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:ServerListPageView}, Path=DataContext.CmdTagAddIncluded}" CommandParameter="{Binding}" 
                                            HorizontalAlignment="Left">
                                <Button.Content>
                                    <MultiBinding Converter="{StaticResource ConverterTagNameCount}">
                                        <Binding Path="Name"></Binding>
                                        <Binding Path=" ItemsCount"></Binding>
                                        <Binding Path=" IsPinned"></Binding>
                                    </MultiBinding>
                                </Button.Content>
                            </Button>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ListBox>
        </Grid>


    </Grid>
</UserControl>
