<Window
        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:local="clr-namespace:JoinerSplitter"
        xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" x:Class="JoinerSplitter.Pages.MainWindow"
        mc:Ignorable="d"
        Closing="MainWindow_OnClosing"
        Title="JoinerSplitter" Height="541" Width="1000">
    <Window.DataContext>
        <local:AppModel />
    </Window.DataContext>
    <Window.Resources>
        <CollectionViewSource Source="{Binding CurrentJob.Files}" IsLiveGroupingRequested="True" x:Key="Files">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="GroupIndex" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <local:DoubleToTimeSpan x:Key="DoubleToTimeSpanConverter" />
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Menu>
            <MenuItem Header="_File">
                <MenuItem Header="_New job" Click="NewJob" />
                <MenuItem Header="_Open job" Click="OpenJob" />
                <MenuItem Header="_Save job" Click="SaveJob" />
                <MenuItem Header="Save job _As..." Click="SaveJobAs" />
                <Separator />
                <MenuItem Header="_Add video" Click="Button_AddVideo" />
                <MenuItem Header="Du_plicate" Click="Button_Duplicate" />
                <MenuItem Header="_Delete" Click="Button_Delete" />
                <Separator />
                <MenuItem Header="E_xport" />
                <Separator />
                <MenuItem Header="E_xit" />
            </MenuItem>
            <MenuItem Header="_Video"></MenuItem>
            <MenuItem Header="Encdoing _Presets" Click="Button_OpenPresets" />
        </Menu>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="300" />
            </Grid.ColumnDefinitions>
            <Grid x:Name="MainGrid" Grid.RowSpan="5">
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <MediaElement x:Name="MediaElement" Visibility="{Binding CurrentFile, TargetNullValue=Hidden}" ScrubbingEnabled="True" Volume="1" LoadedBehavior="Manual" UnloadedBehavior="Manual" />
                <WrapPanel Grid.Row="1" Visibility="{Binding CurrentFile, TargetNullValue=Hidden}" Width="550" Margin="0,5,0,5">
                    <TextBlock Text="Current:" Margin="10,2,0,0" />
                    <xctk:TimeSpanUpDown FractionalSecondsDigitsCount="3" Value="{Binding Value, Converter={StaticResource DoubleToTimeSpanConverter}, ElementName=Slider}" />
                    <TextBlock Text="Start:" Margin="10,2,0,0" />
                    <xctk:TimeSpanUpDown FractionalSecondsDigitsCount="3" Value="{Binding SelectionStart, Converter={StaticResource DoubleToTimeSpanConverter}, ElementName=Slider}" />
                    <TextBlock Text="End:" Margin="10,2,0,0" />
                    <xctk:TimeSpanUpDown FractionalSecondsDigitsCount="3" Value="{Binding SelectionEnd, Converter={StaticResource DoubleToTimeSpanConverter}, ElementName=Slider}" />
                    <TextBlock Text="Duration:" Margin="10,2,0,0" />
                    <TextBlock Text="{Binding CurrentFile.Duration,Converter={StaticResource DoubleToTimeSpanConverter}, StringFormat=hh\\:mm\\:ss\\.fff}"  Margin="5,2,0,0" />
                </WrapPanel>
                <Slider x:Name="Slider"
            IsMoveToPointEnabled="True"
            Grid.Row="2"
            IsSelectionRangeEnabled="True"
            Minimum="0"
            Maximum="{Binding CurrentFile.Duration, FallbackValue=0}"
            SelectionStart="{Binding CurrentFile.Start, FallbackValue=0}"
            SelectionEnd="{Binding CurrentFile.End, FallbackValue=0}"
            TickFrequency="1"
            TickPlacement="BottomRight"
            IsEnabled="{Binding HasCurrentFile}"
                Margin="0,0,0,5"
            ValueChanged="Slider_ValueChanged" PreviewMouseDown="Slider_PreviewMouseDown" PreviewMouseUp="Slider_PreviewMouseUp" />
                <StackPanel Orientation="Horizontal" Grid.Row="3" IsEnabled="{Binding HasCurrentFile}">
                    <Button Content="Prev" ToolBar.OverflowMode="Never" />
                    <Button ToolTip="Seek to the start of selection" Click="Button_Start">
                        <Image Source="/Icons/SeekStart.png" />
                    </Button>
                    <Button ToolTip="Selection Start" Click="Button_SelStart">
                        <Image Source="/Icons/CutStart.png" />
                    </Button>
                    <Button x:Name="PlayButton" ToolTip="Play/stop video" Click="Button_Play">
                        <Image Source="/Icons/PlayPause.png" />
                    </Button>
                    <Button x:Name="SplitButton" ToolTip="Split selected cut to separate videos by current position" Click="SplitButton_Click">
                        <Image Source="/Icons/Cut_6523_24.png" />
                    </Button>
                    <Button ToolTip="Selection End" Click="Button_SelEnd">
                        <Image Source="/Icons/CutEnd.png" />
                    </Button>
                    <Button ToolTip="Seek to the end of selection" Click="Button_End">
                        <Image Source="/Icons/SeekEnd.png" />
                    </Button>
                    <Button Content="Next" ToolBar.OverflowMode="Never" />
                </StackPanel>
            </Grid>
            <GridSplitter x:Name="MainSplitter" Grid.Column="1" Grid.Row="0" Grid.RowSpan="6" HorizontalAlignment="Stretch" Style="{DynamicResource GridSplitterStyleVertical}" />
            <Grid Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <StackPanel Orientation="Horizontal" Grid.Row="0">
                    <Button ToolTip="Add video" Click="Button_AddVideo" ToolBar.OverflowMode="Never">
                        <Image Source="/Icons/AddMark_10580_inverse.png" />
                    </Button>
                    <Button ToolTip="Move files up in group or to previous group" IsEnabled="{Binding HasCurrentFile}" Click="Button_MoveUp">
                        <Image Source="/Icons/arrow_Up_16xLG.png" />
                    </Button>
                    <Button ToolTip="Down" IsEnabled="{Binding HasCurrentFile}" Click="Button_MoveDown">
                        <Image Source="/Icons/arrow_Down_16xLG.png" />
                    </Button>
                    <Button ToolTip="Split" IsEnabled="{Binding HasCurrentFile}" Click="Button_SplitFiles">
                        <Image Source="/Icons/Cut_6523.png" />
                    </Button>
                    <Button ToolTip="Duplicate" IsEnabled="{Binding HasCurrentFile}" Click="Button_Duplicate">
                        <Image Source="/Icons/Copy_6524.png" />
                    </Button>
                    <Button ToolTip="Delete" IsEnabled="{Binding HasCurrentFile}" Click="Button_Delete">
                        <Image Source="/Icons/action_Cancel_16xLG.png" />
                    </Button>
                </StackPanel>
                <ListView
            x:Name="FilesList" Grid.Row="1"
            ItemsSource="{Binding Source={StaticResource Files}}" d:DataContext="{d:DesignInstance {x:Type local:VideoFile}}"
            SelectionChanged="OutputList_SelectionChanged" DragEnter="FilesList_DragEnter" DragOver="FilesList_DragOver" Drop="FilesList_Drop"
            AllowDrop="True" DragLeave="FilesList_DragLeave" PreviewMouseLeftButtonDown="FilesList_MouseLeftButtonDown" PreviewMouseMove="FilesList_MouseMove" PreviewMouseLeftButtonUp="FilesList_PreviewMouseLeftButtonUp">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Original File" Width="110" DisplayMemberBinding="{Binding FileName}" />
                            <GridViewColumn Header="Start" Width="80" DisplayMemberBinding="{Binding Start,Converter={StaticResource DoubleToTimeSpanConverter}, StringFormat=hh\\:mm\\:ss\\.fff}" />
                            <GridViewColumn Header="End" Width="80" DisplayMemberBinding="{Binding End,Converter={StaticResource DoubleToTimeSpanConverter}, StringFormat=hh\\:mm\\:ss\\.fff}" />
                        </GridView>
                    </ListView.View>

                    <ListView.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <Grid Visibility="{Binding DataContext.CurrentJob.HasMoreGroupsThanOne, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <Separator Margin="0,5,5,2" />
                                        <TextBlock Grid.Column="1" FontWeight="Bold" FontSize="14" Text="{Binding FileName}" />
                                        <Separator Grid.Column="2" Margin="5,5,0,2" />
                                    </Grid>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                        </GroupStyle>
                    </ListView.GroupStyle>
                </ListView>
                <TextBox x:Name="OutputFolderBox"  Grid.Row="3" TextWrapping="WrapWithOverflow"  Text="{Binding OutputFolder, Mode=OneWay}" Tag="Output folder"
                    Style="{StaticResource WatermarkedTextBox}" IsReadOnly="True" PreviewMouseDown="OutputFolderBox_MouseDown" />
                <TextBox x:Name="OutputFilenameBox" Grid.Row="4" TextWrapping="WrapWithOverflow" Text="{Binding CurrentJob.OutputName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Tag="Output file name"
                    Style="{StaticResource WatermarkedTextBox}"  PreviewTextInput="OutputFilenameBox_TextInput" />
                <Button x:Name="ProcessButton" Content="Export" HorizontalAlignment="Stretch" Grid.Row="5" Click="ProcessButton_Click" />
                <ComboBox Grid.Row="2" ItemsSource="{Binding ComboBoxEncoderPresets}" HorizontalContentAlignment="Stretch" SelectedItem="{Binding CurrentJob.Encoding, Mode=TwoWay}">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <Label Content="{Binding DisplayName}"></Label>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </Grid>
        </Grid>
    </Grid>
</Window>