<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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:controls="clr-namespace:HandBrakeWPF.Controls"
             xmlns:Properties="clr-namespace:HandBrakeWPF.Properties"
             xmlns:behaviours="clr-namespace:HandBrakeWPF.Behaviours"
             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 -->
        <ListView 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"
                  Background="Transparent"
                  ItemsSource="{Binding Task.AudioTracks}"
                  BorderBrush="{StaticResource Ui.ContrastLight}"
                  SelectionMode="Extended">

            <ListView.Resources>
                <Style TargetType="{x:Type GridViewColumnHeader}" >
                    <Setter Property="Template" >
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                                <Border BorderBrush="{StaticResource Ui.ContrastLight}" BorderThickness="0,0,0,1" Margin="0,0,0,5">
                                    <TextBlock Text="{TemplateBinding Content}" TextAlignment="Left" Margin="0,2,0,3" Style="{StaticResource sectionHeader}"
                                               TextTrimming="CharacterEllipsis"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.Resources>

            <ListView.View>
                <GridView AllowsColumnReorder="False">
                    <GridViewColumn Header="" Width="28" />
                    <GridViewColumn Header="{x:Static Properties:Resources.AudioView_Source}" Width="250" />
                    <GridViewColumn Header="{x:Static Properties:Resources.AudioView_Codec}" Width="118" />
                    <GridViewColumn Header="{x:Static Properties:Resources.AudioView_Quality}" Width="142" />
                    <GridViewColumn Header="{x:Static Properties:Resources.AudioView_Mixdown}" Width="132" />
                    <GridViewColumn Header="{x:Static Properties:Resources.AudioView_Samplerate}" Width="85" />
                    <GridViewColumn Header="{x:Static Properties:Resources.AudioView_Gain}" Width="55" />
                    <GridViewColumn Header="{x:Static Properties:Resources.AudioView_DRC}" Width="82" />
                </GridView>
            </ListView.View>

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

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

            <ListView.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>
            </ListView.ContextMenu>

            <ListView.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="16" />
                                <ColumnDefinition Width="250"/> <!-- Source -->
                                <ColumnDefinition Width="120" /> <!-- Encoder -->
                                <ColumnDefinition Width="140" /> <!-- Quality -->
                                <ColumnDefinition Width="130" /> <!-- Mixdown -->
                                <ColumnDefinition Width="85" /> <!-- Samplerate -->
                                <ColumnDefinition Width="55" /> <!-- Gain -->
                                <ColumnDefinition Width="55" /> <!-- DRC -->
                                <ColumnDefinition Width="20" /> <!-- Adv -->
                            </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"  Margin="5,0,5,0" HorizontalAlignment="Stretch" ToolTip="{Binding ScannedTrack.Description}"
                                      AutomationProperties.Name="{x:Static Properties:Resources.AudioView_Source}"
                                      ItemsSource="{Binding DataContext.SourceTracks, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                      SelectedItem="{Binding ScannedTrack}" />

                            <ComboBox Grid.Column="2" Margin="5,0,5,0" DisplayMemberPath="DisplayName" HorizontalAlignment="Stretch"
                                      AutomationProperties.Name="{x:Static Properties:Resources.AudioView_Codec}" >
                                <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>

                            <StackPanel Orientation="Horizontal" Grid.Column="3" HorizontalAlignment="Stretch">
                                <ComboBox HorizontalAlignment="Stretch"
                                          AutomationProperties.Name="{x:Static Properties:Resources.AudioView_QualityMode}"
                                          ItemsSource="{Binding AudioEncoderRateTypes, Converter={StaticResource audioRateTypeConverter}}"
                                          SelectedItem="{Binding EncoderRateType, Converter={StaticResource audioRateTypeConverter}}"
                                          Visibility="{Binding IsRateTypeVisible, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}" />

                                <ComboBox Margin="5,0,5,0" MinWidth="65" ItemsSource="{Binding EncoderQualityValues}" HorizontalAlignment="Stretch"
                                          AutomationProperties.Name="{x:Static Properties:Resources.AudioView_AudioQuality}"
                                          SelectedItem="{Binding Quality}" ToolTip="{x:Static Properties:ResourcesTooltips.AudioView_Quality}"
                                          Visibility="{Binding IsQualityVisible, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}" />

                                <ComboBox Margin="5,0,5,0" MinWidth="65" ItemsSource="{Binding Bitrates}" HorizontalAlignment="Stretch"
                                          AutomationProperties.Name="{x:Static Properties:Resources.AudioView_BitrateSelection}"
                                          SelectedItem="{Binding Bitrate}"
                                          Visibility="{Binding IsBitrateVisible, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}" />
                            </StackPanel>
                      

                            <ComboBox Grid.Row="0" Grid.Column="4" Margin="5,0,5,0" HorizontalAlignment="Stretch"
                                      AutomationProperties.Name="{x:Static Properties:Resources.AudioView_Mixdown}"
                                      ItemsSource="{Binding TrackReference,  Converter={StaticResource audioMixdownListConverter}}"
                                      SelectedItem="{Binding MixDown, Converter={StaticResource audioMixdownConverter}}"
                                      DisplayMemberPath="DisplayName"
                                      Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />

                            <ComboBox Margin="5,0,5,0" Grid.Column="5"
                                      AutomationProperties.Name="{x:Static Properties:Resources.AudioView_Samplerate}"
                                      ItemsSource="{Binding DataContext.SampleRates, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                      SelectedItem="{Binding SampleRateDisplayValue}"
                                      Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />

                            <controls:NumberBox x:Name="gainNumericCtl" Margin="5,0,5,0" HorizontalAlignment="Stretch" Grid.Column="6"  VerticalAlignment="Center"
                                                AutomationProperties.Name="{x:Static Properties:Resources.AudioView_Gain}"
                                                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}"/>

                            <controls:NumberBox x:Name="drcNumericCtl" Margin="5,0,5,0" HorizontalAlignment="Stretch" Grid.Column="7"
                                                AutomationProperties.Name="{x:Static Properties:Resources.AudioView_DRC}"
                                                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}"/>

                            <ToggleButton Name="advancedOptionsToggle" VerticalAlignment="Center" Grid.Column="8" Grid.Row="0" Margin="5,0,0,0" HorizontalAlignment="Stretch"
                                           Padding="3,0" 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">

                            <Button Style="{StaticResource TransparentButtonStyle}" Command="{Binding DataContext.RemoveCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"          
                                    CommandParameter="{Binding}">
                                <Button.Content>
                                    <Image Width="16" Height="16" Margin="10,0,10,0" Source="Images/close64.png" />
                                </Button.Content>
                            </Button>                                                        
                        </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 Text="{x:Static Properties:Resources.AudioView_TrackName}" Style="{StaticResource sectionHeader}"  VerticalAlignment="Center" Margin="0,0,5,0" />
                                <TextBox Text="{Binding TrackName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="250" behaviours:TextBoxBehavior.TripleClickSelectAll="True"
                                         AutomationProperties.Name="{x:Static Properties:Resources.AudioView_AudioTrackName}"/>                                                                
                            </StackPanel>
                        </Grid>
                    </Grid>

                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </Grid>
</UserControl>
