<UserControl x:Class="HandBrakeWPF.Views.SubtitlesView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             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"
             xmlns:subtitles="clr-namespace:HandBrakeWPF.Converters.Subtitles"
             d:DesignHeight="350"
             d:DesignWidth="500"
             mc:Ignorable="d">
    <UserControl.Resources>
        <Converters:BooleanToVisibilityConverter x:Key="booleanToVisConverter" />
        <Converters:BooleanConverter x:Key="booleanConverter" />
        <subtitles:CanBurnSubtitleConverter x:Key="canBurnSubtitleConverter" />
        <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.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Button x:Name="subtitleOptionsButton" FontWeight="Bold" Margin="0,0,10,0" Grid.Column="0" Click="SubtitleOptionsButton_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="AddUser" />
                        <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllRemaining}" Command="{Binding RelayCommand}" CommandParameter="AddAllRemainingUser" />
                        <MenuItem Header="{x:Static Properties:Resources.SubtitleView_AddRemainingCC}" Command="{Binding RelayCommand}" CommandParameter="AddAllClosedCaptionsUser" />
                        <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllForSelected}" Command="{Binding RelayCommand}" CommandParameter="AddAllRemainingForSelectedLanguagesUser" />
                        <Separator/>
                        <MenuItem Header="{x:Static Properties:Resources.SubtitlesView_ImportSubtitle}" Command="{Binding RelayCommand}" CommandParameter="ImportSubtitle" />
                    </ContextMenu>
                </Button.ContextMenu>
            </Button>


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

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

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


        <!-- Row 0 -->
        <ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
                 Margin="10,10,10,10" x:Name="subtitleListBox"
                 dd:DragDrop.DropHandler="{Binding}"
                 dd:DragDrop.IsDragSource="True"
                 dd:DragDrop.IsDropTarget="True"
                 ItemsSource="{Binding Task.SubtitleTracks}"
                 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="AddUser" />
                    <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllRemaining}" Command="{Binding RelayCommand}" CommandParameter="AddAllRemainingUser" />
                    <MenuItem Header="{x:Static Properties:Resources.SubtitleView_AddAllCC}" Command="{Binding RelayCommand}" CommandParameter="AddAllClosedCaptionsUser" />
                    <MenuItem Header="{x:Static Properties:Resources.Shared_AddAllForSelected}" Command="{Binding RelayCommand}" CommandParameter="AddAllRemainingForSelectedLanguagesUser" />    
                    <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="ShowSubtitleDefaultsPanel" />
                </ContextMenu>
            </ListBox.ContextMenu>

            <ListBox.ItemTemplate>
                <DataTemplate>

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

                        <!--  Standard Subtitle Settings  -->
                        <Grid Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,8,0,8"
                              Visibility="{Binding IsSrtSubtitle, Converter={StaticResource booleanToVisConverter}, ConverterParameter=true}">
                            <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" />
                            </Grid.ColumnDefinitions>

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

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

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

                            <CheckBox Grid.Column="3" Margin="5,1,5,0" IsEnabled="{Binding CanBeForced}" IsChecked="{Binding Forced}" 
                                      Content="{x:Static Properties:Resources.SubtitlesView_ForcedOnly}" FontWeight="Bold" />

                            <CheckBox Grid.Column="4" Margin="5,1,5,0" IsChecked="{Binding Burned}" x:Name="burnInCheckbox" Content="{x:Static Properties:Resources.SubtitlesView_BurnIn}" FontWeight="Bold"
                                      ToolTip="{x:Static Properties:ResourcesTooltips.SubtitlesView_BurnIn}">
                                <CheckBox.IsEnabled>
                                    <MultiBinding Converter="{StaticResource canBurnSubtitleConverter}">
                                        <Binding Path="CanBeBurned" />
                                        <Binding Path="SubtitleType" />
                                        <Binding ElementName="subtitleListBox" Path="DataContext.Task"  />
                                    </MultiBinding>
                                </CheckBox.IsEnabled>
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <i:InvokeCommandAction Command="{Binding DataContext.SetBurnedToFalseForAllExceptCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>

                            <CheckBox Grid.Column="5" Margin="5,1,5,0" IsChecked="{Binding Default}" Content="{x:Static Properties:Resources.SubtitlesView_Default}" FontWeight="Bold"
                                      IsEnabled="{Binding DataContext.IsBurnableOnly, Converter={StaticResource booleanConverter}, ConverterParameter=true, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <i:InvokeCommandAction Command="{Binding DataContext.SelectDefaultTrackCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>

                            <StackPanel Orientation="Horizontal" Grid.Column="6" Margin="10,0,0,0">
                                <TextBlock Text="Name: " FontWeight="Bold" Margin="0,0,5,0" VerticalAlignment="Center" />
                                <TextBox x:Name="TrackName" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Width="250" />
                            </StackPanel>
                        </Grid>

                        <!--  SRT Subtitle Settings  -->
                        <Grid Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,8,0,8"
                              Visibility="{Binding IsSrtSubtitle, Converter={StaticResource booleanToVisConverter}, ConverterParameter=false}" >
                            <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" />
                            </Grid.ColumnDefinitions>

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

                            <!--  Row 1  -->
                            <TextBlock Grid.Column="0" VerticalAlignment="Center" FontWeight="Bold" Text="Source" Margin="10,0,0,0" />
                            <TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{Binding SrtFileName}" Margin="5,0,5,0" Width="75" TextWrapping="NoWrap" TextTrimming="WordEllipsis"
                                       Visibility="{Binding IsSrtSubtitle, Converter={StaticResource booleanToVisConverter}, ConverterParameter=false}" />

                            <TextBlock  Grid.Column="2" VerticalAlignment="Center" FontWeight="Bold" Text="Lang" />
                            <ComboBox  Grid.Column="3" Width="125" Margin="5,0,5,0" HorizontalAlignment="Stretch" IsEnabled="{Binding IsSrtSubtitle}"
                                      ItemsSource="{Binding DataContext.Languages, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                      SelectedItem="{Binding SrtLang}" />
                            
                            <TextBlock  Grid.Column="4" VerticalAlignment="Center" FontWeight="Bold" Text="Char Code" />
                            <ComboBox Grid.Column="5" Width="70" Margin="5,0,5,0" IsEnabled="{Binding IsSrtSubtitle}"
                                      ItemsSource="{Binding DataContext.CharacterCodes, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                      SelectedItem="{Binding SrtCharCode}" />
                            
                            <TextBlock Grid.Column="6" VerticalAlignment="Center" FontWeight="Bold" Text="Offset (ms)" />
                            <controls:NumberBox Grid.Column="7" Width="58" Margin="5,0,5,0" Minimum="-9000000" Maximum="900000" 
                                                IsEnabled="{Binding IsSrtSubtitle}" Number="{Binding SrtOffset, Mode=TwoWay}" />

                            <CheckBox Grid.Column="8" Margin="5,1,5,0" VerticalAlignment="Center" VerticalContentAlignment="Center"  IsChecked="{Binding Burned}" IsEnabled="{Binding CanBeBurned}" Content="Burn In" FontWeight="Bold">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <i:InvokeCommandAction Command="{Binding DataContext.SetBurnedToFalseForAllExceptCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>

                            <CheckBox Grid.Column="9" Margin="0,1,5,0" VerticalAlignment="Center"  IsChecked="{Binding Default}" Content="Default" FontWeight="Bold" VerticalContentAlignment="Center"
                                      IsEnabled="{Binding DataContext.IsBurnableOnly, Converter={StaticResource booleanConverter}, ConverterParameter=true, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <i:InvokeCommandAction Command="{Binding DataContext.SelectDefaultTrackCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}" />

                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>

                            <StackPanel Orientation="Horizontal" Grid.Column="10" Margin="5,0,0,0">
                                <TextBlock Text="Name: " FontWeight="Bold" Margin="0,0,5,0" />
                                <TextBox x:Name="TrackNameSrt" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Width="160" />
                            </StackPanel>

                        </Grid>

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

                    </Grid>


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

    </Grid>
</UserControl>
