<UserControl x:Class="NETworkManager.Views.PortScannerView"
        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:mah="http://metro.mahapps.com/winfx/xaml/controls"        
        xmlns:IconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
        xmlns:Validator="clr-namespace:NETworkManager.Validators"
        xmlns:Converter="clr-namespace:NETworkManager.Converters"
        xmlns:WpfHelper="clr-namespace:NETworkManager.WpfHelper"
        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:IPAddressToStringConverter x:Key="IPAddressToStringConverter" />
        <Converter:NullableDateTimeToStringConverter x:Key="NullableDateTimeToStringConverter" />
        <Converter:NullOrEmptyToBoolConverter x:Key="NullOrEmptyToBoolConverter" />
        <Converter:NullOrEmptyToStringConverter x:Key="NullOrEmptyToStringConverter" />
        <Converter:PortStatusToStringConverter x:Key="PortStatusToStringConverter" />
    </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_Scan}" />
        <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" Margin="0,0,0,10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="10" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBlock Grid.Column="0" Grid.Row="0" Text="{DynamicResource String_Host}" Style="{StaticResource CenterTextBlock}" />
                <ComboBox Grid.Column="2" Grid.Row="0" x:Name="cbHost" Height="{Binding ElementName=cbPort, Path=ActualHeight}" ItemsSource="{Binding HostHistoryView}" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_ExampleHostnameAndOrIPAddress}" IsEnabled="{Binding IsScanRunning, Converter={StaticResource BooleanReverseConverter}}" Style="{StaticResource HistoryComboBox}">
                    <ComboBox.Text>
                        <Binding Path="Host" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <Validator:EmptyValidator ValidatesOnTargetUpdated="True" />
                                <Validator:MultipleHostsValidator ValidatesOnTargetUpdated="True" />
                            </Binding.ValidationRules>
                        </Binding>
                    </ComboBox.Text>
                </ComboBox>
                <TextBlock Grid.Column="0" Grid.Row="2" Text="{DynamicResource String_Ports}" Style="{StaticResource CenterTextBlock}" />
                <ComboBox Grid.Column="2" Grid.Row="2" x:Name="cbPort" ItemsSource="{Binding PortHistoryView}" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_ExamplePortScanRange}" IsEnabled="{Binding IsScanRunning, Converter={StaticResource BooleanReverseConverter}}" Style="{StaticResource HistoryComboBox}">
                    <ComboBox.Text>
                        <Binding Path="Port" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <Validator:EmptyValidator ValidatesOnTargetUpdated="True" />
                                <Validator:PortRangeValidator ValidatesOnTargetUpdated="True" />
                            </Binding.ValidationRules>
                        </Binding>
                    </ComboBox.Text>
                </ComboBox>
                <Button Grid.Column="4" Grid.Row="2" Command="{Binding ScanCommand}" IsDefault="{Binding IsScanRunning, Converter={StaticResource BooleanReverseConverter}}" IsCancel="{Binding IsScanRunning}" HorizontalAlignment="Right">
                    <Button.Resources>
                        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ImageWithTextButton}">
                            <Setter Property="IsEnabled" Value="True" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=cbHost, Path=(Validation.HasError)}" Value="True">
                                    <Setter Property="IsEnabled" Value="False" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=cbPort, Path=(Validation.HasError)}" Value="True" >
                                    <Setter Property="IsEnabled" Value="False" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding CancelScan}" 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 IsScanRunning}" Value="True">
                                                <Setter Property="OpacityMask" Value="{StaticResource VisualClose}" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Rectangle.Style>
                            </Rectangle>
                            <TextBlock Grid.Column="1" Margin="10,5" TextAlignment="Center">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Text" Value="{DynamicResource String_Button_Scan}"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsScanRunning}" Value="True" >
                                                <Setter Property="Text" Value="{DynamicResource String_Button_Cancel}"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </Grid>
                    </Button.Content>
                </Button>
            </Grid>
            <StackPanel Grid.Row="1" Margin="0,10,0,0">
                <StackPanel.Style>
                    <Style TargetType="{x:Type StackPanel}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsScanRunning}" Value="False">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </StackPanel.Style>
                <Controls:MetroProgressBar Maximum="{Binding PortsToScan}" Foreground="{DynamicResource AccentColorBrush}" Value="{Binding PortsScanned}">
                    <Controls:MetroProgressBar.Style>
                        <Style TargetType="{x:Type Controls:MetroProgressBar}" >
                            <Setter Property="IsIndeterminate" Value="False" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding PreparingScan}" Value="True">
                                    <Setter Property="IsIndeterminate" Value="True" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding CancelScan}" Value="True">
                                    <Setter Property="IsIndeterminate" Value="True" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Controls:MetroProgressBar.Style>
                </Controls:MetroProgressBar>
                <TextBlock HorizontalAlignment="Center">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DefaultTextBlock}">
                            <Setter Property="Visibility" Value="Visible"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding PreparingScan}" Value="True">
                                    <Setter Property="Visibility" Value="Hidden" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding CancelScan}" Value="True">
                                    <Setter Property="Visibility" Value="Hidden" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0} / {1} ">
                            <Binding Path="PortsScanned" />
                            <Binding Path="PortsToScan" />
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </StackPanel>
            <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" Style="{StaticResource HeaderTextBlock}" Text="{DynamicResource String_Header_Result}" />
        <DataGrid Grid.Row="3" ItemsSource="{Binding PortScanResultView}" SelectedItem="{Binding SelectedScanResult}">
            <DataGrid.Resources>
                <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource DataGridScrollBar}" />
                <WpfHelper:BindingProxy x:Key="BindingProxy" Data="{Binding}" />
                <ContextMenu x:Key="RowContextMenu" Opened="ContextMenu_Opened" MinWidth="150">
                    <MenuItem Header="{DynamicResource String_CopyDots}">
                        <MenuItem.Icon>
                            <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                <Rectangle.OpacityMask>
                                    <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=ContentCopy}" />
                                </Rectangle.OpacityMask>
                            </Rectangle>
                        </MenuItem.Icon>
                        <MenuItem Header="{DynamicResource String_IPAddress}" Command="{Binding CopySelectedIPAddressCommand}">
                            <MenuItem.Icon>
                                <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                    <Rectangle.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=ContentCopy}" />
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{DynamicResource String_Hostname}" Command="{Binding CopySelectedHostnameCommand}">
                            <MenuItem.Icon>
                                <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                    <Rectangle.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=ContentCopy}" />
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{DynamicResource String_Port}" Command="{Binding CopySelectedPortCommand}">
                            <MenuItem.Icon>
                                <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                    <Rectangle.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=ContentCopy}" />
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{DynamicResource String_Status}" Command="{Binding CopySelectedStatusCommand}">
                            <MenuItem.Icon>
                                <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                    <Rectangle.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=ContentCopy}" />
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{DynamicResource String_Protocol}" Command="{Binding CopySelectedProtocolCommand}">
                            <MenuItem.Icon>
                                <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                    <Rectangle.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=ContentCopy}" />
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{DynamicResource String_Service}" Command="{Binding CopySelectedServiceCommand}">
                            <MenuItem.Icon>
                                <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                    <Rectangle.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=ContentCopy}" />
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="{DynamicResource String_Description}" Command="{Binding CopySelectedDescriptionCommand}">
                            <MenuItem.Icon>
                                <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                    <Rectangle.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=ContentCopy}" />
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                            </MenuItem.Icon>
                        </MenuItem>
                    </MenuItem>
                </ContextMenu>
            </DataGrid.Resources>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource MetroDataGridRow}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding SelectedItems.Count, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}" Value="1" >
                            <Setter Property="ContextMenu" Value="{StaticResource RowContextMenu}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="{DynamicResource String_IPAddress}" Binding="{Binding Host.Item1}" MinWidth="150" />
                <DataGridTextColumn Header="{DynamicResource String_Hostname}" Binding="{Binding Host.Item2, Converter={StaticResource NullOrEmptyToStringConverter}}" MinWidth="150" />
                <DataGridTextColumn Header="{DynamicResource String_Port}" Binding="{Binding Port}" MinWidth="80" />
                <DataGridTextColumn Header="{DynamicResource String_Status}" Binding="{Binding Status, Converter={StaticResource PortStatusToStringConverter}}" MinWidth="100" />
                <DataGridTextColumn Header="{DynamicResource String_Protocol}" Binding="{Binding LookupInfo.Protocol}" MinWidth="100" />
                <DataGridTextColumn Header="{DynamicResource String_Service}" Binding="{Binding LookupInfo.Service}" MinWidth="100" />
                <DataGridTextColumn Header="{DynamicResource String_Description}" Binding="{Binding LookupInfo.Description}" MinWidth="200" Width="*" />
            </DataGrid.Columns>
        </DataGrid>
        <Expander Grid.Row="4" IsExpanded="{Binding ExpandStatistics}" Style="{StaticResource HeaderExpander}" Visibility="{Binding ShowStatistics, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="0,20,0,0">
            <Expander.Header>
                <TextBlock Text="{DynamicResource String_Header_Statistics}" Style="{StaticResource HeaderTextBlock}" Margin="0" />
            </Expander.Header>
            <StackPanel>
                <Grid Margin="0,10,0,0">
                    <Grid.ColumnDefinitions>
                        <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_Scanned}" />
                    <TextBox Grid.Column="4" Grid.Row="0">
                        <TextBox.Text>
                            <MultiBinding StringFormat="{}{0} / {1}">
                                <Binding Path="PortsScanned" />
                                <Binding Path="PortsToScan" />
                            </MultiBinding>
                        </TextBox.Text>
                    </TextBox>
                    <TextBlock Grid.Column="3" Grid.Row="1" Text="{DynamicResource String_Open}" />
                    <TextBox Grid.Column="4" Grid.Row="1" Text="{Binding PortsOpen}" />
                </Grid>
            </StackPanel>
        </Expander>
    </Grid>
</UserControl>
