<Window x:Class="HandBrakeWPF.Views.QueueSelectionView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Converters="clr-namespace:HandBrakeWPF.Converters"
        xmlns:Properties="clr-namespace:HandBrakeWPF.Properties"
        xmlns:dd="urn:gong-wpf-dragdrop"
        xmlns:controls="clr-namespace:HandBrakeWPF.Controls"
        xmlns:main="clr-namespace:HandBrakeWPF.Converters.Main"
        Title="{Binding Title}"
        MinWidth="750"
        Width="750"
        Height="600"
        MinHeight="600"
        WindowStartupLocation="CenterScreen"
        TextOptions.TextFormattingMode="Display"
        mc:Ignorable="d">

    <Window.Resources>
        <Converters:BooleanToVisibilityConverter x:Key="boolToVisConverter" />
        <Converters:ThemeImageConverter x:Key="themeConverter" />
        <main:PointToPointConverter x:Key="PointToPointConverter" />
        <Converters:LongToIntConverter x:Key="longToIntConverter" />

    </Window.Resources>

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

        <!--  Header  -->
        <TextBlock Text="{x:Static Properties:Resources.QueueSelectionView_Title}" Style="{StaticResource windowTitle}"  Margin="10,10,10,10" Grid.Row="0" />

        <!-- Text -->
        <Grid Grid.Row="1" Margin="10,0,10,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <TextBlock Text="{x:Static Properties:Resources.QueueSelectionView_ChooseTitles}" FontSize="14" />

            <Menu HorizontalAlignment="Right" Background="Transparent" VerticalAlignment="Bottom" Grid.Column="2" Margin="0,0,0,0">
                <MenuItem>
                    <MenuItem.Header>
                        <StackPanel Height="27" Orientation="Horizontal">
                            <Image Source="{Binding Converter={StaticResource themeConverter}, ConverterParameter='Advanced.png'}" Width="20" Height="20" VerticalAlignment="Center" />
                            <TextBlock VerticalAlignment="Center"
                                       Text="{x:Static Properties:Resources.QueueView_Options}" Margin="5,0,0,0" />
                            <Path Height="5"
                                  Margin="2,2,0,0"
                                  Data="M 0 0 L 4 4 L 8 0 Z"
                                  Fill="{DynamicResource ButtonGlyphBrush}" />
                        </StackPanel>
                    </MenuItem.Header>
                    <MenuItem Header="Select All" Command="{Binding RelayCommand}" CommandParameter="SelectAll" />
                    <MenuItem Header="Deselect All" Command="{Binding RelayCommand}" CommandParameter="UnSelectAll" />
                    <MenuItem Header="Invert Selection" Command="{Binding RelayCommand}" CommandParameter="InvertSelection" />
                    <Separator />
                    <MenuItem Header="Order by Title"  Command="{Binding RelayCommand}" CommandParameter="OrderByTitle" />
                    <MenuItem Header="Order by Duration" IsChecked="{Binding OrderedByDuration}" Command="{Binding RelayCommand}" CommandParameter="OrderByDuration" />
                    <MenuItem Header="Order by Name" IsChecked="{Binding OrderedByName}" Command="{Binding RelayCommand}" CommandParameter="OrderByName" />
                </MenuItem>
            </Menu>
        </Grid>

        <!-- Selection -->
        <ListBox x:Name="SelectionGrid" Grid.Row="2"
                 MinHeight="240"
                 Margin="10,10,10,10"
                 VerticalAlignment="Stretch"
                 HorizontalAlignment="Stretch"
                 ItemsSource="{Binding TitleList}"
                 SelectionMode="Single" 
                 PreviewKeyDown="SelectionGrid_OnKeyDown"
                 dd:DragDrop.DropHandler="{Binding}"
                 dd:DragDrop.IsDragSource="True"
                 dd:DragDrop.IsDropTarget="True">
            <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="Select All" Command="{Binding RelayCommand}" CommandParameter="SelectAll" />
                    <MenuItem Header="Deselect All" Command="{Binding RelayCommand}" CommandParameter="UnSelectAll" />
                    <MenuItem Header="Invert Selection" Command="{Binding RelayCommand}" CommandParameter="InvertSelection" />
                    <Separator />
                    <MenuItem Header="Order by Title" Command="{Binding RelayCommand}" CommandParameter="OrderByTitle" />
                    <MenuItem Header="Order by Duration" Command="{Binding RelayCommand}" CommandParameter="OrderByDuration" />
                    <MenuItem Header="Order by Name"  Command="{Binding RelayCommand}" CommandParameter="OrderByName" />
                </ContextMenu>
            </ListBox.ContextMenu>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch" MinHeight="32" Margin="5">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        
                        <CheckBox IsChecked="{Binding IsSelected}" Grid.RowSpan="3" Grid.Row="0" Grid.Column="0" Margin="0,0,5,0" VerticalAlignment="Center" />

                        <TextBlock Text="Title:" Margin="0,0,5,0" FontWeight="Bold" Grid.Row="0" Grid.Column="1" />
                        <TextBlock Text="{Binding Title}" Margin="5,0,5,0" Grid.Row="0" Grid.Column="2" />

                        <TextBlock Text="Source:" Margin="0,0,5,0" FontWeight="Bold" Grid.Row="1" Grid.Column="1" />
                        <TextBlock Text="{Binding SourceName}" Margin="5,0,5,0" Grid.Row="1" Grid.Column="2" />

                        <TextBlock Text="{Binding SourceInfo}" Margin="5,0,5,0" Grid.Row="2" Grid.Column="2" Foreground="DimGray" />

                    </Grid>

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

        <!-- Range Limits -->
        <StackPanel Orientation="Vertical" Grid.Row="3" Margin="10,0,0,0">

            <TextBlock Text="{x:Static Properties:Resources.QueueSelectionView_RangeLimits}" FontSize="14" />

            <CheckBox Content="{x:Static Properties:Resources.QueueSelectionView_RangeLimitsDesc}" IsChecked="{Binding RangeLimits.IsEnabled}" Margin="0,5,0,5" />

            <StackPanel Orientation="Horizontal" Margin="17,0,0,0" IsEnabled="{Binding RangeLimits.IsEnabled}">
                <ComboBox Name="PointToPointMode" MinWidth="80" Margin="5,0,0,0"
                          ItemsSource="{Binding RangeMode, Converter={StaticResource PointToPointConverter}}" ToolTip="{x:Static Properties:ResourcesTooltips.MainView_Range}"
                          SelectedItem="{Binding RangeLimits.SelectedPointToPoint, Converter={StaticResource PointToPointConverter}}" />
                <ComboBox Name="StartPoint" MinWidth="60" Margin="5,0,0,0" ItemsSource="{Binding StartEndRangeItems}"
                          SelectedItem="{Binding RangeLimits.SelectedStartPoint, Converter={StaticResource longToIntConverter}}"
                          ToolTip="{x:Static Properties:ResourcesTooltips.MainView_StartPoint}"
                          Visibility="{Binding RangeLimits.ShowTextEntryForPointToPointMode, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />
                <controls:TimeSpanBox  Number="{Binding RangeLimits.SelectedStartPoint, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          Minimum="0" AllowEmpty="False" ShowTimeSpan="{Binding RangeLimits.IsTimespanRange}"
                          ToolTip="{x:Static Properties:ResourcesTooltips.MainView_StartPoint}"
                          MinWidth="72" Margin="5,0,0,0"  Visibility="{Binding RangeLimits.ShowTextEntryForPointToPointMode, Converter={StaticResource boolToVisConverter}, ConverterParameter=false}" />

                <TextBlock Margin="4,0,0,0" Text="{x:Static Properties:Resources.MainView_through}" />
                <ComboBox Name="EndPoint" MinWidth="60" Margin="4,0,0,0" 
                          ItemsSource="{Binding StartEndRangeItems}" 
                          SelectedItem="{Binding RangeLimits.SelectedEndPoint, Converter={StaticResource longToIntConverter}}"
                          ToolTip="{x:Static Properties:ResourcesTooltips.MainView_EndPoint}"
                          Visibility="{Binding RangeLimits.ShowTextEntryForPointToPointMode, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />
                <controls:TimeSpanBox  Number="{Binding RangeLimits.SelectedEndPoint, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          Minimum="0" AllowEmpty="False" ShowTimeSpan="{Binding RangeLimits.IsTimespanRange}"
                          ToolTip="{x:Static Properties:ResourcesTooltips.MainView_EndPoint}"
                          MinWidth="72" Margin="5,0,0,0"  
                          Visibility="{Binding RangeLimits.ShowTextEntryForPointToPointMode,  Converter={StaticResource boolToVisConverter},  ConverterParameter=false}" />

            </StackPanel>
        </StackPanel>
 

        <!-- Checklist -->
        <StackPanel Orientation="Vertical" Grid.Row="4" Margin="10,10,10,0">
            <StackPanel Orientation="Vertical" Margin="0,0,0,5">
                <TextBlock Text="{x:Static Properties:Resources.MainView_SelectedPresets}" FontSize="14" />
                <TextBlock Text="{Binding CurrentPreset}" TextWrapping="Wrap" Margin="0,5,0,0" />
            </StackPanel>
          
            <TextBlock Text="{x:Static Properties:Resources.QueueSelection_AutoNameWarning}"
                       TextWrapping="Wrap" Visibility="{Binding IsAutoNamingEnabled, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}"/>
        </StackPanel>

        <!--  Controls  -->
        <Grid Grid.Row="5"
              Margin="0,20,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Button Grid.Column="1"
                    Margin="0,5,10,10"
                    Command="{Binding RelayCommand}" CommandParameter="Cancel"
                    Content="{x:Static Properties:Resources.Generic_Cancel}"
                    IsCancel="True"
                    Padding="8,2" />
            <Button Grid.Column="2"
                    Margin="0,5,10,10"
                    Command="{Binding RelayCommand}" CommandParameter="Add"
                    Content="{x:Static Properties:Resources.Generic_Add}"
                    IsDefault="True"
                    Padding="8,2" />
        </Grid>

    </Grid>
</Window>
