<UserControl x:Class="HandBrakeWPF.Views.AudioView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Audio="clr-namespace:HandBrakeWPF.Converters.Audio"
             xmlns:Converters="clr-namespace:HandBrakeWPF.Converters"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:controls="clr-namespace:HandBrakeWPF.Controls"
             xmlns:Properties="clr-namespace:HandBrakeWPF.Properties"
             d:DesignHeight="500"
             d:DesignWidth="770"
             mc:Ignorable="d">

    <UserControl.Resources>
        <Converters:BooleanToVisibilityConverter x:Key="boolToVisConverter" />
        <Converters:BooleanToHiddenVisibilityConverter x:Key="boolToHiddenVisConverter" />
        <Audio:AudioEncoderConverter x:Key="audioEncoderConverter" />
        <Audio:AudioRateTypeConverter x:Key="audioRateTypeConverter" />
        <Audio:AudioMixdownConverter x:Key="audioMixdownConverter" />
        <Audio:AudioMixdownListConverter x:Key="audioMixdownListConverter" />
        <Converters:ThemeImageConverter x:Key="themeConverter" />
    </UserControl.Resources>

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

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

        <!-- Row 0 -->
        <Grid Grid.Row="0" Margin="10,5,10,0" Grid.Column="0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

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

                <Button x:Name="audioOptionsButton" FontWeight="Bold" Margin="0,0,10,0" Grid.Column="0" Click="AudioOptionsButton_OnClick">
                    <Button.Content>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Margin="0,0,5,0" Padding="0"
                                       VerticalAlignment="Center"
                                       Text="{x:Static Properties:Resources.Shared_AddTrack}" />

                            <Path Height="5" Margin="2,2,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"
                                  Data="M 0 0 L 4 4 L 8 0 Z"
                                  Fill="{DynamicResource ButtonGlyphBrush}" x:Name="dropdownArrowPreset" />
                        </StackPanel>
                    </Button.Content>

                    <Button.ContextMenu>
                        <ContextMenu x:Name="MainContextMenu" PlacementRectangle="{Binding RelativeSource={RelativeSource Self}}">
                            <MenuItem Header="{x:Static Properties:Resources.Shared_AddNewTrack}" Command="{Binding RelayCommand}" CommandParameter="Add" />
                            <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllRemaining}" Command="{Binding RelayCommand}" CommandParameter="AddAllRemaining" />
                            <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllForSelected}" Command="{Binding RelayCommand}" CommandParameter="AddAllRemainingForSelectedLanguages" />
                        </ContextMenu>
                    </Button.ContextMenu>
                </Button>

    
                <Button MinWidth="65" Grid.Column="1"
                    Margin="10,0,5,0" FontWeight="Bold"
                    Command="{Binding RelayCommand}" CommandParameter="ShowAudioDefaults"
                    ToolTip="{x:Static Properties:ResourcesTooltips.AudioView_AudioDefaults}"
                    Content="{x:Static Properties:Resources.AudioViewModel_ConfigureDefaults}" />

                <Button MinWidth="65" Grid.Column="2"
                        Command="{Binding RelayCommand}" CommandParameter="ReloadDefaults"
                        ToolTip="{x:Static Properties:ResourcesTooltips.BehavioursReload}"
                        Content="{x:Static Properties:Resources.Shared_ReloadDefaults}" />

                <Button MinWidth="65" Grid.Column="4"
                        Margin="0,0,0,0"
                        Command="{Binding RelayCommand}" CommandParameter="Clear"
                        Content="{x:Static Properties:Resources.Generic_Clear}" />
            </Grid>
        </Grid>

        <!-- Row 1 -->
        <ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Stretch"
                 Margin="10,10,10,10"
                 dd:DragDrop.DropHandler="{Binding}"
                 dd:DragDrop.IsDragSource="True"
                 dd:DragDrop.IsDropTarget="True"
                 ItemsSource="{Binding Task.AudioTracks}"
                 SelectionMode="Extended">

            <ListBox.InputBindings>
                <KeyBinding Key="Delete" Command="{Binding DeleteCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"  />
            </ListBox.InputBindings>

            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="Margin" Value="0,0,0,1" />
                </Style>
            </ListBox.ItemContainerStyle>

            <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="{x:Static Properties:Resources.Shared_AddNewTrack}" Command="{Binding RelayCommand}" CommandParameter="Add" />
                    <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllRemaining}" Command="{Binding RelayCommand}" CommandParameter="AddAllRemaining" />
                    <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllForSelected}" Command="{Binding RelayCommand}" CommandParameter="AddAllRemainingForSelectedLanguages" />
                    <Separator />
                    <MenuItem Header="{x:Static Properties:Resources.Generic_Clear}" Command="{Binding RelayCommand}" CommandParameter="Clear" />
                    <Separator />
                    <MenuItem Header="{x:Static Properties:Resources.Shared_ConfigureDefaultBehaviours}" Command="{Binding RelayCommand}" CommandParameter="ShowAudioDefaults" />
                    <Separator />
                    <MenuItem Header="{x:Static Properties:Resources.Shared_ExpandAll}" Command="{Binding RelayCommand}" CommandParameter="ExpandAllTracks"  />
                    <MenuItem Header="{x:Static Properties:Resources.Shared_CollapseAll}" Command="{Binding RelayCommand}" CommandParameter="CollapseAllTracks"  />
                </ContextMenu>
            </ListBox.ContextMenu>

            <ListBox.ItemTemplate>
                <DataTemplate>

                    <Grid HorizontalAlignment="Stretch">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <!--  Settings  -->
                        <Grid Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" Margin="0,5,0,5">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

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

                            <Image Width="16" Source="{Binding Converter={StaticResource themeConverter}, ConverterParameter='Reorder.png'}" />

                            <!--  Row 1  -->
                            <ComboBox Grid.Column="1" Width="160" Margin="5,0,5,0" HorizontalAlignment="Stretch"
                                      ItemsSource="{Binding DataContext.SourceTracks, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                      SelectedItem="{Binding ScannedTrack}" />

                            <TextBlock Grid.Column="2" VerticalAlignment="Center" FontWeight="Bold" Text="{x:Static Properties:Resources.AudioView_Codec}" />
                            <ComboBox Grid.Column="3" Width="105" Margin="5,0,5,0" DisplayMemberPath="DisplayName">
                                <ComboBox.SelectedItem>
                                    <MultiBinding Converter="{StaticResource audioEncoderConverter}">
                                        <Binding Path="Encoder" />
                                    </MultiBinding>
                                </ComboBox.SelectedItem>
                                <ComboBox.ItemsSource>
                                    <MultiBinding Converter="{StaticResource audioEncoderConverter}">
                                        <Binding Path="DataContext.AudioEncoders" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                                        <Binding Path="DataContext.Task" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" />
                                        <Binding Path="ScannedTrack" />
                                    </MultiBinding>
                                </ComboBox.ItemsSource>
                            </ComboBox>

                            <ComboBox Grid.Column="4" Width="65" HorizontalAlignment="Stretch"
                                      ItemsSource="{Binding AudioEncoderRateTypes, Converter={StaticResource audioRateTypeConverter}}"
                                      SelectedItem="{Binding EncoderRateType, Converter={StaticResource audioRateTypeConverter}}"
                                      Visibility="{Binding IsRateTypeVisible, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}" />

                            <ComboBox Grid.Column="5" Width="55" Margin="5,0,5,0" ItemsSource="{Binding EncoderQualityValues}"
                                      SelectedItem="{Binding Quality}" ToolTip="{x:Static Properties:ResourcesTooltips.AudioView_Quality}"
                                      Visibility="{Binding IsQualityVisible, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}" />

                            <ComboBox Grid.Column="5" Width="55" Margin="5,0,5,0" ItemsSource="{Binding Bitrates}"
                                      SelectedItem="{Binding Bitrate}"
                                      Visibility="{Binding IsBitrateVisible, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}" />

                            <TextBlock Grid.Row="0" Grid.Column="6" VerticalAlignment="Center" FontWeight="Bold" Text="{x:Static Properties:Resources.AudioView_Mixdown}"
                                       Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />
                            <ComboBox Grid.Row="0" Grid.Column="7" Width="120" Margin="5,0,5,0" HorizontalAlignment="Stretch"
                                      ItemsSource="{Binding TrackReference,  Converter={StaticResource audioMixdownListConverter}}"
                                      SelectedItem="{Binding MixDown, Converter={StaticResource audioMixdownConverter}}"
                                      DisplayMemberPath="DisplayName"
                                      Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />

                            <TextBlock VerticalAlignment="Center" FontWeight="Bold" Text="{x:Static Properties:Resources.AudioView_Samplerate}" Grid.Column="8"
                                       Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />
                            <ComboBox Width="70" Margin="5,0,5,0" Grid.Column="9"
                                      ItemsSource="{Binding DataContext.SampleRates, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                      SelectedItem="{Binding SampleRateDisplayValue}"
                                      Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />


                            <TextBlock  VerticalAlignment="Center" FontWeight="Bold" Text="{x:Static Properties:Resources.AudioView_Gain}" Grid.Column="10"
                                        Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />

                            <controls:NumberBox Name="gainNumericCtl" Width="45" Margin="5,0,5,0" HorizontalAlignment="Left" Grid.Column="11"  VerticalAlignment="Center"
                                                Minimum="-20" Modulus="1" Maximum="20" Number="{Binding Gain, Mode=TwoWay}" UpdateBindingOnTextChange="True" ShowIncrementButtons="True" AllowEmpty="False"
                                                Visibility="{Binding IsPassthru, Converter={StaticResource boolToHiddenVisConverter}, ConverterParameter=true}"/>

                            <ToggleButton Name="advancedOptionsToggle" VerticalAlignment="Center" Grid.Column="12" Grid.Row="0" Grid.ColumnSpan="2" Margin="5,0,0,0"
                                           Padding="3,0" HorizontalAlignment="Left" Background="Transparent" IsChecked="{Binding IsExpandedTrackView}" >
                                <ToggleButton.Style>
                                    <Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
                                        <Setter Property="Content" Value="Show"/>
                                        <Setter Property="FontWeight" Value="Bold" />
                                        <Style.Triggers>
                                            <Trigger Property="IsChecked" Value="True">
                                                <Setter Property="Content" Value="˄"/>
                                            </Trigger>
                                            <Trigger Property="IsChecked" Value="False">
                                                <Setter Property="Content" Value="˅" />
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </ToggleButton.Style>
                            </ToggleButton>

                        </Grid>

                        <!--  Delete  -->
                        <StackPanel Grid.Column="2" Grid.Row="0" VerticalAlignment="Center">
                            <Image Width="16" Height="16" Margin="10,0,10,0" Source="Images/close64.png">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseDown">
                                        <i:InvokeCommandAction Command="{Binding DataContext.RemoveCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Image>
                        </StackPanel>

                        <!-- Hidden Row -->
                        <Grid Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch" Visibility="{Binding IsExpandedTrackView, Converter={StaticResource boolToVisConverter}}">
                            <StackPanel Orientation="Horizontal" Margin="22,5,0,5" >

                                <TextBlock VerticalAlignment="Center" FontWeight="Bold" Text="{x:Static Properties:Resources.AudioView_DRC}"
                                       Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />

                                <controls:NumberBox Name="drcNumericCtl" Width="45" Margin="5,0,5,0" HorizontalAlignment="Left"
                                                    Minimum="0" Modulus="0.1" Maximum="4" Number="{Binding DRC, Mode=TwoWay}" UpdateBindingOnTextChange="True" ShowIncrementButtons="True" AllowEmpty="False"
                                                    Visibility="{Binding IsPassthru, Converter={StaticResource boolToHiddenVisConverter}, ConverterParameter=true}"/>
       
                                <TextBlock Text="{x:Static Properties:Resources.AudioView_TrackName}" VerticalAlignment="Center" FontWeight="Bold" Margin="5,0,5,0" />
                                <TextBox Text="{Binding TrackName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="250" />

                            </StackPanel>
                        </Grid>
                    </Grid>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
</UserControl>
