<UserControl x:Class="NETworkManager.Views.RemoteDesktopSessionConnectDialog"
             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:Validator="clr-namespace:NETworkManager.Validators"
             xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
             xmlns:WpfHelper="clr-namespace:NETworkManager.WpfHelper"
             xmlns:Converter="clr-namespace:NETworkManager.Converters"
             xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             mc:Ignorable="d" Loaded="UserControl_Loaded">
    <UserControl.Resources>
        <Converter:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <Converter:BooleanReverseToVisibilityConverter x:Key="BooleanReverseToVisibilityConverter" />
        <Converter:BooleanReverseConverter x:Key="BooleanReverseConverter" />
    </UserControl.Resources>
    <Grid Margin="0,20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="10" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <Grid.Resources>
                <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource CenterTextBlock}" />
                <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultTextBox}" />
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="10" />
                <ColumnDefinition Width="1*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="10" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="10" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0" Text="{DynamicResource String_Name}" Visibility="{Binding ConnectAs, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="0,0,0,10" />
            <TextBox x:Name="txtName" Grid.Column="2" Grid.Row="0" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_ExampleRemoteDesktopSessionName}" Visibility="{Binding ConnectAs, Converter={StaticResource BooleanToVisibilityConverter}}" IsEnabled="{Binding ConnectAs, Converter={StaticResource BooleanReverseConverter}}" Margin="0,0,0,10">
                <TextBox.Text>
                    <Binding Path="Name" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <Validator:EmptyValidator ValidatesOnTargetUpdated="True" />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
            <TextBlock Grid.Column="0" Grid.Row="1" Text="{DynamicResource String_Host}" />
            <ComboBox x:Name="cbHost" Grid.Column="2" Grid.Row="1" ItemsSource="{Binding HostHistoryView}" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_ExampleHostname}" IsEnabled="{Binding ConnectAs, Converter={StaticResource BooleanReverseConverter}}" Style="{StaticResource EditableComboBox}">
                <ComboBox.Text>
                    <Binding Path="Host" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <Validator:EmptyValidator ValidatesOnTargetUpdated="True" />
                            <Validator:NoSpacesValidator ValidatesOnTargetUpdated="True" />
                        </Binding.ValidationRules>
                    </Binding>
                </ComboBox.Text>
            </ComboBox>
            <TextBlock Grid.Column="0" Grid.Row="3" Text="{DynamicResource String_UseCredentials}" />
            <mah:ToggleSwitch Grid.Column="2" Grid.Row="3" OffLabel="" OnLabel="" IsChecked="{Binding UseCredentials}" IsEnabled="{Binding ConnectAs, Converter={StaticResource BooleanReverseConverter}}" />
            <StackPanel Grid.Column="2" Grid.Row="5" Orientation="Horizontal" Visibility="{Binding UseCredentials, Converter={StaticResource BooleanToVisibilityConverter}}">
                <RadioButton Content="{DynamicResource String_Credentials}" IsEnabled="{Binding CredentialsLocked, Converter={StaticResource BooleanReverseConverter}}" />
                <RadioButton Content="{DynamicResource String_Custom}" IsChecked="{Binding CustomCredentials}" Margin="10,0,0,0"/>
            </StackPanel>
        </Grid>
        <Grid Grid.Row="1" Visibility="{Binding UseCredentials, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="0,10,0,0">
            <Grid Visibility="{Binding CustomCredentials, Converter={StaticResource BooleanToVisibilityConverter}}">
                <Grid.Resources>
                    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource CenterTextBlock}" />
                    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultTextBox}" />
                    <Style TargetType="{x:Type PasswordBox}" BasedOn="{StaticResource DefaultPasswordBox}" />
                </Grid.Resources>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="1*" />
                </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_Username}" />
                <TextBox x:Name="txtUsername" Grid.Column="2" Grid.Row="0" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_ExampleUsername}">
                    <TextBox.Text>
                        <Binding Path="Username" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
                            <Binding.ValidationRules>
                                <Validator:EmptyValidator ValidatesOnTargetUpdated="True" />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
                <TextBlock Grid.Column="0" Grid.Row="2" Text="{DynamicResource String_Password}" />
                <PasswordBox x:Name="passwordBoxPassword" Grid.Column="2" Grid.Row="2" Style="{StaticResource DefaultPasswordBox}" >
                    <Interactivity:Interaction.Behaviors>
                        <WpfHelper:PasswordBoxBindingBehavior Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    </Interactivity:Interaction.Behaviors>
                </PasswordBox>
            </Grid>
            <Grid Visibility="{Binding CustomCredentials, Converter={StaticResource BooleanReverseToVisibilityConverter}}">
                <Grid.Resources>
                    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource CenterTextBlock}" />
                    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource DefaultTextBox}" />
                </Grid.Resources>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBlock Grid.Column="0" Grid.Row="0" Text="{DynamicResource String_Credential}" />
                <ComboBox x:Name="cbCredentialID" Grid.Column="2" Grid.Row="0" SelectedValuePath="ID" SelectedValue="{Binding CredentialID}" Style="{StaticResource DefaultComboBox}" ItemsSource="{Binding Credentials}">
                    <ComboBox.InputBindings>
                        <KeyBinding Command="{Binding UnselectCredentialCommand}" Key="Delete"/>
                    </ComboBox.InputBindings>
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock>
                                    <TextBlock.Text>
                                        <MultiBinding StringFormat="{}[{0}] {1}">
                                            <Binding Path="ID" />
                                            <Binding Path="Name" />
                                        </MultiBinding>
                                    </TextBlock.Text>
                                </TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </Grid>
        </Grid>
        <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Content="{DynamicResource String_Button_Cancel}" Command="{Binding CancelCommand}" IsCancel="True" Style="{StaticResource DefaultButton}" />
            <Button Content="{DynamicResource String_Button_Connect}" Command="{Binding ConnectCommand}" IsDefault="True" Margin="10,0,0,0">
                <Button.Style>
                    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource HighlightedButton}">
                        <Setter Property="IsEnabled" Value="True" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=(Validation.HasError), ElementName=cbHost}" Value="True" >
                                <Setter Property="IsEnabled" Value="False" />
                            </DataTrigger>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding UseCredentials}" Value="True" />
                                    <Condition Binding="{Binding CustomCredentials}" Value="False" />
                                    <Condition Binding="{Binding CredentialID}" Value="{x:Null}" />
                                </MultiDataTrigger.Conditions>
                                <MultiDataTrigger.Setters>
                                    <Setter Property="IsEnabled" Value="False" />
                                </MultiDataTrigger.Setters>
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </StackPanel>
    </Grid>
</UserControl>
