<UserControl x:Class="NETworkManager.Views.WakeOnLANView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"          
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
             xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
             xmlns:IconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
             xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
             xmlns:Validator="clr-namespace:NETworkManager.Validators"
             xmlns:Converter="clr-namespace:NETworkManager.Converters"
             xmlns:WpfHelper="clr-namespace:NETworkManager.WpfHelper"
             xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
             Dialog:DialogParticipation.Register="{Binding}"
             mc:Ignorable="d">
    <UserControl.Resources>
        <Converter:NullOrEmptyToBoolConverter x:Key="NullOrEmptyToBoolConverter" />
        <Converter:SelectedItemsToBoolConverter x:Key="SelectedItemsToBoolConverter" />
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition MinWidth="40" Width="{Binding ClientWidth, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MaxWidth="350" />
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="0" Margin="10">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Style="{StaticResource HeaderTextBlock}" Text="{DynamicResource String_Header_Client}" />
            <Grid Grid.Row="1" 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" />
                    <RowDefinition Height="10" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBlock Grid.Column="0" Grid.Row="0" Style="{StaticResource CenterTextBlock}" Text="{DynamicResource String_MACAddress}" />
                <TextBox Grid.Column="2" Grid.Row="0" x:Name="txtMACAddress" Height="{Binding ElementName=txtPort, Path=ActualHeight}" WpfHelper:ProtocolSettingsLayout.MVVMHasError="{Binding MACAddressHasError}" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_ExampleMACAddress}">
                    <TextBox.InputBindings>
                        <KeyBinding Key="Enter" Command="{Binding WakeUpCommand}" />
                    </TextBox.InputBindings>
                    <TextBox.Text>
                        <Binding Path="MACAddress" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <Validator:EmptyValidator ValidatesOnTargetUpdated="True" />
                                <Validator:MACAddressValidator ValidatesOnTargetUpdated="True"/>
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
                <TextBlock Grid.Column="0" Grid.Row="2" Text="{DynamicResource String_Broadcast}" Style="{StaticResource CenterTextBlock}" />
                <TextBox x:Name="txtBroadcast" Grid.Column="2" Grid.Row="2" Height="{Binding ElementName=txtPort, Path=ActualHeight}" WpfHelper:ProtocolSettingsLayout.MVVMHasError="{Binding BroadcastHasError}" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_ExampleIPv4Broadcast}">
                    <TextBox.InputBindings>
                        <KeyBinding Key="Enter" Command="{Binding WakeUpCommand}" />
                    </TextBox.InputBindings>
                    <TextBox.Text>
                        <Binding Path="Broadcast" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <Validator:EmptyValidator ValidatesOnTargetUpdated="True" />
                                <Validator:IPv4AddressValidator ValidatesOnTargetUpdated="True" />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
                <TextBlock Grid.Column="0" Grid.Row="4" Text="{DynamicResource String_Port}" Style="{StaticResource CenterTextBlock}" />
                <TextBox x:Name="txtPort" Grid.Column="2" Grid.Row="4" WpfHelper:ProtocolSettingsLayout.MVVMHasError="{Binding PortHasError}" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_ExamplePort7}" HorizontalAlignment="Left" Width="65" Validation.ErrorTemplate="{StaticResource DefaultErrorTemplate}">
                    <TextBox.InputBindings>
                        <KeyBinding Key="Enter" Command="{Binding WakeUpCommand}" />
                    </TextBox.InputBindings>
                    <TextBox.Text>
                        <Binding Path="Port" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <Validator:EmptyValidator ValidatesOnTargetUpdated="True" />
                                <Validator:PortValidator ValidatesOnTargetUpdated="True" />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
                <Button Grid.Column="4" Grid.Row="4" HorizontalAlignment="Right" Command="{Binding WakeUpCommand}">
                    <Button.Content>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Rectangle Width="20" Height="20" Margin="10,5,0,5" Fill="{DynamicResource GrayBrush3}">
                                <Rectangle.OpacityMask>
                                    <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=Power}" />
                                </Rectangle.OpacityMask>
                            </Rectangle>
                            <TextBlock Grid.Column="1" Text="{DynamicResource String_Button_WakeUp}" FontSize="14" Margin="10,5" TextAlignment="Center"/>
                        </Grid>
                    </Button.Content>
                    <Button.Style>
                        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ImageWithTextButton}" >
                            <Setter Property="IsEnabled" Value="True" />
                            <Style.Triggers>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding Path=(Validation.HasError), ElementName=txtMACAddress}" Value="True"/>
                                        <Condition Binding="{Binding Path=(Validation.HasError), ElementName=txtBroadcast}" Value="True"/>
                                        <Condition Binding="{Binding Path=(Validation.HasError), ElementName=txtPort}" Value="True"/>
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="IsEnabled" Value="False" />
                                </MultiDataTrigger>
                                <DataTrigger Binding="{Binding IsSending}" Value="True" >
                                    <Setter Property="IsEnabled" Value="False" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
            </Grid>
            <Controls:MetroProgressBar Grid.Row="2" Margin="0,10,0,0" Foreground="{DynamicResource AccentColorBrush}" IsIndeterminate="True">
                <Controls:MetroProgressBar.Style>
                    <Style TargetType="{x:Type  Controls:MetroProgressBar}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsSending}" Value="False">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Controls:MetroProgressBar.Style>
            </Controls:MetroProgressBar>
            <TextBlock Grid.Row="3" Foreground="{DynamicResource AccentColorBrush}" Text="{Binding StatusMessage}" Visibility="{Binding DisplayStatusMessage, Converter={StaticResource BooleanToVisibilityConverter}}" Style="{DynamicResource DefaultTextBlock}" Margin="0,10,0,0" />
        </Grid>
        <GridSplitter Grid.Column="1" Style="{StaticResource ProfileGridSplitter}" />
        <Expander Grid.Column="2" x:Name="expanderProfiles" Header="{DynamicResource String_Header_Clients}" Style="{StaticResource RightExpander}" IsExpanded="{Binding ExpandClientView}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="10" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="10" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBox x:Name="txtSearch" Grid.Column="0" VerticalAlignment="Center" Text="{Binding Search, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource SearchTextBox}" />
                <ListBox Grid.Column="0" Grid.Row="2" x:Name="listBoxProfiles" ItemsSource="{Binding WakeOnLANClients}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedClient}" Style="{StaticResource ProfileListBox}">
                    <ListBox.Resources>
                        <ContextMenu x:Key="ListBoxItemContextMenu" Opened="ContextMenu_Opened" MinWidth="150">
                            <MenuItem Header="{DynamicResource String_WakeUp}" Command="{Binding WakeUpClientCommand}">
                                <MenuItem.Icon>
                                    <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                        <Rectangle.OpacityMask>
                                            <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=Power}" />
                                        </Rectangle.OpacityMask>
                                    </Rectangle>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="{DynamicResource String_Edit}" Command="{Binding EditClientCommand}">
                                <MenuItem.Icon>
                                    <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                        <Rectangle.OpacityMask>
                                            <VisualBrush Stretch="Uniform" Visual="{IconPacks:Modern Kind=Edit}" />
                                        </Rectangle.OpacityMask>
                                    </Rectangle>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="{DynamicResource String_CopyAs}" Command="{Binding CopyAsClientCommand}">
                                <MenuItem.Icon>
                                    <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                        <Rectangle.OpacityMask>
                                            <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=ContentDuplicate}" />
                                        </Rectangle.OpacityMask>
                                    </Rectangle>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="{DynamicResource String_Delete}" Command="{Binding DeleteClientCommand}">
                                <MenuItem.Icon>
                                    <Rectangle Width="16" Height="16" Fill="{DynamicResource BlackColorBrush}">
                                        <Rectangle.OpacityMask>
                                            <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=Close}" />
                                        </Rectangle.OpacityMask>
                                    </Rectangle>
                                </MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                        <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource MetroListBoxItem}">
                            <Setter Property="ContextMenu" Value="{StaticResource ListBoxItemContextMenu}" />
                        </Style>
                    </ListBox.Resources>
                    <ListBox.InputBindings>
                        <KeyBinding Command="{Binding EditClientCommand}" Key="F2" />
                        <KeyBinding Command="{Binding DeleteClientCommand}" Key="Delete" />
                    </ListBox.InputBindings>
                    <ListBox.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate>
                                                <Expander IsExpanded="True" Style="{StaticResource DefaultExpander}">
                                                    <Expander.Header>
                                                        <Grid>
                                                            <Grid.ColumnDefinitions>
                                                                <ColumnDefinition Width="*" />
                                                                <ColumnDefinition Width="Auto" />
                                                            </Grid.ColumnDefinitions>
                                                            <TextBlock Grid.Column="0" Text="{Binding Name}" Style="{DynamicResource DefaultTextBlock}"/>
                                                            <Button Grid.Column="1" ToolTip="{DynamicResource String_EditGroup}" Command="{Binding DataContext.EditGroupCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}}" CommandParameter="{Binding Name}" Visibility="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Converter={StaticResource BooleanToVisibilityConverter}}" Style="{StaticResource CleanButton}">
                                                                <Rectangle Width="16" Height="16">
                                                                    <Rectangle.OpacityMask>
                                                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Modern Kind=Edit}" />
                                                                    </Rectangle.OpacityMask>
                                                                    <Rectangle.Style>
                                                                        <Style TargetType="{x:Type Rectangle}">
                                                                            <Setter Property="Fill" Value="{DynamicResource GrayBrush3}" />
                                                                            <Style.Triggers>
                                                                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsMouseOver}" Value="True">
                                                                                    <Setter Property="Fill" Value="{DynamicResource GrayBrush5}" />
                                                                                </DataTrigger>
                                                                            </Style.Triggers>
                                                                        </Style>
                                                                    </Rectangle.Style>
                                                                </Rectangle>
                                                            </Button>
                                                        </Grid>
                                                    </Expander.Header>
                                                    <ItemsPresenter />
                                                </Expander>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </ListBox.GroupStyle>
                </ListBox>
                <Grid Grid.Row="2" VerticalAlignment="Center">
                    <Grid.Style>
                        <Style TargetType="{x:Type Grid}">
                            <Setter Property="Visibility" Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding WakeOnLANClients.Count}" Value="0">
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Grid.Style>
                    <StackPanel>
                        <Button HorizontalAlignment="Center" Focusable="False" Margin="0,0,0,10">
                            <Button.Style>
                                <Style TargetType="{x:Type Button}" BasedOn="{StaticResource CleanButton}">
                                    <Setter Property="Command" Value="{Binding AddClientCommand}" />
                                    <Setter Property="ToolTip" Value="{DynamicResource String_AddClientDots}" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Search, Converter={StaticResource NullOrEmptyToBoolConverter}}" Value="False">
                                            <Setter Property="Command" Value="{Binding ClearSearchCommand}" />
                                            <Setter Property="ToolTip" Value="{DynamicResource String_ClearFilter}" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Button.Style>
                            <Rectangle Width="20" Height="20">
                                <Rectangle.Resources>
                                    <VisualBrush x:Key="VisualPlus" Stretch="Uniform" Visual="{IconPacks:Material Kind=Plus}"/>
                                    <VisualBrush x:Key="VisualClear" Stretch="Uniform" Visual="{IconPacks:Material Kind=FilterRemoveOutline}" />
                                </Rectangle.Resources>
                                <Rectangle.Style>
                                    <Style TargetType="{x:Type Rectangle}">
                                        <Setter Property="OpacityMask" Value="{StaticResource VisualPlus}" />
                                        <Setter Property="Fill" Value="{DynamicResource GrayBrush3}" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsMouseOver}" Value="True">
                                                <Setter Property="Fill" Value="{DynamicResource GrayBrush5}" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Search, Converter={StaticResource NullOrEmptyToBoolConverter}}" Value="False">
                                                <Setter Property="OpacityMask" Value="{StaticResource VisualClear}" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Rectangle.Style>
                            </Rectangle>
                        </Button>
                        <TextBlock>
                            <TextBlock.Style>
                                <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource MessageTextBlock}">
                                    <Setter Property="Text" Value="{DynamicResource String_NoClientsFoundCreateOne}" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Search, Converter={StaticResource NullOrEmptyToBoolConverter}}" Value="False">
                                            <Setter Property="Text" Value="{DynamicResource String_NothingFound}" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </StackPanel>
                </Grid>
                <Button x:Name="btnAddClient" Grid.Column="0" Grid.Row="4" Command="{Binding AddClientCommand}" Style="{StaticResource ImageWithTextButton}">
                    <Button.Content>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Rectangle Width="20" Height="20" Margin="10,5,0,5" Fill="{DynamicResource GrayBrush3}">
                                <Rectangle.OpacityMask>
                                    <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=Plus}" />
                                </Rectangle.OpacityMask>
                            </Rectangle>
                            <TextBlock Grid.Column="1" Text="{DynamicResource String_Button_AddClient}" FontSize="14" Margin="10,5" TextAlignment="Center"/>
                        </Grid>
                    </Button.Content>
                </Button>
            </Grid>
        </Expander>
    </Grid>
</UserControl>