<?xml version="1.0" encoding="utf-8"?>
<Page
    x:Class="VDownload.Core.Views.Home.HomeView"
    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:i="using:Microsoft.Xaml.Interactivity" 
    xmlns:ic="using:Microsoft.Xaml.Interactions.Core"
    xmlns:ctuc="using:CommunityToolkit.WinUI.UI.Controls"
    mc:Ignorable="d"
    Background="Transparent"
    x:Name="Root">
    <i:Interaction.Behaviors>
        <ic:EventTriggerBehavior EventName="Loaded">
            <ic:InvokeCommandAction Command="{Binding NavigationCommand}"/>
        </ic:EventTriggerBehavior>
    </i:Interaction.Behaviors>
    <Grid RowSpacing="10"
          Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Frame Content="{Binding MainContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ViewModelToViewConverter}}">
            
        </Frame>
        <ctuc:SwitchPresenter Grid.Row="1"
                              Value="{Binding OptionBarError, Converter={StaticResource IsNotNullConverter}}">
            <ctuc:Case Value="False">
                <Grid Background="{ThemeResource OptionBarBackgroundColor}"
                      CornerRadius="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="50"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <ctuc:UniformGrid Grid.Column="0" 
                                      Rows="1" 
                                      Margin="15,0,0,0">
                        <ctuc:UniformGrid.RowDefinitions>
                            <RowDefinition/>
                        </ctuc:UniformGrid.RowDefinitions>
                        <ctuc:UniformGrid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="Auto"/>
                        </ctuc:UniformGrid.ColumnDefinitions>
                        <ctuc:SwitchPresenter Grid.Row="0" 
                                              VerticalAlignment="Stretch" 
                                              Margin="0,0,15,0" 
                                              Value="{Binding OptionBarContent, Converter={StaticResource ObjectToStringConverter}}">
                            <i:Interaction.Behaviors>
                                <ic:DataTriggerBehavior Binding="{Binding OptionBarContent, Converter={StaticResource ObjectToStringConverter}}"
                                                        ComparisonCondition="Equal"
                                                        Value="None">
                                    <ic:ChangePropertyAction PropertyName="Visibility"
                                                             Value="Collapsed"/>
                                </ic:DataTriggerBehavior>
                                <ic:DataTriggerBehavior Binding="{Binding OptionBarContent, Converter={StaticResource ObjectToStringConverter}}"
                                                        ComparisonCondition="NotEqual"
                                                        Value="None">
                                    <ic:ChangePropertyAction PropertyName="Visibility"
                                                             Value="Visible"/>
                                </ic:DataTriggerBehavior>
                            </i:Interaction.Behaviors>
                            <ctuc:Case Value="VideoSearch">
                                <Grid ColumnSpacing="10" 
                                      VerticalAlignment="Stretch">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBox x:Uid="/HomeViewResources/OptionBarVideoSearchContentTextBox"
                                             Grid.Column="0"
                                             VerticalAlignment="Center"
                                             Text="{Binding OptionBarVideoSearchTBValue, Mode=TwoWay}"/>
                                    <Button x:Uid="/HomeViewResources/OptionBarSearchButton"
                                            Grid.Column="1"
                                            IsEnabled="{Binding OptionBarSearchNotPending}"
                                            Command="{Binding VideoSearchStartCommand}"/>
                                </Grid>
                            </ctuc:Case>
                            <ctuc:Case Value="PlaylistSearch">
                                <Grid ColumnSpacing="10">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBox x:Uid="/HomeViewResources/OptionBarPlaylistSearchContentTextBox"
                                             Grid.Column="0" 
                                             VerticalAlignment="Center"
                                             Text="{Binding OptionBarPlaylistSearchTBValue, Mode=TwoWay}"/>
                                    <NumberBox x:Uid="/HomeViewResources/OptionBarPlaylistSearchContentNumberBox"
                                               Grid.Column="1" 
                                               VerticalAlignment="Center"
                                               SpinButtonPlacementMode="Compact" 
                                               SmallChange="1" 
                                               LargeChange="10" 
                                               Value="{Binding OptionBarPlaylistSearchNBValue, Mode=TwoWay}"
                                               Minimum="0"/>
                                    <Button x:Uid="/HomeViewResources/OptionBarSearchButton"
                                            Grid.Column="2"
                                            IsEnabled="{Binding OptionBarSearchNotPending}"
                                            Command="{Binding PlaylistSearchStartCommand}"/>
                                </Grid>
                            </ctuc:Case>
                        </ctuc:SwitchPresenter>
                        <StackPanel VerticalAlignment="Center" 
                                    Orientation="Horizontal">
                            <ProgressRing Width="20" 
                                          Height="20" 
                                          Margin="0,0,10,0"
                                          Visibility="{Binding OptionBarLoading, Converter={StaticResource BoolToVisibilityConverter}}"/>
                            <TextBlock Text="{Binding OptionBarMessage}"/>
                        </StackPanel>
                    </ctuc:UniformGrid>
                    <StackPanel Grid.Column="2" 
                                Orientation="Horizontal"
                                Margin="0,0,4,0">
                        <AppBarToggleButton x:Uid="/HomeViewResources/OptionBarLoadSubscription"
                                            Icon="Favorite"
                                            IsEnabled="{Binding OptionBarSearchNotPending}"
                                            IsChecked="{Binding OptionBarLoadSubscriptionButtonChecked, Mode=TwoWay}"
                                            Command="{Binding LoadFromSubscriptionCommand}"/>
                        <AppBarToggleButton x:Uid="/HomeViewResources/OptionBarVideoSearch"
                                            Icon="Video"
                                            IsEnabled="{Binding OptionBarSearchNotPending}"
                                            IsChecked="{Binding OptionBarVideoSearchButtonChecked, Mode=TwoWay}"
                                            Command="{Binding VideoSearchShowCommand}"/>
                        <AppBarToggleButton x:Uid="/HomeViewResources/OptionBarPlaylistSearch"
                                            Icon="List"
                                            IsEnabled="{Binding OptionBarSearchNotPending}"
                                            IsChecked="{Binding OptionBarPlaylistSearchButtonChecked, Mode=TwoWay}"
                                            Command="{Binding PlaylistSearchShowCommand}"/>
                        <AppBarSeparator/>
                        <AppBarButton x:Uid="/HomeViewResources/OptionBarCancelAll"
                                      Icon="Cancel"
                                      Command="{Binding CancelCommand}"/>
                        <AppBarButton x:Uid="/HomeViewResources/OptionBarDownloadAll"
                                      Icon="Download"
                                      Command="{Binding DownloadCommand}"/>
                    </StackPanel>
                </Grid>
            </ctuc:Case>
            <ctuc:Case Value="True">
                <InfoBar x:Uid="/HomeViewResources/ErrorInfoBar"
                         Severity="Error"
                         IsOpen="{Binding OptionBarIsErrorOpened, Mode=TwoWay}"
                         Message="{Binding OptionBarError}"
                         CloseButtonCommand="{Binding CloseErrorCommand}"/>
            </ctuc:Case>
        </ctuc:SwitchPresenter>
        
    </Grid>
</Page>
