<UserControl x:Class="NETworkManager.Views.PingView"
        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"
        xmlns:Control="clr-namespace:NETworkManager.Controls"
        xmlns:Validator="clr-namespace:NETworkManager.Validators"
        xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns:Converter="clr-namespace:NETworkManager.Converters"
        xmlns:IconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        Loaded="UserControl_Loaded"
        mc:Ignorable="d">
    <UserControl.Resources>
        <Converter:BooleanReverseConverter x:Key="BooleanReverseConverter" />
        <Converter:BooleanReverseToVisibilityConverter x:Key="BooleanReverseToVisibilityConverter" />
        <Converter:IntToStringConverter x:Key="IntToStringConverter" />
        <Converter:IPStatusToStringConverter x:Key="IPStatusToStringConverter" />
        <Converter:NullableDateTimeToStringConverter x:Key="NullableDateTimeToStringConverter" />
        <Converter:PercentConverter x:Key="PercentConverter" />
        <Converter:PingTimeToStringConverter x:Key="PingTimeToStringConverter" />
    </UserControl.Resources>
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Style="{StaticResource HeaderTextBlock}" Text="{DynamicResource String_Header_Destination}" />
        <Grid Grid.Row="1" Margin="0,0,0,20">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{DynamicResource String_Host}" Style="{DynamicResource DefaultTextBlock}" VerticalAlignment="Center" />
                <ComboBox Grid.Column="2" x:Name="txtHost" ItemsSource="{Binding HostHistoryView}" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_ExampleHostnameOrIPAddress}" IsEnabled="{Binding IsPingRunning, Converter={StaticResource BooleanReverseConverter}}" Style="{StaticResource HistoryComboBox}">
                    <ComboBox.Text>
                        <Binding Path="Host" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <Validator:EmptyValidator ValidatesOnTargetUpdated="True" />
                            </Binding.ValidationRules>
                        </Binding>
                    </ComboBox.Text>
                </ComboBox>
                <Button Grid.Column="4" Command="{Binding PingCommand}" IsDefault="{Binding IsPingRunning,Converter={StaticResource BooleanReverseConverter}}" IsCancel="{Binding IsPingRunning}" HorizontalAlignment="Right">
                    <Button.Resources>
                        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ImageWithTextButton}">
                            <Setter Property="IsEnabled" Value="True" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=(Validation.HasError), ElementName=txtHost}" Value="True" >
                                    <Setter Property="IsEnabled" Value="False" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding CancelPing}" Value="True">
                                    <Setter Property="IsEnabled" Value="False" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Resources>
                    <Button.Content>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Rectangle Width="20" Height="20" Margin="10,5,0,5">
                                <Rectangle.Resources>
                                    <VisualBrush x:Key="VisualControlPlay" Stretch="Uniform" Visual="{IconPacks:Material Kind=Play}" />
                                    <VisualBrush x:Key="VisualClose" Stretch="Uniform" Visual="{IconPacks:Material Kind=Close}" />
                                </Rectangle.Resources>
                                <Rectangle.Style>
                                    <Style TargetType="{x:Type Rectangle}">
                                        <Setter Property="OpacityMask" Value="{StaticResource VisualControlPlay}" />
                                        <Setter Property="Fill" Value="{DynamicResource GrayBrush3}" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsPingRunning}" Value="True">
                                                <Setter Property="OpacityMask" Value="{StaticResource VisualClose}" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Rectangle.Style>
                            </Rectangle>
                            <TextBlock Grid.Column="1" FontSize="14" Margin="10,5" TextAlignment="Center">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Text" Value="{DynamicResource String_Button_Ping}"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsPingRunning}" Value="True" >
                                                <Setter Property="Text" Value="{DynamicResource String_Button_Cancel}"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </Grid>
                    </Button.Content>
                </Button>
            </Grid>
            <Controls:MetroProgressBar Grid.Row="1" Margin="0,10,0,0" Foreground="{DynamicResource AccentColorBrush}" IsIndeterminate="True">
                <Controls:MetroProgressBar.Style>
                    <Style TargetType="{x:Type  Controls:MetroProgressBar}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsPingRunning}" Value="False">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Controls:MetroProgressBar.Style>
            </Controls:MetroProgressBar>
            <TextBlock Grid.Row="2" Foreground="{DynamicResource AccentColorBrush}" Text="{Binding StatusMessage}" Visibility="{Binding DisplayStatusMessage, Converter={StaticResource BooleanToVisibilityConverter}}" Style="{DynamicResource DefaultTextBlock}" Margin="0,10,0,0" />
        </Grid>
        <TextBlock Grid.Row="2" Text="{DynamicResource String_Header_Status}" Style="{StaticResource HeaderTextBlock}" />
        <Control:ScrollingDataGrid Grid.Row="3" ItemsSource="{Binding PingResultView}">
            <Control:ScrollingDataGrid.Resources>
                <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource ScrollingDataGridScrollBar}" />
            </Control:ScrollingDataGrid.Resources>
            <Control:ScrollingDataGrid.Columns>
                <DataGridTextColumn Header="Timestamp" Binding="{Binding Timestamp}" MinWidth="180" />
                <DataGridTextColumn Header="{DynamicResource String_IPAddress}" Binding="{Binding IPAddress}" MinWidth="150" />
                <DataGridTextColumn Header="{DynamicResource String_Bytes}" Binding="{Binding Bytes, Converter={StaticResource IntToStringConverter}}" MinWidth="80" />
                <DataGridTextColumn Header="{DynamicResource String_Time}" MinWidth="100">
                    <DataGridTextColumn.Binding>
                        <MultiBinding Converter="{StaticResource PingTimeToStringConverter}">
                            <Binding Path="Status" />
                            <Binding Path="Time" />
                        </MultiBinding>
                    </DataGridTextColumn.Binding>
                </DataGridTextColumn>
                <DataGridTextColumn Header="{DynamicResource String_TTL}" Binding="{Binding TTL, Converter={StaticResource IntToStringConverter}}" MinWidth="80"/>
                <DataGridTextColumn Header="{DynamicResource String_Status}" Binding="{Binding Status, Converter={StaticResource IPStatusToStringConverter}}" MinWidth="150" Width="*" />
            </Control:ScrollingDataGrid.Columns>
        </Control:ScrollingDataGrid>
        <Expander Grid.Row="4" IsExpanded="{Binding ExpandStatistics}" Visibility="{Binding ShowStatistics, Converter={StaticResource BooleanToVisibilityConverter}}" Style="{StaticResource HeaderExpander}" Margin="0,20,0,0">
            <Expander.Header>
                <TextBlock Text="{DynamicResource String_Header_Statistics}" Style="{StaticResource HeaderTextBlock}" Margin="0" />
            </Expander.Header>
            <Grid Margin="0,10,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.Resources>
                    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource CenterTextBlock}" />
                    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextBlockAsTextBox}">
                        <Setter Property="VerticalAlignment" Value="Center" />
                        <Setter Property="ContextMenu" Value="{StaticResource CopyContextMenu}" />
                        <Setter Property="TextWrapping" Value="NoWrap" />
                    </Style>
                </Grid.Resources>
                <TextBlock Grid.Column="0" Grid.Row="0" Text="{DynamicResource String_StartTime}" />
                <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding StartTime, Converter={StaticResource NullableDateTimeToStringConverter}}" />
                <TextBlock Grid.Column="0" Grid.Row="1" Text="{DynamicResource String_Duration}" />
                <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Duration, StringFormat={}{0:hh}h {0:mm}m {0:ss}s {0:ff}ms}" />
                <TextBlock Grid.Column="0" Grid.Row="2" Text="{DynamicResource String_EndTime}" />
                <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding EndTime, Converter={StaticResource NullableDateTimeToStringConverter}}" />
                <TextBlock Grid.Column="3" Grid.Row="0" Text="{DynamicResource String_PacketsTransmitted}" />
                <TextBox Grid.Column="4" Grid.Row="0" Text="{Binding PingsTransmitted}" />
                <TextBlock Grid.Column="3" Grid.Row="1" Text="{DynamicResource String_ReceivedLost}" />
                <TextBox Grid.Column="4" Grid.Row="1">
                    <TextBox.Text>
                        <MultiBinding StringFormat="{}{0} / {1}">
                            <Binding Path="PingsReceived" />
                            <Binding Path="PingsLost" />
                        </MultiBinding>
                    </TextBox.Text>
                </TextBox>
                <TextBlock Grid.Column="3" Grid.Row="2" Text="{DynamicResource String_PacketLoss}" />
                <TextBox Grid.Column="4" Grid.Row="2">
                    <TextBox.Text>
                        <MultiBinding Converter="{StaticResource PercentConverter}" StringFormat="{}{0} %">
                            <Binding Path="PingsTransmitted" />
                            <Binding Path="PingsLost" />
                        </MultiBinding>
                    </TextBox.Text>
                </TextBox>
                <TextBlock Grid.Column="6" Grid.Row="0" Text="{DynamicResource String_AverageTime}" />
                <TextBox Grid.Column="7" Grid.Row="0" Text="{Binding AverageTime, StringFormat={}{0} ms}" />
                <TextBlock Grid.Column="6" Grid.Row="1" Text="{DynamicResource String_Minimum}" />
                <TextBox Grid.Column="7" Grid.Row="1" Text="{Binding MinimumTime, StringFormat={}{0} ms}" />
                <TextBlock Grid.Column="6" Grid.Row="2" Text="{DynamicResource String_Maximum}" />
                <TextBox Grid.Column="7" Grid.Row="2" Text="{Binding MaximumTime, StringFormat={}{0} ms}" />
            </Grid>
        </Expander>
    </Grid>
</UserControl>
