<Window x:Class="HandBrakeWPF.Views.QueueView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cal="http://www.caliburnproject.org"
        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/expression/2010/interactivity"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:YourNamespace="clr-namespace:HandBrakeWPF.AttachedProperties"
        xmlns:Audio="clr-namespace:HandBrakeWPF.Converters.Audio"
        xmlns:Subtitles="clr-namespace:HandBrakeWPF.Converters.Subtitles" Title="{Binding Title}"
        Width="600"
        Height="400"
        MinWidth="600"
        MinHeight="400"
        MaxWidth="600"
        Background="#FFF0F0F0"
        WindowStartupLocation="CenterScreen"
        TextOptions.TextFormattingMode="Display"
        mc:Ignorable="d">

    <Window.Resources>
        <Converters:BooleanToVisibilityConverter x:Key="boolToVisConverter" />
        <Converters:FullPathToFileNameConverter x:Key="filePathToFilenameConverter" />
        <Converters:EnumComboConverter x:Key="enumComboConverter" />
        <Converters:QueueStatusToVisibilityConverter x:Key="queueStatusVisConverter" />
        <Audio:AudioQueueDisplayConverter x:Key="audioTrackDisplayConverter" />
        <Subtitles:SubtitlesQueueDisplayConverter x:Key="subtitleTrackDisplayConverter" />
    </Window.Resources>

    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ToolBar Name="mainToolBar"
                 HorizontalAlignment="Stretch"
                 VerticalAlignment="Stretch"
                 SnapsToDevicePixels="True">

            <DockPanel Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ToolBarPanel}}}" Background="Transparent">
                <DockPanel.Resources>
                    <Style BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" TargetType="{x:Type Button}" />
                    <Style BasedOn="{StaticResource {x:Static ToolBar.MenuStyleKey}}" TargetType="{x:Type Menu}" />
                </DockPanel.Resources>

                <Button Name="Start"
                        cal:Message.Attach="[Event Click] = [Action StartEncode]"
                        Visibility="{Binding IsEncoding,
                                             Converter={StaticResource boolToVisConverter},
                                             ConverterParameter=true}">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="32"
                               Height="32"
                               Source="Images/Play.png" />
                        <Label Margin="8,0,0,0"
                               VerticalAlignment="Center"
                               Content="Start" />
                    </StackPanel>
                </Button>

                <Button Name="Pause"
                        cal:Message.Attach="[Event Click] = [Action PauseEncode]"
                        Visibility="{Binding IsEncoding,
                                             Converter={StaticResource boolToVisConverter},
                                             ConverterParameter=false}">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="32"
                               Height="32"
                               Source="Images/Pause.png" />
                        <Label Margin="8,0,0,0"
                               VerticalAlignment="Center"
                               Content="Pause" />
                    </StackPanel>
                </Button>

                <Menu HorizontalAlignment="Right" Background="Transparent">
                    <MenuItem>
                        <MenuItem.Header>
                            <StackPanel Height="32" Orientation="Horizontal">
                                <TextBlock VerticalAlignment="Center"
                                           FontWeight="Bold"
                                           Text="When Done: " />
                                <Label Margin="8,0,0,0"
                                       VerticalAlignment="Center"
                                       Content="{Binding WhenDoneAction}" />
                                <Path Height="5"
                                      Margin="2,2,2,0"
                                      Data="M 0 0 L 4 4 L 8 0 Z"
                                      Fill="{DynamicResource GlyphBrush}" />
                            </StackPanel>
                        </MenuItem.Header>
                        <MenuItem x:Name="doNothing"
                                  cal:Message.Attach="[Event Click] = [Action WhenDone(doNothing.Header)]"
                                  Header="Do nothing"
                                  IsCheckable="True"
                                  YourNamespace:MenuItemExtensions.GroupName="whenDone" />
                        <MenuItem x:Name="shutdown"
                                  cal:Message.Attach="[Event Click] = [Action WhenDone(shutdown.Header)]"
                                  Header="Shutdown"
                                  IsCheckable="True"
                                  YourNamespace:MenuItemExtensions.GroupName="whenDone" />
                        <MenuItem x:Name="suspend"
                                  cal:Message.Attach="[Event Click] = [Action WhenDone(suspend.Header)]"
                                  Header="Suspend"
                                  IsCheckable="True"
                                  YourNamespace:MenuItemExtensions.GroupName="whenDone" />
                        <MenuItem x:Name="hibernate"
                                  cal:Message.Attach="[Event Click] = [Action WhenDone(hibernate.Header)]"
                                  Header="Hibernate"
                                  IsCheckable="True"
                                  YourNamespace:MenuItemExtensions.GroupName="whenDone" />
                        <MenuItem x:Name="lock"
                                  cal:Message.Attach="[Event Click] = [Action WhenDone(lock.Header)]"
                                  Header="Lock system"
                                  IsCheckable="True"
                                  YourNamespace:MenuItemExtensions.GroupName="whenDone" />
                        <MenuItem x:Name="logoff"
                                  cal:Message.Attach="[Event Click] = [Action WhenDone(logoff.Header)]"
                                  Header="Log off"
                                  IsCheckable="True"
                                  YourNamespace:MenuItemExtensions.GroupName="whenDone" />
                        <MenuItem x:Name="quit"
                                  cal:Message.Attach="[Event Click] = [Action WhenDone(quit.Header)]"
                                  Header="Quit HandBrake"
                                  IsCheckable="True"
                                  YourNamespace:MenuItemExtensions.GroupName="whenDone" />
                    </MenuItem>
                </Menu>
            </DockPanel>
        </ToolBar>

        <StackPanel Grid.Row="1" Margin="10,20,10,20">
            <TextBlock Text="{Binding JobsPending}" />
            <TextBlock Text="{Binding JobStatus}" />
        </StackPanel>
        
        <ListBox Grid.Row="2"
                 Margin="10,0,10,10"
                 Background="LightGray"
                 dd:DragDrop.DropHandler="{Binding}"
                 dd:DragDrop.IsDragSource="True"
                 dd:DragDrop.IsDropTarget="True"
                 ItemsSource="{Binding QueueTasks, Mode=OneWay}"
                 SelectionMode="Extended">

            <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem cal:Message.Attach="[Event Click] = [Action ClearCompleted]" Header="Clear Completed" />
                    <MenuItem cal:Message.Attach="[Event Click] = [Action Clear]" Header="Clear" />
                    <Separator />
                    <MenuItem cal:Message.Attach="[Event Click] = [Action Import]" Header="Import Queue" />
                    <MenuItem cal:Message.Attach="[Event Click] = [Action Export]" Header="Export Queue" />
                </ContextMenu>
            </ListBox.ContextMenu>

            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="Background" Value="WhiteSmoke" />
                    <Setter Property="Margin" Value="0,0,0,1" />
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <StackPanel Grid.Column="1"
                                    Margin="0,5,0,5"
                                    HorizontalAlignment="Stretch">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock FontWeight="Bold" Text="Video" />
                                    <TextBlock Text=": " />
                                    <TextBlock Text="{Binding Task.VideoEncoder, Converter={StaticResource enumComboConverter}}" />
                                    <TextBlock Margin="10,0,0,0" FontWeight="Bold" Text="Audio: " />
                                    <TextBlock Text="{Binding Task.AudioTracks, Converter={StaticResource audioTrackDisplayConverter}}" />
                                </StackPanel>

                                <StackPanel Orientation="Horizontal">
                                    <TextBlock FontWeight="Bold" Text="Subtitles: " />
                                    <TextBlock Text="{Binding Task.SubtitleTracks, Converter={StaticResource subtitleTrackDisplayConverter}}" />
                                </StackPanel>

                                <StackPanel Orientation="Horizontal">
                                    <TextBlock FontWeight="Bold" Text="Advanced: " />
                                    <TextBlock Text="{Binding Task.AdvancedEncoderOptions}" />
                                </StackPanel>

                            </StackPanel>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>

            <ListBox.ItemTemplate>
                <DataTemplate x:Name="QueueItemTemplate">

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

                        <!--  Marker  -->
                        <Image Grid.Column="0"
                               Width="16"
                               Height="16"
                               Margin="10,0,10,0">
                            <Image.Style>
                                <Style TargetType="{x:Type Image}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Status, Converter={StaticResource enumComboConverter}}" Value="Waiting">
                                            <Setter Property="Source" Value="Images\Movies.png" />
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status, Converter={StaticResource enumComboConverter}}" Value="In Progress">
                                            <Setter Property="Source" Value="Images\Working0.png" />
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status, Converter={StaticResource enumComboConverter}}" Value="Completed">
                                            <Setter Property="Source" Value="Images\Complete.png" />
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status, Converter={StaticResource enumComboConverter}}" Value="Error">
                                            <Setter Property="Source" Value="Images\WarningSmall.png" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>

                        <!--  Settings  -->
                        <StackPanel Grid.Column="1"
                                    Margin="0,5,0,5"
                                    HorizontalAlignment="Stretch">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock FontWeight="Bold" Text="Title: " />
                                <TextBlock Text="{Binding Task.Title}" />

                                <TextBlock Margin="10,0,0,0"
                                           FontWeight="Bold"
                                           Text="{Binding Task.PointToPointMode}" />
                                <TextBlock Text=": " />
                                <TextBlock Text="{Binding Task.StartPoint}" />
                                <TextBlock Margin="5,0,5,0" Text="to" />
                                <TextBlock Text="{Binding Task.EndPoint}" />
                            </StackPanel>

                            <!--  TODO Support Drive Label Name  -->
                            <StackPanel Orientation="Horizontal">
                                <TextBlock FontWeight="Bold" Text="Source: " />
                                <TextBlock Text="{Binding Task.Source, Converter={StaticResource filePathToFilenameConverter}}" />
                            </StackPanel>

                            <StackPanel Orientation="Horizontal">
                                <TextBlock FontWeight="Bold" Text="Destination: " />
                                <TextBlock Text="{Binding Task.Destination, Converter={StaticResource filePathToFilenameConverter}}" />
                            </StackPanel>
                        </StackPanel>

                        <!--  Delete  -->
                        <Grid Grid.Column="2"
                              Margin="10,0,10,0"
                              VerticalAlignment="Center">

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

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

                            <Image Width="20"  Grid.Row="0" Grid.Column="0"
                                       Height="20" VerticalAlignment="Center" Margin="0,5,0,0"
                                       Source="Images/Refresh.ico"
                                       ToolTip="Reset job status to Waiting."
                                       Visibility="{Binding Status,
                                                            Converter={StaticResource queueStatusVisConverter}}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseDown">
                                        <cal:ActionMessage MethodName="RetryJob">
                                            <cal:Parameter Value="{Binding}" />
                                        </cal:ActionMessage>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Image>

                            <Image Width="20" Grid.Row="0" Grid.Column="1"
                                       Height="20"
                                       Margin="10,5,0,0"
                                       Source="Images/Options24.png"
                                       ToolTip="Edit this Job">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseDown">
                                        <cal:ActionMessage MethodName="EditJob">
                                            <cal:Parameter Value="{Binding}" />
                                        </cal:ActionMessage>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Image>

                            <Image Width="20"  Grid.Row="0" Grid.Column="2"
                                       Height="20"
                                       Margin="10,5,0,0"
                                       Source="Images/delete.png"
                                       ToolTip="Remove this Job">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseDown">
                                        <cal:ActionMessage MethodName="RemoveJob">
                                            <cal:Parameter Value="{Binding}" />
                                        </cal:ActionMessage>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Image>


                        </Grid>

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


    </Grid>
</Window>
