<UserControl x:Class="_1RM.View.Launcher.ServerSelectionsView"
             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.Launcher"
             xmlns:model="clr-namespace:_1RM.Model"
             xmlns:view="clr-namespace:_1RM.View"
             xmlns:attachProperty="clr-namespace:Shawn.Utils.WpfResources.Theme.AttachProperty;assembly=Shawn.Utils.WpfResources"
             xmlns:s="https://github.com/canton7/Stylet"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance local:ServerSelectionsViewModel}"
             d:DesignHeight="450" d:DesignWidth="400">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="LauncherDictionary.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>

            <VisualBrush x:Key="HintText" TileMode="None" Stretch="None" AlignmentX="Left">
                <VisualBrush.Visual>
                    <Grid VerticalAlignment="Center">
                        <TextBlock Style="{StaticResource TextBlockBase}" FontStyle="Italic" Text="{DynamicResource filter_mainwindow}" Foreground="Black" Opacity="0.5" FontSize="18" />
                        <TextBlock Style="{StaticResource TextBlockBase}" FontStyle="Italic" Text="{DynamicResource filter_mainwindow}" Foreground="White" Opacity="0.5" FontSize="18" />
                    </Grid>
                </VisualBrush.Visual>
            </VisualBrush>

            <Style x:Key="SearchBoxTbKeyWordStyle" TargetType="TextBox" BasedOn="{StaticResource SearchBoxTbKeyWordStyleBase}">
                <Style.Triggers>
                    <Trigger Property="Text" Value="{x:Null}">
                        <Setter Property="Background" Value="{StaticResource HintText}" />
                    </Trigger>
                    <Trigger Property="Text" Value="">
                        <Setter Property="Background" Value="{StaticResource HintText}" />
                    </Trigger>
                </Style.Triggers>
            </Style>


            <Style x:Key="TextBlockTagStringStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockBase}">
                <Setter Property="Opacity" Value="0.5"></Setter>
                <Setter Property="FontSize" Value="10"></Setter>
                <Setter Property="VerticalAlignment" Value="Center"></Setter>
                <Setter Property="TextTrimming" Value="CharacterEllipsis"></Setter>
                <Setter Property="Margin" Value="5 0 0 0"></Setter>
                <Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"></Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                        <Setter Property="Foreground" Value="{DynamicResource AccentTextBrush}"></Setter>
                        <Setter Property="Opacity" Value="0.8"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

            <Style x:Key="TextBlocProtocolNameStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockBase}">
                <Setter Property="Opacity" Value="0.3"></Setter>
                <Setter Property="FontSize" Value="16"></Setter>
                <Setter Property="FontWeight" Value="Bold"></Setter>
                <Setter Property="HorizontalAlignment" Value="Right"></Setter>
                <Setter Property="VerticalAlignment" Value="Center"></Setter>
                <Setter Property="TextTrimming" Value="CharacterEllipsis"></Setter>
                <Setter Property="TextAlignment" Value="Right"></Setter>
                <Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"></Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                        <Setter Property="Foreground" Value="{DynamicResource AccentTextBrush}"></Setter>
                        <Setter Property="Opacity" Value="0.7"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

            <DataTemplate DataType="{x:Type view:ProtocolBaseViewModel}">
                <Grid Height="{x:Static view:LauncherWindowViewModel.LAUNCHER_SERVER_LIST_ITEM_HEIGHT}"
                      Width="{x:Static view:LauncherWindowViewModel.LAUNCHER_LIST_AREA_WIDTH}">
                    <!--<Grid.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding CmdConnServer}"></MouseBinding>
                        <MouseBinding MouseAction="RightClick" 
                                      Command="{Binding  RelativeSource={RelativeSource FindAncestor, AncestorType=local:ServerSelectionsView}, Path=DataContext.CmdShowActionsList}"
                                      CommandParameter="{Binding}"
                                      ></MouseBinding>
                    </Grid.InputBindings>-->
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition> <!-- Main title -->
                        <ColumnDefinition Width="Auto"></ColumnDefinition> <!-- Tag auto size -->
                        <ColumnDefinition Width="Auto"></ColumnDefinition> <!--Note button-->
                        <ColumnDefinition Width="50"></ColumnDefinition> <!--Protocol-->
                        <ColumnDefinition Width="10"></ColumnDefinition> <!--placeholder-->
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>

                    <Image Width="30" Height="30" Margin="5 0"
                           Grid.Column="0" Grid.RowSpan="2"
                           Source="{Binding Server.IconImg}"></Image>

                    <!--main title-->
                    <ContentControl Grid.Row="0" 
                                    Grid.Column="1" Grid.ColumnSpan="2"
                                    ToolTip="{Binding DisplayName}"
                                    s:View.Model="{Binding LauncherMainTitleViewModel}">
                        <ContentControl.Resources>
                            <Style TargetType="TextBlock" BasedOn="{StaticResource TextBlockBase}">
                                <Setter Property="Opacity" Value="0.8"></Setter>
                                <Setter Property="FontSize" Value="18"></Setter>
                                <Setter Property="VerticalAlignment" Value="Center"></Setter>
                                <Setter Property="TextTrimming" Value="CharacterEllipsis"></Setter>
                                <Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"></Setter>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                                        <Setter Property="Foreground" Value="{DynamicResource AccentTextBrush}"></Setter>
                                        <Setter Property="Opacity" Value="1"></Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ContentControl.Resources>
                    </ContentControl>

                    <Grid Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="4">

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <!--subtitle-->
                        <ContentControl Grid.Column="0"
                                        ToolTip="{Binding SubTitle}"
                                        Opacity="0.8" s:View.Model="{Binding LauncherSubTitleViewModel}"
                                        Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:ServerSelectionsView}}, Path=DataContext.ShowCredentials, Converter={StaticResource ConverterBool2Visible}}">
                            <ContentControl.Resources>
                                <Style TargetType="TextBlock" BasedOn="{StaticResource TextBlockBase}">
                                    <Setter Property="Opacity" Value="0.6"></Setter>
                                    <Setter Property="FontSize" Value="12"></Setter>
                                    <Setter Property="VerticalAlignment" Value="Center"></Setter>
                                    <Setter Property="TextTrimming" Value="CharacterEllipsis"></Setter>
                                    <Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"></Setter>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                                            <Setter Property="Foreground" Value="{DynamicResource AccentTextBrush}"></Setter>
                                            <Setter Property="Opacity" Value="1"></Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ContentControl.Resources>
                        </ContentControl>
                        <!--tags-->
                        <TextBlock Grid.Column="1"
                                   VerticalAlignment="Bottom"
                                   Margin="10 0 0 0"
                                   MaxWidth="120"
                                   Text="{Binding TagString}"
                                   Style="{StaticResource TextBlockTagStringStyle}"
                                   ToolTip="{Binding TagString}">
                        </TextBlock>
                    </Grid>


                    <!--data source-->
                    <TextBlock Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4"
                               HorizontalAlignment="Right"
                               VerticalAlignment="Top"
                               Text="{Binding DataSourceNameForLauncher}"
                               Style="{StaticResource TextBlockTagStringStyle}"
                               Opacity="0.15"
                               >
                    </TextBlock>






                    <!--Note button-->
                    <Button Grid.Row="0" Grid.RowSpan="2" Grid.Column="3" Background="Transparent" ToolTip="press '←' key to show/hide Note"
                            attachProperty:VisionElement.BackgroundAccent="Transparent"
                            attachProperty:VisionElement.BackgroundOnMouseDown="Transparent"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            attachProperty:VisionElement.Opacity="0.8"
                            attachProperty:VisionElement.OpacityAccent="0.9"
                            Visibility="{Binding Server.Note, Converter={StaticResource ConverterNoteToVisibility}}"
                            Tag="{Binding Server.Id}"
                            PreviewMouseDown="ButtonShowNote_OnClick"
                            Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=view:LauncherWindowView}, Path=DataContext.ServerSelectionsViewModel.CmdShowNoteField}"
                            Style="{StaticResource ButtonBarPrimaryStyle}"
                            IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=view:LauncherWindowView}, Path=DataContext.ServerSelectionsViewModel.IsShowNoteFieldEnabled}"
                    >
                        <Path Data="{StaticResource NoteGeometry}"
                              Width="16" Height="16"
                              Stroke="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=Button}}"
                              Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=Button}}"
                              StrokeThickness="0" Stretch="Uniform" />
                    </Button>

                    <!--ProtocolDisplayName-->
                    <Viewbox Grid.Row="0" Grid.RowSpan="2" Grid.Column="4"
                             Width="50"
                             VerticalAlignment="Center"
                             HorizontalAlignment="Right"
                             StretchDirection="DownOnly" Stretch="Uniform">
                        <TextBlock Text="{Binding Server.ProtocolDisplayName}" 
                                   Style="{StaticResource TextBlocProtocolNameStyle}"/>
                    </Viewbox>
                </Grid>
            </DataTemplate>

        </ResourceDictionary>
    </UserControl.Resources>
    <Grid Background="{DynamicResource PrimaryMidBrush}">

        <!--servers list title-->
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>

            <Grid Name="GridKeyword"
                  Width="{x:Static view:LauncherWindowViewModel.LAUNCHER_LIST_AREA_WIDTH}" 
                  Height="{x:Static view:LauncherWindowViewModel.LAUNCHER_GRID_KEYWORD_HEIGHT}">
                <TextBox Name="TbKeyWord"
                         Foreground="{DynamicResource PrimaryTextBrush}"
                         PreviewKeyDown="TbKeyWord_OnPreviewKeyDown"
                         Margin="10 0 0 0"
                         Text="{Binding Filter, UpdateSourceTrigger=PropertyChanged}"
                         Style="{StaticResource SearchBoxTbKeyWordStyle}"></TextBox>

                <!-- show tags -->
                <Grid Height="40" HorizontalAlignment="Right" Visibility="{Binding TagFilters.Count, Converter={StaticResource ConverterEqual2Collapsed}, ConverterParameter='0'}">
                    <ItemsControl VerticalAlignment="Center" HorizontalAlignment="Center" ItemsSource="{Binding TagFilters}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate DataType="model:TagFilter">
                                <Border VerticalAlignment="Center" Background="{DynamicResource AccentMidBrush}" CornerRadius="5" Margin="3 0">
                                    <Grid ToolTip="{Binding TagName}">
                                        <TextBlock Text="{Binding TagName}" 
                                                   Foreground="{DynamicResource AccentTextBrush}" 
                                                   VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5">
                                            <TextBlock.Style>
                                                <Style TargetType="TextBlock" BasedOn="{StaticResource TextBlockBase}">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding IsExcluded}" Value="True">
                                                            <Setter Property="TextDecorations">
                                                                <Setter.Value>
                                                                    <TextDecorationCollection>
                                                                        <TextDecoration Location="Strikethrough">
                                                                            <TextDecoration.Pen>
                                                                                <Pen Brush="{DynamicResource PrimaryMidBrush}" Thickness="3"></Pen>
                                                                            </TextDecoration.Pen>
                                                                        </TextDecoration>
                                                                    </TextDecorationCollection>
                                                                </Setter.Value>
                                                            </Setter>
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </TextBlock.Style>
                                        </TextBlock>
                                    </Grid>
                                </Border>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Grid>
            </Grid>


            <!--servers list body-->
            <ListBox x:Name="ListBoxSelections"
                     Grid.Row="1"
                     Grid.IsSharedSizeScope="True"
                     BorderThickness="0"
                     PreviewMouseRightButtonDown="ListBoxSelections_OnMouseRightButtonDown"
                     PreviewMouseLeftButtonDown="ListBoxSelections_OnPreviewMouseLeftButtonDown"
                     SelectedIndex="{Binding SelectedIndex}"
                     ItemsSource="{Binding VmServerList}"
                     Style="{StaticResource SearchBoxSelectionList}">
            </ListBox>

        </Grid>


        <!--actions of selected server-->
        <Grid Name="GridActionsList" Visibility="Hidden" Background="{DynamicResource PrimaryMidBrush}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid Width="{x:Static view:LauncherWindowViewModel.LAUNCHER_LIST_AREA_WIDTH}" 
                  Height="{x:Static view:LauncherWindowViewModel.LAUNCHER_GRID_KEYWORD_HEIGHT}">
                <Grid VerticalAlignment="Center">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="40"></ColumnDefinition>
                        <ColumnDefinition Width="30"></ColumnDefinition>
                        <ColumnDefinition Width="5"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <Grid Grid.Row="0" Grid.Column="0" Background="{DynamicResource PrimaryMidBrush}" MouseDown="ButtonActionBack_OnMouseDown">
                        <Path Margin="2" Fill="{DynamicResource PrimaryLightBrush}" Height="25" Data="F1 M 16.591797 2.431641 L 9.023438 10 L 16.591797 17.568359 L 14.238281 19.931641 L 4.306641 10 L 14.238281 0.068359 L 16.591797 2.431641 Z " Stretch="Uniform"></Path>
                    </Grid>

                    <Grid Grid.Row="0" Grid.Column="1">
                        <Image Source="{Binding SelectedItem.Server.IconImg}" Width="{Binding Path=Width, RelativeSource={RelativeSource FindAncestor,AncestorType=Grid}}"></Image>
                    </Grid>

                    <Grid Grid.Column="3">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="Auto"></RowDefinition>
                        </Grid.RowDefinitions>

                        <Grid Grid.Row="0" Grid.Column="0">
                            <TextBlock Style="{StaticResource TextBlockBase}"
                                       TextTrimming="CharacterEllipsis"
                                       FontSize="18" Foreground="{DynamicResource PrimaryTextBrush}"
                                       VerticalAlignment="Center"
                                       Text="{Binding SelectedItem.Server.DisplayName}"></TextBlock>
                        </Grid>

                        <Grid Grid.Row="1" Grid.Column="0" Opacity="0.6">
                            <TextBlock Style="{StaticResource TextBlockBase}"
                                       TextTrimming="CharacterEllipsis"
                                       FontSize="12" Foreground="{DynamicResource PrimaryTextBrush}"
                                       VerticalAlignment="Top"
                                       Text="{Binding SelectedItem.Server.SubTitle}"></TextBlock>
                        </Grid>

                        <TextBlock Style="{StaticResource TextBlockBase}"
                                   Grid.Row="0" Grid.Column="1" Grid.RowSpan="2"
                                   Text="{Binding SelectedItem.Server.ProtocolDisplayName}"
                                   FontSize="18" FontWeight="Bold"
                                   Foreground="{DynamicResource PrimaryMidBrush}"
                                   Opacity="0.4"
                                   HorizontalAlignment="Right"
                                   Margin="10 0"
                                   VerticalAlignment="Center"></TextBlock>
                        <TextBlock Style="{StaticResource TextBlockBase}"
                                   Grid.Row="0" Grid.Column="1" Grid.RowSpan="2"
                                   Text="{Binding SelectedItem.Server.ProtocolDisplayName}"
                                   FontSize="18" FontWeight="Bold"
                                   Foreground="{DynamicResource PrimaryTextBrush}"
                                   Opacity="0.2"
                                   HorizontalAlignment="Right"
                                   Margin="10 0"
                                   VerticalAlignment="Center"></TextBlock>
                    </Grid>
                </Grid>
            </Grid>

            <ListBox x:Name="ListBoxActions"
                     Grid.Row="1"
                     PreviewMouseRightButtonDown="ListBoxActions_OnMouseRightButtonDown"
                     PreviewMouseDoubleClick="ListBoxActions_OnMouseDoubleClick"
                     Style="{StaticResource SearchBoxActionList}"
                     BorderThickness="0"
                     SelectedIndex="{Binding SelectedActionIndex}"
                     ItemsSource="{Binding Actions}">
            </ListBox>
        </Grid>

    </Grid>
</UserControl>
