<UserControl x:Class="_1RM.Controls.ServerLineItem"
             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:view="clr-namespace:_1RM.View"
             xmlns:model="clr-namespace:_1RM.Model"
             xmlns:dao="clr-namespace:_1RM.Service.DataSource.DAO"
             xmlns:serverList="clr-namespace:_1RM.View.ServerList"
             d:DataContext="{d:DesignInstance view:ProtocolBaseViewModel}"
             mc:Ignorable="d"
             d:DesignHeight="30" d:DesignWidth="800">

    <UserControl.Resources>

        <Style x:Key="LineItemSettingMenuListBoxStyle" TargetType="{x:Type ListBox}">
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="Background" Value="{DynamicResource PrimaryMidBrush}" />
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="ListBoxItem" BasedOn="{StaticResource ControlBase}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="ListBoxItem">
                                    <Grid>
                                        <Border x:Name="GridContainer">
                                        </Border>
                                        <ContentPresenter x:Name="ContentPresenter"/>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate DataType="model:ProtocolAction">
                        <Button Padding="10" Style="{StaticResource ButtonBarPrimaryStyle}" 
                                Click="ServerMenuButton_OnClick"
                                CommandParameter="{Binding}"
                                HorizontalContentAlignment="Left"
                                Content="{Binding ActionName}">
                        </Button>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <ItemsPanelTemplate x:Key="LineItemItemsTemplateWrapPanel">
            <WrapPanel />
        </ItemsPanelTemplate>

        <Style x:Key="TagBorderStyle" TargetType="Border">
            <Setter Property="BorderThickness" Value="1"></Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderBrush" Value="{DynamicResource AccentTextBrush}"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

        <DataTemplate x:Key="LineItemDataTemplateTag" DataType="model:Tag">
            <Border Margin="3" Padding="5"
                    Background="{DynamicResource AccentMidBrush}" 
                    CornerRadius="5" 
                    VerticalAlignment="Center"
                    Tag="{Binding DataContext.TagsPanelViewModel, RelativeSource={RelativeSource AncestorType=serverList:ServerListPageView}}"
                    PreviewMouseDown="UIElement_OnPreviewMouseDown"
                    Cursor="Hand"
                    Style="{StaticResource TagBorderStyle}"
                    >
                <!--right click context menu-->
                <Border.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="{DynamicResource Unpin}" 
                                  Visibility="{Binding IsPinned, Converter={StaticResource ConverterBool2Visible}}"
                                  Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Tag.CmdTagPin}" 
                                  CommandParameter="{Binding}"/>
                        <MenuItem Header="{DynamicResource Pin}" 
                                  Visibility="{Binding IsPinned, Converter={StaticResource ConverterBool2VisibleInv}}"
                                  Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Tag.CmdTagPin}" 
                                  CommandParameter="{Binding}"/>
                        <MenuItem Header="{DynamicResource Rename}" 
                                  Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Tag.CmdTagRename}" 
                                  CommandParameter="{Binding}"/>
                        <MenuItem Header="{DynamicResource Delete}" 
                                  Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Tag.CmdTagDelete}" 
                                  CommandParameter="{Binding}"/>
                        <MenuItem Header="{DynamicResource Connect}" 
                                  Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Tag.CmdTagConnect}" 
                                  CommandParameter="{Binding}"/>
                        <MenuItem Header="{DynamicResource 'Create desktop shortcut'}" 
                                  Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Tag.CmdCreateDesktopShortcut}" 
                                  CommandParameter="{Binding}"/>
                        <MenuItem Header="{DynamicResource 'Connect (New window)'}" 
                                  Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Tag.CmdTagConnectToNewTab}" 
                                  CommandParameter="{Binding}"/>
                    </ContextMenu>
                </Border.ContextMenu>
                <Border.InputBindings>
                    <MouseBinding Gesture="LeftClick" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=serverList:ServerListPageView}, Path=DataContext.CmdTagAddIncluded}" CommandParameter="{Binding}" />
                    <MouseBinding Gesture="Ctrl+LeftClick" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=serverList:ServerListPageView}, Path=DataContext.CmdTagAddIncluded}" CommandParameter="{Binding}" />
                    <MouseBinding Gesture="Alt+LeftClick" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=serverList:ServerListPageView}, Path=DataContext.CmdTagAddExcluded}" CommandParameter="{Binding}" />
                </Border.InputBindings>
                <TextBlock Style="{StaticResource TextBlockBase}" Text="{Binding Name}" Foreground="{DynamicResource AccentTextBrush}"></TextBlock>
            </Border>
        </DataTemplate>


        <Style x:Key="SelectedCheckBoxStyle" TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
            <Setter Property="Visibility" Value="Collapsed"></Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=Grid,Path=IsMouseOver}" Value="True">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                </DataTrigger>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="LineItemSettingMenuStyle" TargetType="Button" BasedOn="{StaticResource ButtonAccentStyle}">
            <Setter Property="Visibility" Value="Hidden"></Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=Grid,Path=IsMouseOver}" Value="True">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=PopupCardSettingMenu, Path=IsOpen}" Value="True">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                    <Setter Property="Background" Value="{StaticResource PrimaryMidBrush}"></Setter>
                    <Setter Property="Foreground" Value="{StaticResource PrimaryTextBrush}"></Setter>
                    <Setter Property="Padding" Value="4"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <Grid Name="Grid" Background="Transparent">
        <Grid Opacity="0.4" Background="{Binding Server.ColorHex,Converter={StaticResource ConverterColorHexString2Brush}, Mode=OneWay}"></Grid>

        <Grid>
            <Grid.Margin>
                <Thickness Right="{StaticResource ScrollBarWidthMouseOver}"></Thickness>
            </Grid.Margin>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Selector"></ColumnDefinition>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Protocol"></ColumnDefinition>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Span"></ColumnDefinition>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Name"></ColumnDefinition>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Tags"></ColumnDefinition>
                <ColumnDefinition Width="Auto" SharedSizeGroup="BriefNote"></ColumnDefinition>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Address"></ColumnDefinition>
            </Grid.ColumnDefinitions>



            <Path Height="14" Width="14" UseLayoutRounding="True" Stretch="Uniform">
                <Path.Style>
                    <Style TargetType="Path">
                        <Setter Property="Data" Value="{StaticResource GeometryCloudDisconnected}"></Setter>
                        <Setter Property="Fill" Value="Red"></Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding DataSource.Status}" Value="{x:Static dao:EnumDatabaseStatus.OK}">
                                <Setter Property="Visibility" Value="Collapsed"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Path.Style>
            </Path>

            <CheckBox Name="CbSelected" 
                      Grid.Column="0" Margin="10 0" 
                      Width="20" Height="20" 
                      HorizontalAlignment="Left" 
                      Style="{StaticResource SelectedCheckBoxStyle}"
                      Click="ItemsCheckBox_OnClick"
                      IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}">
            </CheckBox>

            <!--protocol name-->
            <TextBlock Grid.Column="1" Margin="5 0 10 0" 
                       Style="{StaticResource TextBlockBase}"
                       Text="{Binding Server.ProtocolDisplayName}"
                       FontSize="{DynamicResource GlobalFontSizeSubtitle}" FontWeight="Bold"
                       Opacity="0.6"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Center">
            </TextBlock>


            <!-- setting menu button -->
            <Button Grid.Column="1" 
                    x:Name="BtnSettingMenu"
                    Margin="0 0 10 0"
                    Width="20" Height="20" HorizontalAlignment="Right"
                    Style="{StaticResource LineItemSettingMenuStyle}"
                    Click="BtnSettingMenu_OnClick">
                <Path Name="Symbol" Margin="2"
                      Data="F1 M 20 6.25 L 0 6.25 L 0 5 L 20 5 L 20 6.25 Z M 20 16.25 L 0 16.25 L 0 15 L 20 15 L 20 16.25 Z M 20 11.25 L 0 11.25 L 0 10 L 20 10 L 20 11.25 Z "
                      Fill="{DynamicResource AccentTextBrush}"
                      Stretch="Uniform" />
            </Button>

            <!--icon-->
            <Image Grid.Column="2" 
                   Width="30" HorizontalAlignment="Center" Margin="5 0"
                   Source="{Binding Server.IconImg}" 
                   RenderOptions.BitmapScalingMode="LowQuality"
                   ></Image>

            <!--display name-->
            <TextBlock Grid.Column="3" ToolTip="{Binding DisplayName}" Background="Transparent" 
                       Style="{StaticResource TextBlockBase}"
                       FontSize="{DynamicResource GlobalFontSizeSubtitle}" 
                       Margin="5 0 10 0" 
                       VerticalAlignment="Center" 
                       TextTrimming="CharacterEllipsis" 
                       Text="{Binding Server.DisplayName}"></TextBlock>

            <!--tags-->
            <ItemsControl Grid.Column="4" 
                          Margin="5 0 10 0" 
                          MaxWidth="200" 
                          VerticalAlignment="Center" HorizontalAlignment="Right" 
                          ItemsSource="{Binding Tags}"
                          ItemsPanel="{StaticResource LineItemItemsTemplateWrapPanel}"
                          ItemTemplate="{StaticResource LineItemDataTemplateTag}"
                          >
                <ItemsControl.ToolTip>
                    <Label Content="&#10;" 
                           ContentStringFormat="{DynamicResource tag_tooltips}"/>
                </ItemsControl.ToolTip>
            </ItemsControl>

            <!--brief note -->
            <ContentControl Grid.Column="5" 
                            Margin="0 0 10 0"
                            VerticalAlignment="Center"
                            Content="{Binding HoverNoteDisplayControl}"></ContentControl>

            <!--address-->
            <TextBlock Margin="5 0" MaxWidth="500"
                       Style="{StaticResource TextBlockBase}"
                       Grid.Column="6"
                       Background="Transparent"
                       ToolTip="{Binding Server.SubTitle}"
                       FontSize="{DynamicResource GlobalFontSizeSubtitle}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"
                       Text="{Binding Server.SubTitle}">
            </TextBlock>
        </Grid>


        <Popup Name="PopupCardSettingMenu"
               AllowsTransparency="True"
               PlacementTarget="{Binding ElementName=BtnSettingMenu}" StaysOpen="False">
            <Border Padding="0 0 4 4">
                <Border.Effect>
                    <DropShadowEffect Opacity="0.5" ShadowDepth="3"></DropShadowEffect>
                </Border.Effect>
                <Grid MinWidth="80" Background="{DynamicResource PrimaryDarkBrush}">
                    <ListBox ItemsSource="{Binding Actions}" 
                             Style="{StaticResource LineItemSettingMenuListBoxStyle}"
                             BorderThickness="0"></ListBox>
                </Grid>
            </Border>
        </Popup>

    </Grid>
</UserControl>
