<Window x:Class="ServerManagerTool.RconWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sm="clr-namespace:ServerManagerTool"
        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"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Width="1024" Height="768" MinWidth="640" MinHeight="480" ResizeMode="CanResizeWithGrip" 
        SizeChanged="RCON_SizeChanged" LocationChanged="RCON_LocationChanged"
        Name="RCON" Icon="../Art/favicon.ico" Title="{Binding RconParameters.WindowTitle}">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\Globalization\en-US\en-US.xaml"/>
                <ResourceDictionary Source="..\Styles\Default.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <sm:ScrollToBottomAction x:Key="ScrollToBottomAction" />
            
            <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="InputModes">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="sm:InputMode" />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <Grid x:Name="dockPanel">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition />
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition MinWidth="200" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="200" MinWidth="200" x:Name="playerListColumn" />
            </Grid.ColumnDefinitions>

            <Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" 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_File}" VerticalAlignment="Stretch">
                                <MenuItem Header="{DynamicResource RCON_Menu_ViewLogs}"    Command="{Binding ViewLogsCommand}"/>
                                <MenuItem Header="{DynamicResource RCON_Menu_ClearLogs}">
                                    <MenuItem Header="{DynamicResource RCON_Menu_Confirm}" Command="{Binding ClearLogsCommand}"/>
                                </MenuItem>
                            </MenuItem>
                        </Menu>

                        <Menu VerticalAlignment="Center" Margin="1" Background="{StaticResource HeaderBrush}">
                            <MenuItem Header="{DynamicResource RCON_Menu_Console}" VerticalAlignment="Stretch">
                                <MenuItem Header="{DynamicResource RCON_Menu_AutoScroll}" IsCheckable="True" IsChecked="{Binding ScrollOnNewInput}"/>
                            </MenuItem>
                        </Menu>

                        <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 ServerRcon.CountOnlinePlayers, FallbackValue=0}"/>
                        <Label Content="{DynamicResource RCON_PlayersSeparatorLabel}" Name="PlayerCountSeparator"/>
                        <Label Content="{Binding RconParameters.MaxPlayers, FallbackValue=0}" Name="MaxPlayerLabel"/>
                    </StackPanel>
                </DockPanel>
            </Border>

            <RichTextBox Grid.Row="1" Grid.Column="0" BorderBrush="LightGray" VerticalAlignment="Stretch" VerticalScrollBarVisibility="Visible"  Margin="2,1,0,1" HorizontalScrollBarVisibility="Auto" IsReadOnlyCaretVisible="True" IsReadOnly="True" IsTabStop="False">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="TextChanged" >
                        <sm:ScrollToBottomAction IsEnabled="{Binding ScrollOnNewInput}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <RichTextBox.Resources>
                    <Style TargetType="{x:Type Paragraph}">
                        <Setter Property="Margin" Value="0"/>
                    </Style>
                    <Style TargetType="{x:Type sm:RconOutput_Command}">
                        <Setter Property="FontWeight" Value="Bold"/>
                    </Style>
                    <Style TargetType="{x:Type sm:RconOutput_NoResponse}">
                        <Setter Property="FontStyle" Value="Italic"/>
                    </Style>
                    <Style TargetType="{x:Type sm:RconOutput_CommandOutput}">
                        <Setter Property="FontStyle" Value="Normal"/>
                        <Setter Property="FontWeight" Value="Normal"/>
                    </Style>
                    <Style TargetType="{x:Type sm:RconOutput_CommandTime}">
                        <Setter Property="FontStyle" Value="Normal"/>
                        <Setter Property="FontWeight" Value="Normal"/>
                    </Style>
                    <Style TargetType="{x:Type sm:RconOutput_ConnectionChanged}">
                        <Setter Property="FontStyle" Value="Italic"/>
                        <Setter Property="FontWeight" Value="Normal"/>
                    </Style>
                    <Style TargetType="{x:Type sm:RconOutput_Broadcast}">
                        <Setter Property="FontStyle" Value="Normal"/>
                        <Setter Property="FontWeight" Value="Bold"/>
                    </Style>
                    <Style TargetType="{x:Type sm:RconOutput_Comment}">
                        <Setter Property="FontStyle" Value="Italic"/>
                        <Setter Property="FontWeight" Value="Normal"/>
                    </Style>
                </RichTextBox.Resources>
                <FlowDocument Name="ConsoleContent"/>
            </RichTextBox>

            <GridSplitter Grid.Row="1" Grid.Column="1" Width="5" ShowsPreview="True" HorizontalAlignment="Center" VerticalAlignment="Stretch" Opacity="0"/>

            <DockPanel x:Name="playerListPanel" Grid.Row="1" Grid.Column="2" Margin="0,1,2,1" DataContext="{Binding}" SizeChanged="PlayerList_SizeChanged">
                <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 DockPanel.Dock="Left" Text="{Binding PlayerFilterString, Mode=TwoWay}" BorderBrush="LightGray" VerticalAlignment="Center" VerticalContentAlignment="Center" IsTabStop="True" Margin="0" Padding="2"/>
                </DockPanel>

                <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                    <Label Content="{DynamicResource RCON_ProfileCountLabel}" HorizontalAlignment="Center"/>
                    <Label Content="{Binding ServerRcon.CountPlayers, FallbackValue=0}" HorizontalAlignment="Center"/>
                    <Label Content="{DynamicResource RCON_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 ServerRcon.CountInvalidPlayers, FallbackValue=0, Converter={cc:GreaterThanIntValueConverter 0}}" Value="true">
                                        <Setter Property="Foreground" Value="Red"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Label.Style>
                    </Label>
                    <Label Content="{Binding ServerRcon.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 ServerRcon.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 ServerRcon.Players}" BorderBrush="LightGray" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
                    <ListBox.Resources>
                        <clib:BindingProxy x:Key="proxy" Data="{Binding ElementName=RCON}"/>
                    </ListBox.Resources>
                    
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="ContextMenu">
                                <Setter.Value>
                                    <ContextMenu>
                                        <MenuItem Header="{DynamicResource RCON_Menu_ViewProfile}"                                       Command="{Binding Source={StaticResource proxy}, Path=Data.ViewPlayerProfileCommand}" CommandParameter="{Binding}" />
                                        <MenuItem Header="{DynamicResource RCON_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="1*"/>
                                    </Grid.ColumnDefinitions>

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

            <Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Margin="2,1,2,2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <Label Grid.Column="0" Content="{DynamicResource RCON_ModeLabel}"/>
                <ComboBox Name="InputModesComboBox" Grid.Column="1" ItemsSource="{Binding Source={StaticResource InputModes}}" SelectedItem="{Binding CurrentInputMode, Mode=TwoWay}" Margin="0,0,2,0"/>
                <TextBox Name="ConsoleInput" TabIndex="0" Grid.Column="2" KeyUp="ConsoleInput_KeyUp" BorderBrush="LightGray" VerticalContentAlignment="Center" IsTabStop="True"/>
            </Grid>
        </Grid>

        <!-- It's important that this is in the end of the XAML as it needs to be on top of everything else! -->
        <Grid x:Name="inputBox" Visibility="Collapsed" DockPanel.Dock="Top" >
            <Grid Background="Black" Opacity="0.5"/>
            <Border MinWidth="250" Background="Orange" BorderBrush="Black" BorderThickness="1" CornerRadius="0,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center">
                <StackPanel>
                    <TextBlock x:Name="inputTitle" Margin="5" FontWeight="Bold" />
                    <TextBox x:Name="inputTextBox" MinWidth="150" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="5,0,5,0" MaxLength="200"/>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                        <Button x:Name="button1" Margin="5" Content="{DynamicResource RCON_Button_OK}"     Background="{x:Null}" Command="{Binding Button1Command}" CommandParameter="{Binding}"/>
                        <Button x:Name="button2" Margin="5" Content="{DynamicResource RCON_Button_Cancel}" Background="{x:Null}" Command="{Binding Button2Command}" CommandParameter="{Binding}"/>
                    </StackPanel>
                </StackPanel>
            </Border>
        </Grid>
    </Grid>
</Window>
