<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:cal="http://www.caliburnproject.org"
             xmlns:Conveters="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/expression/2010/interactivity"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:splitButton="clr-namespace:HandBrakeWPF.Controls.SplitButton"
             xmlns:controls="clr-namespace:HandBrakeWPF.Controls"
             xmlns:Properties="clr-namespace:HandBrakeWPF.Properties"
             d:DesignHeight="500"
             d:DesignWidth="770"
             mc:Ignorable="d"
             x:Name="audioTab">

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

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

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


        <!-- Row 0 -->
        <TextBlock Grid.Row="0"
                   Margin="10,5,0,0"
                   FontWeight="Bold"
                   VerticalAlignment="Top"
                   Text="{Binding PanelTitle}" />

        <!-- Row 1 -->
        <Grid Grid.Row="1" 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="*" />
                    <ColumnDefinition Width="Auto" />
                    <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 GlyphBrush}" 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}" cal:Message.Attach="[Event Click] = [Action Add]" />
                            <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllRemaining}" cal:Message.Attach="[Event Click] = [Action AddAllRemaining]" />
                            <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllForSelected}" cal:Message.Attach="[Event Click] = [Action AddAllRemainingForSelectedLanguages]" />
                        </ContextMenu>
                    </Button.ContextMenu>
                </Button>

                <Button MinWidth="65" Grid.Column="1"
                    Margin="0,0,10,0"
                    cal:Message.Attach="[Event Click] = [Action Clear]"
                    Content="{x:Static Properties:Resources.Generic_Clear}" />

                <Button MinWidth="65" Grid.Column="3"
                    Margin="0,0,10,0"
                    cal:Message.Attach="[Event Click] = [Action ShowAudioDefaults]"
                    Content="{Binding SwitchDisplayTitle}" />

                <Button MinWidth="65" Grid.Column="4"
                    cal:Message.Attach="[Event Click] = [Action ReloadDefaults]"
                    Content="{x:Static Properties:Resources.Shared_ReloadDefaults}" />


            </Grid>
        </Grid>

        <!-- Row 2 -->
        <ListBox Grid.Row="3" 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.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}" cal:Message.Attach="[Event Click] = [Action Add]" />
                    <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllRemaining}" cal:Message.Attach="[Event Click] = [Action AddAllRemaining]" />
                    <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllForSelected}" cal:Message.Attach="[Event Click] = [Action AddAllRemainingForSelectedLanguages]" />
                    <Separator />
                    <MenuItem Header="{x:Static Properties:Resources.Generic_Clear}" cal:Message.Attach="[Event Click] = [Action Clear]" />
                    <Separator />
                    <MenuItem Header="{x:Static Properties:Resources.Shared_ConfigureDefaultBehaviours}" cal:Message.Attach="[Event Click] = [Action ShowAudioDefaults]" />
                </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>

                        <!--  Marker  -->
                        <Image Grid.Column="0" Grid.Row="0"
                               Width="20"
                               Height="20"
                               Margin="5,0,5,0"
                               Source="Images/Movies_small.png" />

                        <!--  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" />
                            </Grid.ColumnDefinitions>

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

                            <!--  Row 1  -->
                            <ComboBox Grid.Column="1" Height="22" Width="130" 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="100" Height="22" Margin="5,0,5,0">
                                <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}}" />
                                    </MultiBinding>
                                </ComboBox.ItemsSource>
                            </ComboBox>

                            <ComboBox Grid.Column="4" Height="22" 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" Height="22" Margin="5,0,5,0" ItemsSource="{Binding EncoderQualityValues}"
                                      SelectedItem="{Binding Quality}"
                                      Visibility="{Binding IsQualityVisible, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}" />

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

                            <!--  Row 2  -->
                            <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" Height="22" 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}" />


                            <ToggleButton Name="advancedOptionsToggle" VerticalAlignment="Center" Grid.Column="8" Grid.Row="0" Grid.ColumnSpan="2" Margin="5,0,0,0"
                                           Padding="3,0" Height="22" HorizontalAlignment="Left" >
                                <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/delete.png">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseDown">
                                        <cal:ActionMessage MethodName="Remove">
                                            <cal:Parameter Value="{Binding}" />
                                        </cal:ActionMessage>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Image>
                        </StackPanel>

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

                                <TextBlock VerticalAlignment="Center" FontWeight="Bold" Text="{x:Static Properties:Resources.AudioView_Samplerate}"
                                       Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />
                                <ComboBox Width="70" Height="22" Margin="5,0,5,0"
                                      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_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  VerticalAlignment="Center" FontWeight="Bold" Text="{x:Static Properties:Resources.AudioView_Gain}"
                                       Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />

                                <controls:NumberBox Name="gainNumericCtl" Width="45" Margin="5,0,5,0" HorizontalAlignment="Left"
                                                    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}"/>

                                <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="138" Height="22" />

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

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

    </Grid>
</UserControl>
