<Window x:Class="ServerManagerTool.PlayerListWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cc="clr-namespace:ServerManagerTool.Common.Converters;assembly=ServerManager.Common"
        xmlns:clib="clr-namespace:ServerManagerTool.Common.Lib;assembly=ServerManager.Common"
        xmlns:com="clr-namespace:ServerManagerTool.Common;assembly=ServerManager.Common"
        xmlns:en="clr-namespace:ServerManagerTool.Enums"
        xmlns:vm="clr-namespace:ServerManagerTool.Lib.ViewModel"
        MinWidth="300" MinHeight="200" Width="300" Height="500" ResizeMode="CanResizeWithGrip" WindowStyle="ToolWindow" 
        SizeChanged="Window_SizeChanged" LocationChanged="Window_LocationChanged" 
        Icon="../Art/favicon.ico" Title="{Binding PlayerListParameters.WindowTitle}"
        FocusManager.FocusedElement="{Binding ElementName=PlayerListFilterStringTextBox}"
        Name="PlayerList">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\Globalization\en-US\en-US.xaml"/>
                <ResourceDictionary Source="..\Styles\Default.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid Background="{StaticResource GradientBackground}">
        <DockPanel x:Name="dockPanel">
            <Border DockPanel.Dock="Top" BorderThickness="1" BorderBrush="LightGray" Margin="2" Background="{StaticResource HeaderBrush}">
                <DockPanel Margin="2,2,2,1" LastChildFill="False">
                    <StackPanel Orientation="Horizontal" DockPanel.Dock="Left">
                        <Menu VerticalAlignment="Center" Margin="1" Background="{StaticResource HeaderBrush}">
                            <MenuItem Header="{DynamicResource RCON_Menu_Players}">
                                <MenuItem Header="{DynamicResource RCON_Menu_Sort}">
                                    <MenuItem Header="{DynamicResource RCON_Menu_SortOnline}"         IsCheckable="True" Command="{Binding SortPlayersCommand}"   CommandParameter="{x:Static en:PlayerSortType.Online}"         IsChecked="{Binding PlayerSorting, Converter={StaticResource EnumConverter}, ConverterParameter=Online}"/>
                                    <MenuItem Header="{DynamicResource RCON_Menu_SortName}"           IsCheckable="True" Command="{Binding SortPlayersCommand}"   CommandParameter="{x:Static en:PlayerSortType.Name}"           IsChecked="{Binding PlayerSorting, Converter={StaticResource EnumConverter}, ConverterParameter=Name}"/>
                                    <MenuItem Header="{DynamicResource RCON_Menu_SortTribe}"          IsCheckable="True" Command="{Binding SortPlayersCommand}"   CommandParameter="{x:Static en:PlayerSortType.Tribe}"          IsChecked="{Binding PlayerSorting, Converter={StaticResource EnumConverter}, ConverterParameter=Tribe}"/>
                                    <MenuItem Header="{DynamicResource RCON_Menu_SortLastOnline}"     IsCheckable="True" Command="{Binding SortPlayersCommand}"   CommandParameter="{x:Static en:PlayerSortType.LastOnline}"     IsChecked="{Binding PlayerSorting, Converter={StaticResource EnumConverter}, ConverterParameter=LastOnline}"/>
                                </MenuItem>
                                <MenuItem Header="{DynamicResource RCON_Menu_Filter}">
                                    <MenuItem Header="{DynamicResource RCON_Menu_FilterOnline}"       IsCheckable="True" Command="{Binding FilterPlayersCommand}" CommandParameter="{x:Static en:PlayerFilterType.Online}"       IsChecked="{Binding PlayerFiltering, Converter={StaticResource EnumFlagsConverter}, ConverterParameter=Online, Mode=OneWay}"/>
                                    <MenuItem Header="{DynamicResource RCON_Menu_FilterOffline}"      IsCheckable="True" Command="{Binding FilterPlayersCommand}" CommandParameter="{x:Static en:PlayerFilterType.Offline}"      IsChecked="{Binding PlayerFiltering, Converter={StaticResource EnumFlagsConverter}, ConverterParameter=Offline, Mode=OneWay}"/>
                                    <MenuItem Header="{DynamicResource RCON_Menu_FilterWhitelisted}"  IsCheckable="True" Command="{Binding FilterPlayersCommand}" CommandParameter="{x:Static en:PlayerFilterType.Whitelisted}"  IsChecked="{Binding PlayerFiltering, Converter={StaticResource EnumFlagsConverter}, ConverterParameter=Whitelisted, Mode=OneWay}"/>
                                    <MenuItem Header="{DynamicResource RCON_Menu_FilterInvalid}"      IsCheckable="True" Command="{Binding FilterPlayersCommand}" CommandParameter="{x:Static en:PlayerFilterType.Invalid}"      IsChecked="{Binding PlayerFiltering, Converter={StaticResource EnumFlagsConverter}, ConverterParameter=Invalid, Mode=OneWay}"/>
                                </MenuItem>
                            </MenuItem>
                        </Menu>
                    </StackPanel>

                    <StackPanel Orientation="Horizontal" DockPanel.Dock="Right">
                        <Label Content="{DynamicResource RCON_PlayersLabel}" Margin="5,0,0,0"/>
                        <Label Content="{Binding ServerPlayers.CountOnlinePlayers, FallbackValue=0}"/>
                        <Label Content="{DynamicResource RCON_PlayersSeparatorLabel}" Name="PlayerCountSeparator"/>
                        <Label Content="{Binding PlayerListParameters.MaxPlayers, FallbackValue=0}" Name="MaxPlayerLabel"/>
                    </StackPanel>
                </DockPanel>
            </Border>

            <DockPanel DockPanel.Dock="Right" MinWidth="200" Margin="1,1,2,1" DataContext="{Binding}">
                <DockPanel DockPanel.Dock="Top">
                    <Label DockPanel.Dock="Left" Content="{DynamicResource General_FilterLabel}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    <Button DockPanel.Dock="Right" Margin="5,0,5,0" Width="22" Height="22" HorizontalAlignment="Right" VerticalAlignment="Center" Click="FilterPlayerList_Click" ToolTip="{DynamicResource General_FilterButtonTooltip}">
                        <Image Source="{com:Icon Path=/ConanServerManager;component/Art/Filter.ico,Size=32}"/>
                    </Button>
                    <TextBox x:Name="PlayerListFilterStringTextBox" DockPanel.Dock="Left" Text="{Binding PlayerListFilterString, Mode=TwoWay}" BorderBrush="LightGray" VerticalAlignment="Center" VerticalContentAlignment="Center" IsTabStop="True" Margin="0" Padding="2"/>
                </DockPanel>

                <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                    <Label Content="{DynamicResource PlayerList_ProfileCountLabel}" HorizontalAlignment="Center"/>
                    <Label Content="{Binding ServerPlayers.Players.Count, FallbackValue=0}" HorizontalAlignment="Center"/>
                    <Label Content="{DynamicResource PlayerList_ProfileInvalidCountLabel}" HorizontalAlignment="Center">
                        <Label.Style>
                            <Style BasedOn="{StaticResource {x:Type Label}}" TargetType="{x:Type Label}">
                                <Setter Property="Foreground" Value="Black"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ServerPlayers.CountInvalidPlayers, FallbackValue=0, Converter={cc:GreaterThanIntValueConverter 0}}" Value="true">
                                        <Setter Property="Foreground" Value="Red"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Label.Style>
                    </Label>
                    <Label Content="{Binding ServerPlayers.CountInvalidPlayers, FallbackValue=0}" HorizontalAlignment="Center">
                        <Label.Style>
                            <Style BasedOn="{StaticResource {x:Type Label}}" TargetType="{x:Type Label}">
                                <Setter Property="Foreground" Value="Black"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ServerPlayers.CountInvalidPlayers, FallbackValue=0, Converter={cc:GreaterThanIntValueConverter 0}}" Value="true">
                                        <Setter Property="Foreground" Value="Red"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Label.Style>
                    </Label>
                </StackPanel>

                <ListBox DockPanel.Dock="Top" ItemsSource="{Binding ServerPlayers.Players}" BorderBrush="LightGray" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Padding="-3,0,-3,0">
                    <ListBox.Resources>
                        <clib:BindingProxy x:Key="proxy" Data="{Binding ElementName=PlayerList}"/>
                    </ListBox.Resources>

                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="ContextMenu">
                                <Setter.Value>
                                    <ContextMenu>
                                        <MenuItem Header="{DynamicResource PlayerList_Menu_ViewProfile}"      Command="{Binding Source={StaticResource proxy}, Path=Data.ViewPlayerProfileCommand}" CommandParameter="{Binding}" />
                                        <MenuItem Header="{DynamicResource PlayerList_Menu_ViewTribe}"        Command="{Binding Source={StaticResource proxy}, Path=Data.ViewPlayerTribeCommand}" CommandParameter="{Binding}"/>
                                    </ContextMenu>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>

                    <ListBox.ItemTemplate>
                        <DataTemplate DataType="{x:Type vm:PlayerInfo}">
                            <Border BorderThickness="2" Padding="1" Style="{DynamicResource PlayerStatus}">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>

                                    <StackPanel Grid.Column="0" Margin="5,0,5,0" HorizontalAlignment="Stretch">
                                        <StackPanel Orientation="Horizontal" ToolTip="{Binding PlayerId}">
                                            <Label Content="{Binding CharacterName}" Padding="0,-1,0,-1"/>
                                            <TextBlock Text="{Binding PlayerData.Level, StringFormat=({0})}" Margin="5,0,0,0" />
                                        </StackPanel>
                                        <Label Padding="0,-1,0,-1" Style="{DynamicResource GuildName}"/>
                                    </StackPanel>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </DockPanel>
        </DockPanel>
    </Grid>
</Window>
