<?xml version="1.0" encoding="utf-8"?>
<Page
    x:Class="VDownload.Core.Views.Home.HomeDownloadsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:VDownload.Core.Views.Home"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:i="using:Microsoft.Xaml.Interactivity" 
    xmlns:ic="using:Microsoft.Xaml.Interactions.Core"
    xmlns:m="using:VDownload.Models"
    xmlns:ct="using:CommunityToolkit.WinUI"
    xmlns:ctc="using:CommunityToolkit.WinUI.Controls"
    xmlns:ctuc="using:CommunityToolkit.WinUI.UI.Controls"
    xmlns:ctb="using:CommunityToolkit.WinUI.Behaviors"
    mc:Ignorable="d"
    x:Name="Root"
    Background="Transparent">
    <ctuc:SwitchPresenter Value="{Binding TaskListIsEmpty, Converter={StaticResource ObjectToStringConverter}}">
        <ctuc:Case Value="True">
            <StackPanel HorizontalAlignment="Center"
                        VerticalAlignment="Center">
                <Image Source="{StaticResource ImageHomeDownloadsViewNoTasks}"
                       Width="100"/>
                <TextBlock Text="Click Video/Playlist search button to add new tasks" 
                           Foreground="{StaticResource GreyText}"/>
            </StackPanel>
        </ctuc:Case>
        <ctuc:Case Value="False">
            <ScrollViewer>
                <ItemsControl ItemsSource="{Binding Tasks}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid Background="{ThemeResource ViewBackgroundColor}"
                                  CornerRadius="10"
                                  Height="150"
                                  Margin="0,0,0,10">
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Image Grid.Column="0" 
                                           Source="{Binding Video.ThumbnailUrl, TargetNullValue={StaticResource ImageOtherThumbnail}}" 
                                           VerticalAlignment="Stretch"/>
                                    <Grid Grid.Column="1"
                                          Margin="10"
                                          RowSpacing="10">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition/>
                                        </Grid.RowDefinitions>
                                        <Grid Grid.Row="0"
                                              ColumnSpacing="10" 
                                              HorizontalAlignment="Left">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition/>
                                                <ColumnDefinition Width="Auto"/>
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Grid.Column="0" 
                                                       FontWeight="SemiBold"
                                                       FontSize="18"
                                                       Text="{Binding Video.Title}"
                                                       TextTrimming="CharacterEllipsis"/>
                                            <TextBlock Grid.Column="1" 
                                                       VerticalAlignment="Center"
                                                       FontWeight="Light"
                                                       FontSize="12"
                                                       Text="{Binding Video.Author}"/>
                                        </Grid>
                                        <Grid Grid.Row="1" 
                                              RowSpacing="10" 
                                              ColumnSpacing="10">
                                            <Grid.Resources>
                                                <x:Double x:Key="TextSize">12</x:Double>
                                            </Grid.Resources>
                                            <Grid.RowDefinitions>
                                                <RowDefinition/>
                                                <RowDefinition/>
                                                <RowDefinition/>
                                                <RowDefinition/>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto"/>
                                                <ColumnDefinition/>
                                            </Grid.ColumnDefinitions>
                                            <Image Grid.Row="0"
                                                   Grid.Column="0"
                                                   Source="{ThemeResource ImageHomeDownloadsViewQuality}"/>
                                            <StackPanel Grid.Row="0"
                                                        Grid.Column="1"
                                                        VerticalAlignment="Center"
                                                        Orientation="Horizontal">
                                                <ctuc:SwitchPresenter Value="{Binding DownloadOptions.MediaType, Converter={StaticResource ObjectToStringConverter}}">
                                                    <ctuc:Case Value="Original">
                                                        <TextBlock x:Uid="/CommonResources/MediaTypeOriginal"
                                                                   FontSize="{StaticResource TextSize}"/>
                                                    </ctuc:Case>
                                                    <ctuc:Case Value="OnlyVideo">
                                                        <TextBlock x:Uid="/CommonResources/MediaTypeOnlyVideo"
                                                                   FontSize="{StaticResource TextSize}"/>
                                                    </ctuc:Case>
                                                    <ctuc:Case Value="OnlyAudio">
                                                        <TextBlock x:Uid="/CommonResources/MediaTypeOnlyAudio"
                                                                   FontSize="{StaticResource TextSize}"/>
                                                    </ctuc:Case>
                                                </ctuc:SwitchPresenter>
                                                <TextBlock FontSize="{StaticResource TextSize}">
                                                    <Run Text=" ("/><Run Text="{Binding DownloadOptions.SelectedStream.Name}"/>)
                                                </TextBlock>
                                            </StackPanel>
                                            <Image Grid.Row="1"
                                                   Grid.Column="0"
                                                   Source="{ThemeResource ImageHomeDownloadsViewTime}"/>
                                            <StackPanel Grid.Row="1"
                                                        Grid.Column="1"
                                                        Orientation="Horizontal"
                                                        VerticalAlignment="Center">
                                                <TextBlock Text="{Binding DownloadOptions.DurationAfterTrim}"
                                                           FontSize="{StaticResource TextSize}"/>
                                                <TextBlock Visibility="{Binding DownloadOptions.IsTrimmed, Converter={StaticResource BoolToVisibilityConverter}}"
                                                           FontSize="{StaticResource TextSize}">
                                                    <Run Text=" "/>(<Run Text="{Binding DownloadOptions.TrimStart}"/> - <Run Text="{Binding DownloadOptions.TrimEnd}"/>)
                                                </TextBlock>
                                            </StackPanel>
                                            <Image Grid.Row="2"
                                                   Grid.Column="0"
                                                   Source="{ThemeResource ImageHomeDownloadsViewFile}"/>
											<TextBlock Grid.Row="2"
                                                       Grid.Column="1"
                                                       FontSize="{StaticResource TextSize}"
                                                       VerticalAlignment="Center">
												<Run Text="{Binding DownloadOptions.Directory}"/>\<Run Text="{Binding DownloadOptions.Filename}"/>.<Run Text="{Binding DownloadOptions.Extension}"/>
											</TextBlock>
                                            <Image Grid.Row="3"
                                                   Grid.Column="0">
                                                <i:Interaction.Behaviors>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Idle">
                                                        <ic:ChangePropertyAction PropertyName="Source"
                                                                                 Value="{ThemeResource ImageHomeDownloadsViewIdle}"/>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Queued">
                                                        <ic:ChangePropertyAction PropertyName="Source"
                                                                                 Value="{ThemeResource ImageHomeDownloadsViewQueued}"/>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Initializing">
                                                        <ic:ChangePropertyAction PropertyName="Source"
                                                                                 Value="{ThemeResource ImageHomeDownloadsViewInitializing}"/>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Downloading">
                                                        <ic:ChangePropertyAction PropertyName="Source"
                                                                                 Value="{ThemeResource ImageHomeDownloadsViewDownloading}"/>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Processing">
                                                        <ic:ChangePropertyAction PropertyName="Source"
                                                                                 Value="{ThemeResource ImageHomeDownloadsViewProcessing}"/>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Finalizing">
                                                        <ic:ChangePropertyAction PropertyName="Source"
                                                                                 Value="{ThemeResource ImageHomeDownloadsViewFinalizing}"/>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="EndedUnsuccessfully">
                                                        <ic:ChangePropertyAction PropertyName="Source"
                                                                                 Value="{ThemeResource ImageHomeDownloadsViewError}"/>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="EndedSuccessfully">
                                                        <ic:ChangePropertyAction PropertyName="Source"
                                                                                 Value="{ThemeResource ImageHomeDownloadsViewDone}"/>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="EndedCancelled">
                                                        <ic:ChangePropertyAction PropertyName="Source"
                                                                                 Value="{ThemeResource ImageHomeDownloadsViewCancelled}"/>
                                                    </ic:DataTriggerBehavior>
                                                </i:Interaction.Behaviors>
                                            </Image>
                                            <Border Grid.Row="3"
                                                    Grid.Column="1"
                                                    VerticalAlignment="Center">
                                                <i:Interaction.Behaviors>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Idle">
                                                        <ic:ChangePropertyAction PropertyName="Child">
                                                            <ic:ChangePropertyAction.Value>
                                                                <TextBlock x:Uid="/HomeDownloadsViewResources/StatusIdle" 
                                                                           FontSize="{StaticResource TextSize}"/>
                                                            </ic:ChangePropertyAction.Value>
                                                        </ic:ChangePropertyAction>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Queued">
                                                        <ic:ChangePropertyAction PropertyName="Child">
                                                            <ic:ChangePropertyAction.Value>
                                                                <TextBlock x:Uid="/HomeDownloadsViewResources/StatusQueued" 
                                                                           FontSize="{StaticResource TextSize}"/>
                                                            </ic:ChangePropertyAction.Value>
                                                        </ic:ChangePropertyAction>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Initializing">
                                                        <ic:ChangePropertyAction PropertyName="Child">
                                                            <ic:ChangePropertyAction.Value>
                                                                <TextBlock x:Uid="/HomeDownloadsViewResources/StatusInitializing" 
                                                                           FontSize="{StaticResource TextSize}"/>
                                                            </ic:ChangePropertyAction.Value>
                                                        </ic:ChangePropertyAction>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Downloading">
                                                        <ic:ChangePropertyAction PropertyName="Child">
                                                            <ic:ChangePropertyAction.Value>
                                                                <StackPanel Orientation="Horizontal">
                                                                    <TextBlock x:Uid="/HomeDownloadsViewResources/StatusDownloading"
                                                                               FontSize="{StaticResource TextSize}"/>
                                                                    <TextBlock Text="{Binding Progress, Converter={StaticResource StringFormatConverter}, ConverterParameter='{} ({0:0.##}%)'}"
                                                                               FontSize="{StaticResource TextSize}"/>
                                                                </StackPanel>
                                                            </ic:ChangePropertyAction.Value>
                                                        </ic:ChangePropertyAction>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Processing">
                                                        <ic:ChangePropertyAction PropertyName="Child">
                                                            <ic:ChangePropertyAction.Value>
                                                                <TextBlock FontSize="{StaticResource TextSize}">
                                                                    <Run x:Uid="/HomeDownloadsViewResources/StatusProcessing"/><Run Text=" ("/><Run Text="{Binding Progress, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:0.##}'}"/>%)
                                                                </TextBlock>
                                                            </ic:ChangePropertyAction.Value>
                                                        </ic:ChangePropertyAction>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="Finalizing">
                                                        <ic:ChangePropertyAction PropertyName="Child">
                                                            <ic:ChangePropertyAction.Value>
                                                                <TextBlock x:Uid="/HomeDownloadsViewResources/StatusFinalizing"
                                                                           FontSize="{StaticResource TextSize}"/>
                                                            </ic:ChangePropertyAction.Value>
                                                        </ic:ChangePropertyAction>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="EndedUnsuccessfully">
                                                        <ic:ChangePropertyAction PropertyName="Child">
                                                            <ic:ChangePropertyAction.Value>
                                                                <TextBlock FontSize="{StaticResource TextSize}">
                                                                    <Run x:Uid="/HomeDownloadsViewResources/StatusError"/><Run Text=" ("/><Run Text="{Binding Error}"/>)
                                                                </TextBlock>
                                                            </ic:ChangePropertyAction.Value>
                                                        </ic:ChangePropertyAction>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="EndedSuccessfully">
                                                        <ic:ChangePropertyAction PropertyName="Child">
                                                            <ic:ChangePropertyAction.Value>
                                                                <TextBlock x:Uid="/HomeDownloadsViewResources/StatusDone"
                                                                           FontSize="{StaticResource TextSize}"/>
                                                            </ic:ChangePropertyAction.Value>
                                                        </ic:ChangePropertyAction>
                                                    </ic:DataTriggerBehavior>
                                                    <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToStringConverter}}"
                                                                            ComparisonCondition="Equal"
                                                                            Value="EndedCancelled">
                                                        <ic:ChangePropertyAction PropertyName="Child">
                                                            <ic:ChangePropertyAction.Value>
                                                                <TextBlock x:Uid="/HomeDownloadsViewResources/StatusCancelled"
                                                                           FontSize="{StaticResource TextSize}"/>
                                                            </ic:ChangePropertyAction.Value>
                                                        </ic:ChangePropertyAction>
                                                    </ic:DataTriggerBehavior>
                                                </i:Interaction.Behaviors>
                                            </Border>
                                        </Grid>
                                    </Grid>
                                    <Grid Grid.Column="2"
                                      Margin="0,0,5,0">
                                        <Grid.RowDefinitions>
                                            <RowDefinition/>
                                            <RowDefinition/>
                                            <RowDefinition/>
                                        </Grid.RowDefinitions>
                                        <AppBarButton Grid.Row="0" 
                                                  Width="40" 
                                                  Height="48">
                                            <i:Interaction.Behaviors>
                                                <ic:EventTriggerBehavior EventName="Click">
                                                    <ctb:NavigateToUriAction NavigateUri="{Binding Video.Url}"/>
                                                </ic:EventTriggerBehavior>
                                                <ic:DataTriggerBehavior Binding="{Binding Video.Source, Converter={StaticResource ObjectToStringConverter}}"
                                                                        ComparisonCondition="Equal"
                                                                        Value="Twitch">
                                                    <ic:ChangePropertyAction PropertyName="Icon">
                                                        <ic:ChangePropertyAction.Value>
                                                            <BitmapIcon ShowAsMonochrome="False" 
                                                                    UriSource="{StaticResource ImageSourcesTwitch}"/>
                                                        </ic:ChangePropertyAction.Value>
                                                    </ic:ChangePropertyAction>
                                                </ic:DataTriggerBehavior>
                                            </i:Interaction.Behaviors>
                                        </AppBarButton>
                                        <AppBarButton Grid.Row="1" 
                                                      Width="40" 
                                                      Height="48"
                                                      Command="{Binding ElementName=Root, Path=DataContext.StartCancelTaskCommand}"
                                                      CommandParameter="{Binding}">
                                            <i:Interaction.Behaviors>
                                                <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToIntConverter}}"
                                                                    ComparisonCondition="LessThan"
                                                                    Value="4">
                                                    <ic:ChangePropertyAction PropertyName="Icon">
                                                        <ic:ChangePropertyAction.Value>
                                                            <SymbolIcon Symbol="Download"/>
                                                        </ic:ChangePropertyAction.Value>
                                                    </ic:ChangePropertyAction>
                                                </ic:DataTriggerBehavior>
                                                <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToIntConverter}}"
                                                                    ComparisonCondition="GreaterThanOrEqual"
                                                                    Value="4">
                                                    <ic:ChangePropertyAction PropertyName="Icon">
                                                        <ic:ChangePropertyAction.Value>
                                                            <SymbolIcon Symbol="Cancel"/>
                                                        </ic:ChangePropertyAction.Value>
                                                    </ic:ChangePropertyAction>
                                                </ic:DataTriggerBehavior>
                                            </i:Interaction.Behaviors>
                                        </AppBarButton>
                                        <AppBarButton Grid.Row="2" 
                                                  Icon="Delete" 
                                                  Width="40" 
                                                  Height="48"
                                                  Command="{Binding ElementName=Root, Path=DataContext.RemoveTaskCommand}"
                                                  CommandParameter="{Binding}"/>
                                    </Grid>
                                </Grid>
                                <ProgressBar Grid.Row="1"
                                         Value="{Binding Progress}"
                                         Maximum="100">
                                    <i:Interaction.Behaviors>
                                        <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToIntConverter}}"
                                                        ComparisonCondition="LessThan"
                                                        Value="5">
                                            <ic:ChangePropertyAction PropertyName="Visibility" Value="Collapsed"/>
                                        </ic:DataTriggerBehavior>
                                        <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToIntConverter}}"
                                                        ComparisonCondition="GreaterThanOrEqual"
                                                        Value="5">
                                            <ic:ChangePropertyAction PropertyName="Visibility" Value="Visible"/>
                                        </ic:DataTriggerBehavior>
                                        <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToIntConverter}}"
                                                            ComparisonCondition="LessThan"
                                                            Value="7">
                                            <ic:ChangePropertyAction PropertyName="IsIndeterminate" Value="True"/>
                                        </ic:DataTriggerBehavior>
                                        <ic:DataTriggerBehavior Binding="{Binding Status, Converter={StaticResource ObjectToIntConverter}}"
                                                            ComparisonCondition="GreaterThanOrEqual"
                                                            Value="7">
                                            <ic:ChangePropertyAction PropertyName="IsIndeterminate" Value="False"/>
                                        </ic:DataTriggerBehavior>
                                    </i:Interaction.Behaviors>
                                </ProgressBar>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
        </ctuc:Case>
    </ctuc:SwitchPresenter>

</Page>
