<UserControl x:Class="Pulsar.Server.Controls.Wpf.ClientsListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800" Background="Transparent">
    <UserControl.Resources>
        <controlsWpf:FavoriteToBrushConverter x:Key="FavoriteToBrushConverter"
                                             xmlns:controlsWpf="clr-namespace:Pulsar.Server.Controls.Wpf" />

        <SolidColorBrush x:Key="RowBackgroundBrush" Color="#1E1E1E" />
        <SolidColorBrush x:Key="RowAlternateBackgroundBrush" Color="#232323" />
        <SolidColorBrush x:Key="RowHoverBrush" Color="#2B2B2B" />
        <SolidColorBrush x:Key="RowSelectedBrush" Color="#3A3A3A" />
        <SolidColorBrush x:Key="RowSelectedInactiveBrush" Color="#333333" />
        <SolidColorBrush x:Key="RowForegroundBrush" Color="#FFFFFF" />
        <SolidColorBrush x:Key="RowSelectedForegroundBrush" Color="#FFFFFF" />
        <SolidColorBrush x:Key="HeaderBackgroundBrush" Color="#2A2A2A" />
        <SolidColorBrush x:Key="HeaderForegroundBrush" Color="#FFFFFF" />
        <SolidColorBrush x:Key="GridBackgroundBrush" Color="#141414" />
        <SolidColorBrush x:Key="ScrollBarTrackBrush" Color="#1E1E1E" />
        <SolidColorBrush x:Key="ScrollBarThumbBrush" Color="#444444" />
        <SolidColorBrush x:Key="ScrollBarThumbHoverBrush" Color="#5A5A5A" />
        <SolidColorBrush x:Key="ScrollBarThumbPressedBrush" Color="#737373" />

        <Style x:Key="ClientsRowStyle" TargetType="DataGridRow">
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Background" Value="{DynamicResource RowBackgroundBrush}" />
            <Setter Property="Foreground" Value="{DynamicResource RowForegroundBrush}" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <EventSetter Event="PreviewMouseRightButtonDown" Handler="DataGridRow_OnPreviewMouseRightButtonDown" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="{DynamicResource RowHoverBrush}" />
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{DynamicResource RowSelectedBrush}" />
                    <Setter Property="Foreground" Value="{DynamicResource RowSelectedForegroundBrush}" />
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected" Value="True" />
                        <Condition Property="IsKeyboardFocusWithin" Value="False" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" Value="{DynamicResource RowSelectedInactiveBrush}" />
                </MultiTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="StatusTextStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="{DynamicResource RowForegroundBrush}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="Connected">
                    <Setter Property="Foreground" Value="#32CD32" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="SlimScrollBarThumbStyle" TargetType="Thumb">
            <Setter Property="Background" Value="{DynamicResource ScrollBarThumbBrush}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Thumb">
                        <Border Background="{TemplateBinding Background}" CornerRadius="4" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="{DynamicResource ScrollBarThumbHoverBrush}" />
                </Trigger>
                <Trigger Property="IsDragging" Value="True">
                    <Setter Property="Background" Value="{DynamicResource ScrollBarThumbPressedBrush}" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <ControlTemplate x:Key="SlimVerticalScrollBarTemplate" TargetType="ScrollBar">
            <Grid Background="{TemplateBinding Background}" Width="{TemplateBinding Width}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="0" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="0" />
                </Grid.RowDefinitions>
                <Track x:Name="PART_Track"
                       Grid.Row="1"
                       IsDirectionReversed="true"
                       Orientation="Vertical"
                       Maximum="{TemplateBinding Maximum}"
                       Minimum="{TemplateBinding Minimum}"
                       Value="{TemplateBinding Value}"
                       ViewportSize="{TemplateBinding ViewportSize}">
                    <Track.Thumb>
                        <Thumb Style="{StaticResource SlimScrollBarThumbStyle}" />
                    </Track.Thumb>
                </Track>
            </Grid>
        </ControlTemplate>

        <ControlTemplate x:Key="SlimHorizontalScrollBarTemplate" TargetType="ScrollBar">
            <Grid Background="{TemplateBinding Background}" Height="{TemplateBinding Height}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="0" />
                </Grid.ColumnDefinitions>
                <Track x:Name="PART_Track"
                       Grid.Column="1"
                       Orientation="Horizontal"
                       Maximum="{TemplateBinding Maximum}"
                       Minimum="{TemplateBinding Minimum}"
                       Value="{TemplateBinding Value}"
                       ViewportSize="{TemplateBinding ViewportSize}">
                    <Track.Thumb>
                        <Thumb Style="{StaticResource SlimScrollBarThumbStyle}" />
                    </Track.Thumb>
                </Track>
            </Grid>
        </ControlTemplate>

        <Style x:Key="SlimScrollBarStyle" TargetType="ScrollBar">
            <Setter Property="Background" Value="{DynamicResource ScrollBarTrackBrush}" />
            <Setter Property="Width" Value="10" />
            <Setter Property="Template" Value="{StaticResource SlimVerticalScrollBarTemplate}" />
            <Style.Triggers>
                <Trigger Property="Orientation" Value="Horizontal">
                    <Setter Property="Height" Value="10" />
                    <Setter Property="Width" Value="Auto" />
                    <Setter Property="Template" Value="{StaticResource SlimHorizontalScrollBarTemplate}" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="ClientsCellStyle" TargetType="DataGridCell">
            <Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource AncestorType=DataGridRow}}" />
            <Setter Property="Foreground" Value="{DynamicResource RowForegroundBrush}" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="{DynamicResource RowSelectedForegroundBrush}" />
                </Trigger>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="Foreground" Value="{DynamicResource RowSelectedForegroundBrush}" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Foreground" Value="{DynamicResource HeaderForegroundBrush}" />
            <Setter Property="Background" Value="{DynamicResource HeaderBackgroundBrush}" />
        </Style>

        <Style TargetType="DataGrid">
            <Setter Property="RowBackground" Value="{DynamicResource RowBackgroundBrush}" />
            <Setter Property="AlternatingRowBackground" Value="{DynamicResource RowAlternateBackgroundBrush}" />
            <Setter Property="Foreground" Value="{DynamicResource RowForegroundBrush}" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <DataGrid x:Name="ClientsGrid"
                  AutoGenerateColumns="False"
                  HeadersVisibility="Column"
                  IsReadOnly="True"
                  SelectionMode="Extended"
                  SelectionUnit="FullRow"
                  EnableRowVirtualization="True"
                  EnableColumnVirtualization="True"
                  GridLinesVisibility="None"
                  ScrollViewer.CanContentScroll="True"
                  ItemsSource="{Binding ClientsView}"
                  SelectionChanged="ClientsGrid_OnSelectionChanged"
                  MouseDoubleClick="ClientsGrid_OnMouseDoubleClick"
                  BorderThickness="0"
                  CanUserResizeRows="False"
                  AlternationCount="2"
                  Background="{DynamicResource GridBackgroundBrush}"
                  RowStyle="{StaticResource ClientsRowStyle}"
                  CellStyle="{StaticResource ClientsCellStyle}">
            <DataGrid.Resources>
                <Style TargetType="ScrollBar" BasedOn="{StaticResource SlimScrollBarStyle}" />
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="IP" Width="180">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <Image Source="{Binding FlagImage}"
                                       Width="28"
                                       Height="20"
                                       VerticalAlignment="Center"
                                       Stretch="Uniform"
                                       RenderOptions.BitmapScalingMode="Fant">
                                    <Image.Style>
                                        <Style TargetType="Image">
                                            <Setter Property="Visibility" Value="Visible" />
                                            <Style.Triggers>
                                                <Trigger Property="Source" Value="{x:Null}">
                                                    <Setter Property="Visibility" Value="Collapsed" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Image.Style>
                                </Image>
                                <TextBlock Text="{Binding Ip}" VerticalAlignment="Center">
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="Margin" Value="6,0,0,0" />
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding FlagImage}" Value="{x:Null}">
                                                    <Setter Property="Margin" Value="0" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding Nickname}" Header="Nickname" Width="120" />
                <DataGridTextColumn Binding="{Binding Tag}" Header="Tag" Width="80" />
                <DataGridTextColumn Binding="{Binding UserAtPc}" Header="User@PC" Width="150" />
                <DataGridTemplateColumn Header="Version" Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Version}" Foreground="{Binding VersionBrush}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Status" Width="120">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Status}" Style="{StaticResource StatusTextStyle}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding CurrentWindow}" Header="Current Window" Width="200" />
                <DataGridTextColumn Binding="{Binding UserStatus}" Header="User Status" Width="120" />
                <DataGridTextColumn Binding="{Binding CountryWithCode}" Header="Country" Width="160" />
                <DataGridTextColumn Binding="{Binding OperatingSystem}" Header="OS" Width="180" />
                <DataGridTextColumn Binding="{Binding AccountType}" Header="Account Type" Width="120" />
                <DataGridTemplateColumn Width="40" Header="★">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ToggleButton HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Content="★"
                                          FontSize="14"
                                          Background="Transparent"
                                          BorderThickness="0"
                                          Foreground="{Binding IsFavorite, Converter={StaticResource FavoriteToBrushConverter}}"
                                          Command="{Binding DataContext.ToggleFavoriteCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                          CommandParameter="{Binding}"
                                          IsChecked="{Binding IsFavorite, Mode=TwoWay}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>
