<UserControl x:Name="UserControl"
    x:Class="ModernKeePass.Views.UserControls.CompositeKeyUserControl"
    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:interactivity="using:Microsoft.Xaml.Interactivity" 
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:converters="using:ModernKeePass.Converters"
    xmlns:viewModels="using:ModernKeePass.ViewModels"
    mc:Ignorable="d" >
    <UserControl.Resources>
        <converters:ProgressBarLegalValuesConverter x:Key="ProgressBarLegalValuesConverter"/>
        <converters:DoubleToSolidColorBrushConverter x:Key="DoubleToForegroungBrushConverter"/>
        <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <converters:DiscreteIntToSolidColorBrushConverter x:Key="DiscreteIntToSolidColorBrushConverter"/>
        <converters:EmptyStringToVisibilityConverter x:Key="EmptyStringToVisibilityConverter"/>
    </UserControl.Resources>
    <Grid x:Name="Grid">
        <!-- DataContext is not set at the root of the control because of issues happening when displaying it -->
        <Grid.DataContext>
            <viewModels:CompositeKeyVm />
        </Grid.DataContext>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="45" />
            <RowDefinition Height="40" />
            <RowDefinition Height="40" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <CheckBox Grid.Row="0" Grid.Column="0" IsChecked="{Binding HasPassword, Mode=TwoWay}" />
        <PasswordBox Grid.Row="0" Grid.Column="1" x:Uid="CompositeKeyPassword" Password="{Binding Password, Mode=TwoWay}" Height="30" IsPasswordRevealButtonEnabled="True" KeyDown="PasswordBox_KeyDown" BorderBrush="{Binding StatusType, Converter={StaticResource DiscreteIntToSolidColorBrushConverter}}" >
            <interactivity:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="GotFocus">
                    <core:ChangePropertyAction TargetObject="{Binding}" PropertyName="HasPassword" Value="True" />
                </core:EventTriggerBehavior>
            </interactivity:Interaction.Behaviors>
        </PasswordBox>
        <ProgressBar Grid.Row="0" Grid.Column="1" 
                     Maximum="128" VerticalAlignment="Bottom" 
                     Value="{Binding PasswordComplexityIndicator, ConverterParameter=0\,128, Converter={StaticResource ProgressBarLegalValuesConverter}}"
                     Foreground="{Binding PasswordComplexityIndicator, ConverterParameter=128, Converter={StaticResource DoubleToForegroungBrushConverter}}" 
                     Visibility="{Binding ShowComplexityIndicator, ElementName=UserControl, Converter={StaticResource BooleanToVisibilityConverter}}" />
        <CheckBox Grid.Row="1" Grid.Column="0" IsChecked="{Binding HasKeyFile, Mode=TwoWay}" />
        <HyperlinkButton Grid.Row="1" Grid.Column="1" Margin="-15,0,0,0" Content="{Binding KeyFileText}" IsEnabled="{Binding HasKeyFile}" Click="KeyFileButton_Click" />
        <HyperlinkButton Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" Visibility="{Binding ShowComplexityIndicator, ElementName=UserControl, Converter={StaticResource BooleanToVisibilityConverter}}" IsEnabled="{Binding HasKeyFile}" Click="CreateKeyFileButton_Click">
            <SymbolIcon Symbol="Add">
                <ToolTipService.ToolTip>
                    <ToolTip x:Uid="CompositeKeyNewKeyFileTooltip" />
                </ToolTipService.ToolTip>
            </SymbolIcon>
        </HyperlinkButton>
        <Button Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" Content="{Binding ButtonLabel, ElementName=UserControl}" Click="OpenButton_OnClick" Background="{ThemeResource MainColor}" Foreground="{ThemeResource TextColor}" IsEnabled="{Binding IsValid}" />
        <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3" Height="Auto" FontSize="14" FontWeight="Light" Text="{Binding Status}" Foreground="{Binding StatusType, Converter={StaticResource DiscreteIntToSolidColorBrushConverter}}" Visibility="{Binding Status, Converter={StaticResource EmptyStringToVisibilityConverter}}" />
    </Grid>
</UserControl>
