<UserControl
    x:Class="Bili.App.Controls.Pgc.XboxAnimePageView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:app="using:Bili.App.Controls.App"
    xmlns:behaviors="using:Bili.App.Behaviors"
    xmlns:community="using:Bili.Models.Data.Community"
    xmlns:controls="using:Bili.App.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:loc="using:Bili.App.Resources.Extension"
    xmlns:local="using:Bili.App.Controls.Pgc"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
    xmlns:pgc="using:Bili.ViewModels.Interfaces.Pgc"
    xmlns:pgc1="using:Bili.Models.Data.Pgc"
    xmlns:video="using:Bili.ViewModels.Interfaces.Video"
    xmlns:videos="using:Bili.App.Controls.Videos"
    d:DesignHeight="300"
    d:DesignWidth="400"
    mc:Ignorable="d">

    <Grid RowSpacing="8">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid.Resources>
            <muxc:StackLayout x:Key="HorizontalLayout" Spacing="12" />
        </Grid.Resources>

        <app:PageHeader Title="{x:Bind ViewModel.Title, Mode=OneWay}" IsShowLogo="True" />

        <muxc:NavigationView
            x:Name="RootNavView"
            Style="{StaticResource XboxSideNavigationViewStyle}"
            Grid.Row="1"
            ItemInvoked="OnRootNavViewItemInvoked"
            MenuItemsSource="{x:Bind ViewModel.Partitions}"
            OpenPaneLength="160"
            SelectedItem="{x:Bind ViewModel.CurrentPartition, Mode=OneWay}">
            <muxc:NavigationView.MenuItemTemplate>
                <DataTemplate x:DataType="community:Partition">
                    <muxc:NavigationViewItem Content="{Binding}">
                        <muxc:NavigationViewItem.ContentTemplate>
                            <DataTemplate x:DataType="community:Partition">
                                <TextBlock Style="{StaticResource BodyTextBlockStyle}" Text="{Binding Name}" />
                            </DataTemplate>
                        </muxc:NavigationViewItem.ContentTemplate>
                    </muxc:NavigationViewItem>
                </DataTemplate>
            </muxc:NavigationView.MenuItemTemplate>

            <ScrollViewer
                x:Name="ContentScrollViewer"
                Padding="{x:Bind CoreViewModel.PageHorizontalPadding, Converter={StaticResource HorizontalThicknessConverter}, Mode=OneWay}"
                BringIntoViewOnFocusChange="True"
                HorizontalScrollMode="Disabled"
                VerticalScrollBarVisibility="Auto">
                <Grid x:Name="ContentGrid" XYFocusKeyboardNavigation="Enabled">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Grid
                        Margin="0,0,0,12"
                        TabFocusNavigation="Local"
                        Visibility="{x:Bind ViewModel.IsShowRank, Mode=OneWay}">
                        <controls:HorizontalRepeaterView
                            Header="{loc:Locale Name=Rank}"
                            HeaderVisibility="Visible"
                            IsTabStop="False"
                            ItemsSource="{x:Bind ViewModel.Ranks}"
                            XYFocusKeyboardNavigation="Enabled">
                            <controls:HorizontalRepeaterView.WideItemTemplate>
                                <DataTemplate x:DataType="pgc:IPgcRankViewModel">
                                    <controls:CardPanel
                                        MaxWidth="340"
                                        IsEnableHoverAnimation="False"
                                        IsFocusEngagementEnabled="True">
                                        <Grid Padding="8">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto" />
                                                <RowDefinition Height="*" />
                                            </Grid.RowDefinitions>
                                            <TextBlock
                                                Style="{StaticResource BodyTextBlockStyle}"
                                                Margin="8,0,8,8"
                                                FontWeight="Bold"
                                                Text="{x:Bind Title}" />
                                            <muxc:ItemsRepeater Grid.Row="1" ItemsSource="{x:Bind Episodes}">
                                                <muxc:ItemsRepeater.Layout>
                                                    <muxc:StackLayout Orientation="Vertical" Spacing="0" />
                                                </muxc:ItemsRepeater.Layout>
                                                <muxc:ItemsRepeater.ItemTemplate>
                                                    <DataTemplate x:DataType="pgc:IEpisodeItemViewModel">
                                                        <local:EpisodeItem
                                                            Style="{StaticResource RankEpisodeItemStyle}"
                                                            Width="322"
                                                            Height="120"
                                                            ViewModel="{x:Bind}" />
                                                    </DataTemplate>
                                                </muxc:ItemsRepeater.ItemTemplate>
                                            </muxc:ItemsRepeater>
                                        </Grid>
                                    </controls:CardPanel>
                                </DataTemplate>
                            </controls:HorizontalRepeaterView.WideItemTemplate>
                        </controls:HorizontalRepeaterView>
                    </Grid>

                    <Grid
                        Grid.Row="1"
                        Margin="0,0,0,12"
                        Visibility="{x:Bind ViewModel.IsShowPlaylist, Mode=OneWay}">
                        <muxc:ItemsRepeater ItemsSource="{x:Bind ViewModel.Playlists}" Layout="{StaticResource HorizontalLayout}">
                            <muxc:ItemsRepeater.ItemTemplate>
                                <DataTemplate x:DataType="pgc:IPgcPlaylistViewModel">
                                    <Grid>
                                        <controls:HorizontalRepeaterView
                                            Header="{x:Bind Data.Title}"
                                            HeaderVisibility="Visible"
                                            IsTabStop="False"
                                            ItemsSource="{x:Bind Data.Seasons}"
                                            NarrowHeight="180">
                                            <controls:HorizontalRepeaterView.WideItemTemplate>
                                                <DataTemplate x:DataType="pgc1:SeasonInformation">
                                                    <local:SeasonItem Width="338" Information="{x:Bind}" />
                                                </DataTemplate>
                                            </controls:HorizontalRepeaterView.WideItemTemplate>
                                        </controls:HorizontalRepeaterView>
                                    </Grid>
                                </DataTemplate>
                            </muxc:ItemsRepeater.ItemTemplate>
                        </muxc:ItemsRepeater>
                    </Grid>

                    <controls:VerticalRepeaterView
                        x:Name="VideoView"
                        Grid.Row="2"
                        Margin="0,0,0,12"
                        HeaderVisibility="Collapsed"
                        IsTabStop="False"
                        ItemsSource="{x:Bind ViewModel.Videos}"
                        Visibility="{x:Bind ViewModel.IsShowVideo, Mode=OneWay}">
                        <interactivity:Interaction.Behaviors>
                            <behaviors:IncrementalBehavior />
                        </interactivity:Interaction.Behaviors>
                        <controls:VerticalRepeaterView.ItemTemplate>
                            <DataTemplate x:DataType="video:IVideoItemViewModel">
                                <videos:VideoItem
                                    Style="{StaticResource XboxVerticalVideoItemStyle}"
                                    ContextFlyout="{x:Null}"
                                    IsCustom="True"
                                    ViewModel="{x:Bind}" />
                            </DataTemplate>
                        </controls:VerticalRepeaterView.ItemTemplate>
                    </controls:VerticalRepeaterView>
                </Grid>
            </ScrollViewer>
        </muxc:NavigationView>

        <controls:OverlayLoadingPanel
            Grid.Row="1"
            IsBarActive="{x:Bind ViewModel.IsIncrementalLoading, Mode=OneWay}"
            IsRingActive="{x:Bind ViewModel.IsReloading, Mode=OneWay}" />

        <controls:ErrorPanel
            x:Name="ErrorPanel"
            Grid.Row="1"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            ActionContent="{loc:Locale Name=Refresh}"
            Text="{x:Bind ViewModel.ErrorText, Mode=OneWay}"
            Visibility="{x:Bind ViewModel.IsError, Mode=OneWay}">
            <interactivity:Interaction.Behaviors>
                <behaviors:ReloadBehavior />
            </interactivity:Interaction.Behaviors>
        </controls:ErrorPanel>
    </Grid>
</UserControl>
