<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:NumericUpDown="clr-namespace:EagleBoost.Wpf.Presentation.Controls.NumericUpDown;assembly=EagleBoost.Wpf.Presentation"
             xmlns:dropButton="clr-namespace:HandBrakeWPF.Controls.DropButton"
             xmlns:splitButton="clr-namespace:HandBrakeWPF.Controls.SplitButton"
             xmlns:controls="clr-namespace:HandBrakeWPF.Controls"
             xmlns:helpers="clr-namespace:HandBrakeWPF.Helpers"
             d:DesignHeight="170"
             d:DesignWidth="616"
             mc:Ignorable="d"
             x:Name="audioTab">

    <UserControl.Resources>
        <Conveters:EnumComboConverter x:Key="enumComboConverter" />
        <Conveters:BooleanToVisibilityConverter x:Key="boolToVisConverter" />
        <Conveters:BooleanToHiddenVisibilityConverter x:Key="boolToHiddenVisConverter" />
        <Audio:AudioBitrateConverter x:Key="audioBitrateConverter" />
        <Audio:AudioEncoderConverter x:Key="audioEncoderConverter" />
        <Audio:AudioBehaviourConverter x:Key="audioBehaviourConverter" />
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <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}" />

        <Grid Grid.Row="0" Grid.Column="1" Margin="0,5,10,0">
            <Border cal:Message.Attach="[Event MouseDown] = [Action ShowAudioDefaults]">
                <StackPanel Orientation="Horizontal">
                    <Image Source="../Views/Images/Advanced.png" Width="20"   />
                    <TextBlock Text="{Binding SwitchDisplayTitle}" VerticalAlignment="Center" Margin="5,0,0,0" />
                </StackPanel>
            </Border>
        </Grid>

        <!-- Row 1 -->

        <Grid Grid.Row="1" Margin="10,5,10,0" Grid.Column="0"
              Visibility="{Binding ShowAudioDefaultsPanel, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <StackPanel Orientation="Horizontal" Grid.Row="0">
                <splitButton:SplitMenuButton Content="Add Track" cal:Message.Attach="[Event Click] = [Action Add]"  FontWeight="Bold" Margin="0,0,10,0">
                    <splitButton:SplitMenuButton.ItemSource>
                        <MenuItem Header="Add New Track" cal:Message.Attach="[Event Click] = [Action Add]" />
                        <MenuItem Header="Add All Remaining Tracks" cal:Message.Attach="[Event Click] = [Action AddAllRemaining]" />
                        <MenuItem Header="Add All Remaining Selected Languages" cal:Message.Attach="[Event Click] = [Action AddAllRemainingForSelectedLanguages]" />
                    </splitButton:SplitMenuButton.ItemSource>
                </splitButton:SplitMenuButton>

                <Button MinWidth="65"
                    Margin="0,0,10,0"
                    cal:Message.Attach="[Event Click] = [Action Clear]"
                    Content="Clear" />


            </StackPanel>
        </Grid>

        <!-- Row 2 -->

        <ListBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
                 Visibility="{Binding ShowAudioDefaultsPanel, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}"
                 Margin="10,10,10,10"
                 dd:DragDrop.DropHandler="{Binding}"
                 dd:DragDrop.IsDragSource="True"
                 dd:DragDrop.IsDropTarget="True"
                 ItemsSource="{Binding Task.AudioTracks}"
                 SelectionMode="Extended">
            <ListBox.Style>
                <Style TargetType="ListBox">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding UseSystemColours}" Value="False">
                            <Setter Property="Background" Value="LightGray" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.Style>

            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="Margin" Value="0,0,0,1" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding DataContext.UseSystemColours, ElementName=audioTab}" Value="False">
                            <Setter Property="Background" Value="WhiteSmoke" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>

            <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Add New Track" cal:Message.Attach="[Event Click] = [Action Add]" />
                    <MenuItem Header="Add All Remaining Tracks" cal:Message.Attach="[Event Click] = [Action AddAllRemaining]" />
                    <MenuItem Header="Add All Remaining Selected Languages" cal:Message.Attach="[Event Click] = [Action AddAllRemainingForSelectedLanguages]" />
                    <Separator />
                    <MenuItem Header="Clear All" cal:Message.Attach="[Event Click] = [Action Clear]" />
                    <Separator />
                    <MenuItem Header="Configure Default Behaviours" cal:Message.Attach="[Event Click] = [Action SetDefaultBehaviour]" />
                </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="16"
                               Height="16"
                               Margin="10,0,10,0"
                               Source="Images/Movies.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  -->
                            <TextBlock Grid.Column="0" VerticalAlignment="Center" FontWeight="Bold" Text="Source" />
                            <ComboBox Grid.Column="1" Height="22" Width="100" 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="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>

                            <TextBlock Grid.Column="4" VerticalAlignment="Center" FontWeight="Bold" Text="Bitrate"
                                       Visibility="{Binding CannotSetBitrate, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />
                            <ComboBox Grid.Column="5" Width="55" Height="22" Margin="5,0,5,0" ItemsSource="{Binding TrackReference, Converter={StaticResource audioBitrateConverter}}"
                                      SelectedItem="{Binding Bitrate}"
                                      Visibility="{Binding CannotSetBitrate, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />


                            <!--  Row 2  -->
                            <TextBlock Grid.Row="0" Grid.Column="6" VerticalAlignment="Center" FontWeight="Bold" Text="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 DataContext.AudioMixdowns, Converter={StaticResource enumComboConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                      SelectedItem="{Binding MixDown, Converter={StaticResource enumComboConverter}}"
                                      Visibility="{Binding IsPassthru, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />



                            <ToggleButton Name="advancedOptionsToggle" VerticalAlignment="Center" Grid.Column="8" Grid.Row="0" Grid.ColumnSpan="2"
                                           Padding="3,0" Height="22" HorizontalAlignment="Left" >
                                <ToggleButton.Style>
                                    <Style TargetType="{x:Type ToggleButton}">
                                        <Setter Property="Content" Value="Show"/>
                                        <Style.Triggers>
                                            <Trigger Property="IsChecked" Value="True">
                                                <Setter Property="Content" Value="Hide"/>
                                            </Trigger>
                                            <Trigger Property="IsChecked" Value="False">
                                                <Setter Property="Content" Value="Show"/>
                                            </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="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="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="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="Track Name:" 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>

        <StackPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="10,10,10,10"
              Visibility="{Binding ShowAudioDefaultsPanel, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}">

            <Grid Margin="5,0,0,0" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

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

                <!-- Audio -->
                <TextBlock Text="Track Selection Behaviour:" Grid.Column="0" Grid.Row="0" Margin="0,0,5,0" HorizontalAlignment="Left"  VerticalAlignment="Center" />
                <ComboBox Name="autoAudioMode" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left"
                          ItemsSource="{Binding AudioBehaviourModeList, Converter={StaticResource audioBehaviourConverter}}" 
                          SelectedItem="{Binding AudioBehaviours.SelectedBehaviour, Converter={StaticResource audioBehaviourConverter}}" Width="210" Margin="0,0,5,0" />
            </Grid>

            <TextBlock Text="Choose Languages:" Margin="5,15,0,5" />

            <Grid Margin="20,5,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <StackPanel Orientation="Horizontal">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <StackPanel Orientation="Vertical" Grid.Column="0">
                            <TextBlock Text="Available Languages:" Margin="0,0,0,5"/>
                            <ListBox Name="availableLanguages" ItemsSource="{Binding AvailableLanguages}" 
                                     helpers:ListBoxHelper.SelectedItems="{Binding SelectedAvailableToMove}" 
                                             SelectionMode="Extended" Width="140" Height="120" />
                        </StackPanel>

                        <DockPanel Grid.Column="1" Margin="10,0,10,0">
                            <StackPanel Orientation="Vertical" VerticalAlignment="Center">
                                <Button Content="Move Right"  VerticalAlignment="Center" Margin="0,0,0,5" cal:Message.Attach="[Event Click] = [Action LanguageMoveRight]" MaxWidth="90" />
                                <Button Content="Move Left"  VerticalAlignment="Center" Margin="0,0,0,5" cal:Message.Attach="[Event Click] = [Action LanguageMoveLeft]" MaxWidth="90" />
                                <Button Content="Clear" VerticalAlignment="Center" Margin="0,0,0,5" cal:Message.Attach="[Event Click] = [Action LanguageClearAll]" Width="80"/>
                            </StackPanel>
                        </DockPanel>

                        <StackPanel Orientation="Vertical" Grid.Column="2">
                            <TextBlock Text="Chosen Languages:"  Margin="0,0,0,5"/>
                            <ListBox Name="selectedLangauges" ItemsSource="{Binding AudioBehaviours.SelectedLangauges}" 
                                     helpers:ListBoxHelper.SelectedItems="{Binding SelectedLangaugesToMove}" 
                                             SelectionMode="Extended" Width="140" Height="120"
                                             dd:DragDrop.DropHandler="{Binding}"
                                             dd:DragDrop.IsDragSource="True"
                                             dd:DragDrop.IsDropTarget="True"/>
                        </StackPanel>
                    </Grid>
                </StackPanel>
            </Grid>

            <TextBlock Text="Automatic Passthru Behaviours:" FontWeight="Bold" Margin="0,20,0,5" />
            <StackPanel Margin="10,5,0,0"
                        Orientation="Horizontal">
                <TextBlock Margin="5,0,5,0" Text="Allow passthrough of:" VerticalAlignment="Center" />
                <CheckBox Margin="0,0,5,0"
                          Content="MP3" VerticalAlignment="Center"    
                          IsChecked="{Binding Task.AllowedPassthruOptions.AudioAllowMP3Pass}" />
                <CheckBox Margin="0,0,5,0"
                          Content="AAC" VerticalAlignment="Center"
                          IsChecked="{Binding Task.AllowedPassthruOptions.AudioAllowAACPass}" />
                <CheckBox Margin="0,0,5,0"
                          Content="AC3" VerticalAlignment="Center"
                          IsChecked="{Binding Task.AllowedPassthruOptions.AudioAllowAC3Pass}" />
                <CheckBox Margin="0,0,5,0"
                          Content="DTS" VerticalAlignment="Center"
                          IsChecked="{Binding Task.AllowedPassthruOptions.AudioAllowDTSPass}" />
                <CheckBox Margin="0,0,5,0"
                          Content="DTSHD" VerticalAlignment="Center"
                          IsChecked="{Binding Task.AllowedPassthruOptions.AudioAllowDTSHDPass}" />
                
                <TextBlock Margin="15,0,5,0" Text="Fallback Encoder:" />

                <ComboBox VerticalAlignment="Center" Width="100" Height="22" Margin="10,0,5,0">
                    <ComboBox.SelectedItem>
                        <MultiBinding Converter="{StaticResource audioEncoderConverter}">
                            <Binding Path="Task.AllowedPassthruOptions.AudioEncoderFallback" />
                        </MultiBinding>
                    </ComboBox.SelectedItem>
                    <ComboBox.ItemsSource>
                        <MultiBinding Converter="{StaticResource audioEncoderConverter}" ConverterParameter="True">
                            <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>

            </StackPanel>

        </StackPanel>


    </Grid>
</UserControl>
