<Window x:Class="HandBrakeWPF.Views.PresetManagerView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Properties="clr-namespace:HandBrakeWPF.Properties"
        xmlns:converters="clr-namespace:HandBrakeWPF.Converters"
        xmlns:loc="clr-namespace:HandBrakeWPF.Services.Presets.Model"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:commands="clr-namespace:HandBrakeWPF.Commands"
        xmlns:controls="clr-namespace:HandBrakeWPF.Controls"
        xmlns:picture="clr-namespace:HandBrakeWPF.Converters.Picture"
        Title="{Binding Title}"
        Width="850"
        Height="500"
        MinWidth="850"
        MinHeight="500"
        WindowStartupLocation="CenterScreen"
        TextOptions.TextFormattingMode="Display">

    <Window.Resources>
        <converters:ThemeImageConverter x:Key="themeConverter" />
        <converters:BooleanToVisibilityConverter x:Key="boolToVisConverter" />
        <picture:ResolutionLimitConverter x:Key="resolutionLimitConverter" />
    </Window.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <!-- Header -->
        <Grid Grid.Row="0" Grid.ColumnSpan="2">

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <TextBlock Text="{x:Static Properties:Resources.ManagePresetView_ManagePreset}" Style="{StaticResource windowTitle}" Margin="10,0,0,0" Grid.Row="0" />

            <Button Command="{Binding RelayCommand}" CommandParameter="LaunchHelp" Background="Transparent" BorderThickness="0" Grid.Row="0" HorizontalAlignment="Right" Margin="0,5,5,0" Padding="8,2">
                <Button.Content>
                    <Image Source="/Views/Images/question.png" Width="16" />
                </Button.Content>
            </Button>

        </Grid>

        <!-- Preset List -->
        <Grid Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Grid.Resources>

                <HierarchicalDataTemplate DataType="{x:Type loc:Preset}">
                    <StackPanel Orientation="Horizontal" >
                        <StackPanel.Resources>
                            <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsDefault}" Value="True" >
                                        <Setter Property="FontStyle" Value="Italic" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding IsDefault}" Value="False" >
                                        <Setter Property="FontStyle" Value="Normal" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </StackPanel.Resources>
                        <TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" />
                        <TextBlock Text="{x:Static Properties:Resources.Preset_Default}" Margin="10,0,0,0" Visibility="{Binding IsDefault, Converter={StaticResource boolToVisConverter}}"  />
                        <TextBlock Text="{x:Static Properties:Resources.Preset_NotAvailable}" Margin="10,0,0,0" Visibility="{Binding IsPresetDisabled, Converter={StaticResource boolToVisConverter}}" />
                    </StackPanel>
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate DataType="{x:Type loc:PresetDisplayCategory}" ItemsSource="{Binding Presets}">
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Text="{Binding Category}" FontSize="14" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </Grid.Resources>

            <Menu HorizontalAlignment="Right" Background="Transparent" VerticalAlignment="Bottom" Grid.Row="1" Margin="0,0,0,0" >
                <MenuItem>
                    <MenuItem.Header>
                        <StackPanel Height="27" Orientation="Horizontal">
                            <Image Source="{Binding Converter={StaticResource themeConverter}, ConverterParameter='Advanced.png'}" Width="20" Height="20" VerticalAlignment="Center" />
                            <TextBlock VerticalAlignment="Center"
                                       Text="{x:Static Properties:Resources.QueueView_Options}" Margin="5,0,0,0" />
                            <Path Height="5"
                                  Margin="2,2,0,0"
                                  Data="M 0 0 L 4 4 L 8 0 Z"
                                  Fill="{DynamicResource ButtonGlyphBrush}" />
                        </StackPanel>
                    </MenuItem.Header>

                    <MenuItem Command="{Binding RelayCommand}" CommandParameter="Import" Header="{x:Static Properties:Resources.ManagePresetView_Import}" />
                    <MenuItem Command="{Binding RelayCommand}" CommandParameter="Export" Header="{x:Static Properties:Resources.ManagePresetView_Export}" />
                    <MenuItem Command="{Binding RelayCommand}" CommandParameter="ExportUserPresets" Header="{x:Static Properties:Resources.ManagePresetView_ExportUserPresets}" />
                    <Separator />
                    <MenuItem Command="{Binding RelayCommand}" CommandParameter="SetDefault" Header="{x:Static Properties:Resources.ManagePresetView_SetDefault}" />

                    <MenuItem Command="{Binding RelayCommand}" CommandParameter="DeletePreset" Header="{x:Static Properties:Resources.PresetManagerView_Delete}" />
                    <Separator />
                    <MenuItem Command="{Binding RelayCommand}" CommandParameter="ResetBuiltInPresets" Header="{x:Static Properties:Resources.MainView_ResetPresets}" />
                    <MenuItem Command="{Binding RelayCommand}" CommandParameter="DeleteBuiltInPresets" Header="{x:Static Properties:Resources.ManagePresetView_DeleteBuiltIn}" />
                </MenuItem>
            </Menu>

            <TreeView x:Name="presetListTree" HorizontalAlignment="Stretch" AutomationProperties.Name="Presets List" ToolTip="{x:Static Properties:ResourcesTooltips.MainView_Presets}"
                              VerticalAlignment="Stretch" Grid.Row="2" Margin="10,0,0,10"
                              ItemsSource="{Binding PresetsCategories}" Width="300"
                              SelectedItemChanged="PresetListTree_OnSelectedItemChanged"
                              PreviewMouseRightButtonDown="PresetListTree_OnPreviewMouseRightButtonDown">

                <TreeView.ItemContainerStyle>
                    <Style BasedOn="{StaticResource {x:Type TreeViewItem}}" TargetType="TreeViewItem">
                        <Setter Property="HorizontalAlignment" Value="Stretch" />
                        <Setter Property="Padding" Value="4" />
                        <Setter Property="ToolTip" Value="{Binding Description}" />
                        <Setter Property="ToolTipService.InitialShowDelay" Value="1500"/>
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                        <EventSetter Event="TreeViewItem.Collapsed" Handler="PresetTreeviewItemCollapsed" />
                        <Style.Triggers>
                            <Trigger Property="HasItems" Value="True">
                                <Setter Property="Focusable" Value="false" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>

                <TreeView.ContextMenu>
                    <ContextMenu AutomationProperties.Name="Presets List Context Menu">
                        <MenuItem Header="{x:Static Properties:Resources.MainView_SetDefault}" Command="{Binding RelayCommand}" CommandParameter="SetDefault" />
                        <Separator />
                        <MenuItem Command="{Binding RelayCommand}" CommandParameter="DeletePreset" Header="{x:Static Properties:Resources.PresetManagerView_Delete}" />
                    </ContextMenu>
                </TreeView.ContextMenu>

                <TreeView.CommandBindings>
                    <CommandBinding Command="Delete" Executed="Delete_Execute" />
                </TreeView.CommandBindings>

            </TreeView>
        </Grid>

        <!-- Control Panel -->
        <Border BorderThickness="1,0,0,0" BorderBrush="LightGray" Grid.Column="1" Grid.Row="1"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="5" >

            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <!-- No Preset Selected -->
                <TextBlock Text="{x:Static Properties:Resources.ManagePresetView_NoPresetSelected}" HorizontalAlignment="Center" FontStyle="Italic" 
                       Visibility="{Binding IsPresetSelected, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />

                <!-- Main Content -->
                <Grid Visibility="{Binding IsPresetSelected, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <!-- Row 1 -->
                    <TextBlock Text="{x:Static Properties:Resources.ManagePresetView_PresetInfo}" FontSize="16" Margin="10,0,0,15" Grid.Row="0"  />

                    <!-- Row 2 -->
                    <StackPanel Orientation="Horizontal" Grid.Row="1" Margin="10,0,0,0" HorizontalAlignment="Stretch" >

                        <Button Content="Set Default" Margin="0,0,5,0" Visibility="{Binding SelectedPreset.IsDefault, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}"
                            Command="{Binding RelayCommand}" CommandParameter="SetCurrentPresetAsDefault" />

                        <Button AutomationProperties.Name="{x:Static Properties:Resources.PresetManagerView_Delete}"  
                            Command="{Binding RelayCommand}" CommandParameter="DeletePreset" 
                            Visibility="{Binding SelectedPreset.IsDefault, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" >
                            <Button.Content>
                                <StackPanel Orientation="Horizontal">
                                    <Image Width="16" Height="16" VerticalAlignment="Center" Margin="0,0,5,0"
                                               Source="{Binding Converter={StaticResource themeConverter}, ConverterParameter='Remove.png'}">
                                    </Image>
                                    <TextBlock Text="{x:Static Properties:Resources.PresetManagerView_Delete}" VerticalAlignment="Center" />
                                </StackPanel>

                            </Button.Content>
                        </Button>

                    </StackPanel>


                    <!-- Row 3 -->
                    <TextBlock Text="{x:Static Properties:Resources.ManagePresetView_RestrictedPreset}" HorizontalAlignment="Center" FontStyle="Italic" Grid.Row="2"
                               Margin="0,15,0,20"
                               Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}" />


                    <!--  Header  -->
                    <Grid Grid.Row="3" Margin="10,10,10,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="120" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <!--  Name  -->
                        <TextBlock Grid.Row="0" Grid.Column="0" Text="{x:Static Properties:Resources.AddPresetView_Name}" />
                        <TextBlock HorizontalAlignment="Stretch" Text="{Binding SelectedPreset.Name}" Grid.Column="1" TextTrimming="CharacterEllipsis" Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}}" />
                        <Grid Grid.Column="1"  Visibility="{Binding SelectedPreset.IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=True}"  HorizontalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBox HorizontalAlignment="Stretch" Text="{Binding PresetName, UpdateSourceTrigger=PropertyChanged}" KeyUp="PresetName_OnPreviewKeyDown" />
                            <Button Content="{x:Static Properties:Resources.Generic_Save}" Command="{Binding RelayCommand}" CommandParameter="RenamePreset" Margin="5,0,0,0" Grid.Column="1"  IsEnabled="{Binding IsPresetNameChanged}" />
                        </Grid>

                        <TextBlock Grid.Row="1" Grid.Column="0" Text="{x:Static Properties:Resources.AddPresetView_IsDefault}"  Margin="0,10,0,0"  />
                        <StackPanel Orientation="Horizontal"  Grid.Row="1" Grid.Column="1"  Margin="0,10,0,0" >
                            <TextBlock HorizontalAlignment="Stretch" Text="{x:Static Properties:Resources.Yes}" Visibility="{Binding SelectedPreset.IsDefault, Converter={StaticResource boolToVisConverter}, ConverterParameter=False}" />
                            <TextBlock HorizontalAlignment="Stretch" Text="{x:Static Properties:Resources.No}" Visibility="{Binding SelectedPreset.IsDefault, Converter={StaticResource boolToVisConverter}, ConverterParameter=True}" />
                        </StackPanel>

                        <!--  Description  -->
                        <TextBlock Grid.Row="2" Grid.Column="0" Margin="0,10,0,0" Text="{x:Static Properties:Resources.AddPresetView_Description}" VerticalAlignment="Top" />
                        <TextBox Grid.Row="2" Grid.Column="1" Margin="0,10,0,0" Text="{Binding SelectedPreset.Description, UpdateSourceTrigger=PropertyChanged}" Height="80" TextWrapping="WrapWithOverflow" VerticalContentAlignment="Top" 
                             Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}"  />
                        <TextBlock Grid.Row="2" Grid.Column="1" Margin="0,10,0,0" Text="{Binding SelectedPreset.Description, UpdateSourceTrigger=PropertyChanged}" TextWrapping="WrapWithOverflow" 
                             Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}"  />

                        <!-- Category -->
                        <TextBlock Grid.Row="3" Grid.Column="0" Margin="0,10,0,0" Text="{x:Static Properties:Resources.AddPresetView_Category}" />
                        <StackPanel Grid.Row="3" Grid.Column="1" Orientation="Vertical" Margin="0,10,0,0">

                            <ComboBox ItemsSource="{Binding UserPresetCategories}" SelectedItem="{Binding SelectedUserPresetCategory}" DisplayMemberPath="Category" Width="250" HorizontalAlignment="Left"
                                  Visibility="{Binding SelectedPreset.IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}"/>
                            <TextBlock Text="{Binding SelectedPreset.Category, UpdateSourceTrigger=PropertyChanged}" 
                                 Visibility="{Binding SelectedPreset.IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}" />
                        </StackPanel>

                        <!--  Settings  -->
                        <TextBlock Text="{x:Static Properties:Resources.PictureSettingsView_ResLimit}" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center"  Margin="0,15,0,5"
                               Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />
                        <ComboBox ItemsSource="{Binding ResolutionLimitModes, Converter={StaticResource resolutionLimitConverter}}"  
                          SelectedItem="{Binding SelectedPictureSettingsResLimitMode, Converter={StaticResource resolutionLimitConverter}}" 
                          Width="150" Grid.Row="4" Grid.Column="1" HorizontalAlignment="Left" Margin="0,15,0,5" 
                          AutomationProperties.Name="{x:Static Properties:Resources.PictureSettingsView_ResLimit}"
                          Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />


                        <!-- MAX Width and MAX Height -->
                        <StackPanel Grid.Row="5" Grid.Column="1" Orientation="Horizontal"  Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}">
                            <controls:NumberBox Number="{Binding CustomWidth, Mode=TwoWay}" UpdateBindingOnTextChange="True"
                                    Modulus="2" Minimum="0" Width="60"
                                    AutomationProperties.Name="{x:Static Properties:Resources.PictureSettingsView_MaxWidth}" Visibility="{Binding IsCustomMaxRes, Converter={StaticResource boolToVisConverter}}" />

                            <TextBlock Text="x" Visibility="{Binding IsCustomMaxRes, Converter={StaticResource boolToVisConverter}}" VerticalAlignment="Center" Margin="10,0,10,0" />
                            <controls:NumberBox Number="{Binding CustomHeight, Mode=TwoWay}" UpdateBindingOnTextChange="True"
                                    Modulus="2" Minimum="0" Width="60"
                                    AutomationProperties.Name="{x:Static Properties:Resources.PictureSettingsView_MaxHeight}" Visibility="{Binding IsCustomMaxRes, Converter={StaticResource boolToVisConverter}}" />
                        </StackPanel>

                        <TextBlock Text="Audio:" Grid.Row="6" VerticalAlignment="Center" Margin="0,15,0,0" Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}"  />
                        <Button Content="{x:Static Properties:Resources.AudioViewModel_ConfigureDefaults}" Grid.Row="6"  Grid.Column="1" HorizontalAlignment="Left" Margin="0,15,0,0" Padding="8,2"
                    Command="{Binding RelayCommand}" CommandParameter="EditAudioDefaults"  Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}"  />

                        <TextBlock Text="Subtitles:" Grid.Row="7" VerticalAlignment="Center" Margin="0,5,0,0" Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />
                        <Button Content="{x:Static Properties:Resources.SubtitlesViewModel_ConfigureDefaults}" Grid.Row="7"  Grid.Column="1" HorizontalAlignment="Left" Margin="0,5,0,0" Padding="8,2"
                    Command="{Binding RelayCommand}" CommandParameter="EditSubtitleDefaults" Visibility="{Binding IsBuildIn, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />

                    </Grid>

                </Grid>
            </Grid>


        </Border>

    </Grid>
</Window>
