<UserControl x:Class="NETworkManager.Views.CredentialsView"
        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:IconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
        xmlns:Converter="clr-namespace:NETworkManager.Converters"
        xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
        Dialog:DialogParticipation.Register="{Binding}"
        Loaded="UserControl_Loaded"
        mc:Ignorable="d">
    <UserControl.Resources>
        <Converter:BooleanReverseConverter x:Key="BooleanReverseConverter" />
        <Converter:BooleanReverseToVisibilityConverter x:Key="BooleanReverseToVisibilityConverter" />
        <Converter:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </UserControl.Resources>
    <Grid>
        <!-- Getting started / Set master password-->
        <Grid>
            <Grid.Style>
                <Style TargetType="{x:Type Grid}" >
                    <Setter Property="Visibility" Value="Collapsed" />
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding CredentialsFileExists}" Value="False" />
                                <Condition Binding="{Binding CredentialsLoaded}" Value="False" />
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter Property="Visibility" Value="Visible" />
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </Grid.Style>
            <StackPanel VerticalAlignment="Center">
                <TextBlock Foreground="{DynamicResource GrayBrush3}" Text="{DynamicResource String_CredentialsEncryptionDisclaimer}" TextWrapping="Wrap" TextAlignment="Center" Style="{StaticResource HeaderTextBlock}" Margin="0,0,0,10" />
                <Button HorizontalAlignment="Center" Command="{Binding SetMasterPasswordCommand}" Style="{StaticResource ImageWithTextButton}">
                    <Button.Content>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Rectangle Width="20" Height="20" Fill="{DynamicResource GrayBrush3}" Margin="10,5,0,5">
                                <Rectangle.OpacityMask>
                                    <VisualBrush Stretch="Uniform" Visual="{IconPacks:Entypo Kind=Key}" />
                                </Rectangle.OpacityMask>
                            </Rectangle>
                            <TextBlock Grid.Column="1" Text="{DynamicResource String_Button_SetMasterPassword}" Style="{StaticResource DefaultTextBlock}" TextAlignment="Center" Margin="10,5" />
                        </Grid>
                    </Button.Content>
                </Button>
            </StackPanel>
        </Grid>
        <!-- Load/Unlock -->
        <Grid>
            <Grid.Style>
                <Style TargetType="{x:Type Grid}" >
                    <Setter Property="Visibility" Value="Collapsed" />
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding CredentialsFileExists}" Value="True" />
                                <Condition Binding="{Binding CredentialsLoaded}" Value="False" />
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter Property="Visibility" Value="Visible" />
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </Grid.Style>
            <StackPanel VerticalAlignment="Center">
                <TextBlock Foreground="{DynamicResource GrayBrush3}" Text="{DynamicResource String_DecryptAndLoadCredentialsMessage}" TextWrapping="Wrap" TextAlignment="Center" Style="{StaticResource HeaderTextBlock}" Margin="0,0,0,10" />
                <Button HorizontalAlignment="Center" Command="{Binding DecryptAndLoadCommand}" Style="{StaticResource ImageWithTextButton}">
                    <Button.Content>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Rectangle Width="20" Height="20" Fill="{DynamicResource GrayBrush3}" Margin="10,5,0,5">
                                <Rectangle.OpacityMask>
                                    <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=LockOpenOutline}" />
                                </Rectangle.OpacityMask>
                            </Rectangle>
                            <TextBlock Grid.Column="1" Text="{DynamicResource String_Button_DecryptAndLoad}" Style="{StaticResource DefaultTextBlock}" TextAlignment="Center" Margin="10,5" />
                        </Grid>
                    </Button.Content>
                </Button>
            </StackPanel>
        </Grid>
        <!-- Credentials -->
        <Grid>
            <Grid.Style>
                <Style TargetType="{x:Type Grid}" >
                    <Setter Property="Visibility" Value="Collapsed" />
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding CredentialsLoaded}" Value="True" />
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter Property="Visibility" Value="Visible" />
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </Grid.Style>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="10" />
                <RowDefinition Height="*" />
                <RowDefinition Height="10" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Button Grid.Column="0" Command="{Binding ChangeMasterPasswordCommand}" IsEnabled="{Binding Locked, Converter={StaticResource BooleanReverseConverter}}" Style="{StaticResource ImageWithTextButton}">
                    <Button.Content>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Rectangle Width="20" Height="20" Fill="{DynamicResource GrayBrush3}" Margin="10,5,0,5">
                                <Rectangle.OpacityMask>
                                    <VisualBrush Stretch="Uniform" Visual="{IconPacks:Entypo Kind=Key}" />
                                </Rectangle.OpacityMask>
                            </Rectangle>
                            <TextBlock Grid.Column="1" Text="{DynamicResource String_Button_ChangeMasterPassword}" Style="{StaticResource DefaultTextBlock}" TextAlignment="Center" Margin="10,5" />
                        </Grid>
                    </Button.Content>
                </Button>
                <TextBox x:Name="txtSearch" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Right" Width="250" Text="{Binding Search, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource SearchTextBox}" />
            </Grid>
            <DataGrid x:Name="dataGridCredentials" Grid.Row="2" Style="{StaticResource MetroDataGrid}" FontSize="14" mah:ControlsHelper.ContentCharacterCasing="Normal" ItemsSource="{Binding Credentials}" SelectedItem="{Binding SelectedCredential}" CanUserResizeColumns="False" VerticalScrollBarVisibility="Auto" AutoGenerateColumns="false" IsReadOnly="True">
                <DataGrid.Resources>
                    <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource DataGridScrollBar}" />
                    <ContextMenu x:Key="RowContextMenu" Opened="ContextMenu_Opened"  MinWidth="150">
                        <MenuItem Header="{DynamicResource String_Edit}" Command="{Binding EditCommand}" IsEnabled="{Binding Locked, Converter={StaticResource BooleanReverseConverter}}">
                            <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_Delete}" Command="{Binding DeleteCommand}" IsEnabled="{Binding Locked, Converter={StaticResource BooleanReverseConverter}}">
                            <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>
                </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_ID}" Binding="{Binding ID}" MinWidth="150" />
                    <DataGridTextColumn Header="{DynamicResource String_Name}" Binding="{Binding Name}" MinWidth="200" />
                    <DataGridTextColumn Header="{DynamicResource String_Username}" Binding="{Binding Username}" MinWidth="200" />
                    <DataGridTextColumn Header="{DynamicResource String_Password}" Binding="{Binding Source={StaticResource String_HiddenPassword}, Path=.}" MinWidth="200" />
                </DataGrid.Columns>
            </DataGrid>
            <Grid Grid.Row="4">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <StackPanel Grid.Column="0" Visibility="{Binding Locked, Converter={StaticResource BooleanToVisibilityConverter}}" Orientation="Horizontal" >
                    <Button Command="{Binding LockUnlockCommand}" Style="{StaticResource ImageButton}">
                        <Rectangle Width="20" Height="20" Fill="{DynamicResource GrayBrush3}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Uniform" Visual="{IconPacks:Entypo Kind=Lock}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button>
                    <TextBlock Style="{StaticResource DefaultTextBlock}" Text="{StaticResource String_UserInterfaceLocked}" VerticalAlignment="Center" Margin="10,0,0,0" />
                </StackPanel>
                <StackPanel Grid.Column="0" Visibility="{Binding Locked, Converter={StaticResource BooleanReverseToVisibilityConverter}}" Orientation="Horizontal" >
                    <Button Command="{Binding LockUnlockCommand}" Style="{StaticResource ImageButton}">
                        <Rectangle Width="20" Height="20" Fill="{DynamicResource GrayBrush3}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Uniform" Visual="{IconPacks:Entypo Kind=LockOpen}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                    </Button>                    
                    <TextBlock Style="{StaticResource DefaultTextBlock}" VerticalAlignment="Center" Margin="10,0,0,0">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0}: {1}">
                                <Binding Source="{StaticResource String_RemainingTime}" Path="." />
                                <Binding Path="TimeRemaining" />
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </StackPanel>
                <StackPanel Grid.Column="2" HorizontalAlignment="Right" Orientation="Horizontal">
                    <Button Command="{Binding DeleteCommand}">
                        <Button.Style>
                            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ImageWithTextButton}">
                                <Setter Property="IsEnabled" Value="False"/>
                                <Style.Triggers>
                                    <MultiDataTrigger>
                                        <MultiDataTrigger.Conditions>
                                            <Condition Binding="{Binding ElementName=dataGridCredentials, Path=SelectedItems.Count}" Value="1" />
                                            <Condition Binding="{Binding Locked}" Value="False" />
                                        </MultiDataTrigger.Conditions>
                                        <MultiDataTrigger.Setters>
                                            <Setter Property="IsEnabled" Value="True"/>
                                        </MultiDataTrigger.Setters>
                                    </MultiDataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                        <Button.Content>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Rectangle Width="20" Height="20" Fill="{DynamicResource GrayBrush3}" Margin="10,5,0,5">
                                    <Rectangle.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=Close}" />
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                                <TextBlock Grid.Column="1" Text="{DynamicResource String_Button_DeleteDots}" Style="{StaticResource DefaultTextBlock}" TextAlignment="Center" Margin="10,5" />
                            </Grid>
                        </Button.Content>
                    </Button>
                    <Button Command="{Binding EditCommand}" Margin="10,0,0,0">
                        <Button.Style>
                            <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ImageWithTextButton}">
                                <Setter Property="IsEnabled" Value="False"/>
                                <Style.Triggers>
                                    <MultiDataTrigger>
                                        <MultiDataTrigger.Conditions>
                                            <Condition Binding="{Binding ElementName=dataGridCredentials, Path=SelectedItems.Count}" Value="1" />
                                            <Condition Binding="{Binding Locked}" Value="False" />
                                        </MultiDataTrigger.Conditions>
                                        <MultiDataTrigger.Setters>
                                            <Setter Property="IsEnabled" Value="True"/>
                                        </MultiDataTrigger.Setters>
                                    </MultiDataTrigger>                                  
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                        <Button.Content>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Rectangle Width="20" Height="20" Fill="{DynamicResource GrayBrush3}" Margin="10,5,0,5">
                                    <Rectangle.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Modern Kind=Edit}" />
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                                <TextBlock Grid.Column="1" Text="{DynamicResource String_Button_EditDots}" Style="{StaticResource DefaultTextBlock}" TextAlignment="Center" Margin="10,5" />
                            </Grid>
                        </Button.Content>
                    </Button>
                    <Button Command="{Binding AddCommand}" Style="{StaticResource ImageWithTextButton}" IsEnabled="{Binding Locked, Converter={StaticResource BooleanReverseConverter}}" Margin="10,0,0,0">
                        <Button.Content>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Rectangle Width="20" Height="20" Fill="{DynamicResource GrayBrush3}" Margin="10,5,0,5">
                                    <Rectangle.OpacityMask>
                                        <VisualBrush Stretch="Uniform" Visual="{IconPacks:Material Kind=Plus}" />
                                    </Rectangle.OpacityMask>
                                </Rectangle>
                                <TextBlock Grid.Column="1" Text="{DynamicResource String_Button_AddDots}" Style="{StaticResource DefaultTextBlock}" TextAlignment="Center" Margin="10,5" />
                            </Grid>
                        </Button.Content>
                    </Button>
                </StackPanel>
            </Grid>
        </Grid>
    </Grid>
</UserControl>
