<base:SearchPageBase
    x:Class="Bili.App.Pages.Desktop.Overlay.SearchPage"
    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:base="using:Bili.App.Pages.Base"
    xmlns:behaviors="using:Bili.App.Behaviors"
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
    xmlns:search="using:Bili.ViewModels.Interfaces.Search"
    mc:Ignorable="d">

    <interactivity:Interaction.Behaviors>
        <behaviors:InitializeBehavior />
    </interactivity:Interaction.Behaviors>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="LayoutGroup">
                <VisualState x:Name="WideState">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="{StaticResource MediumWindowThresholdWidth}" />
                    </VisualState.StateTriggers>
                </VisualState>
                <VisualState x:Name="NarrowState">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="FilterRepeaterLayout.ItemStretch" Value="Fill" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <app:PageHeader>
            <app:PageHeader.Title>
                <TextBlock
                    Style="{StaticResource TitleTextBlockStyle}"
                    Margin="0,0,0,8"
                    HorizontalAlignment="Left"
                    TextTrimming="CharacterEllipsis">
                    <Run Text="{loc:Locale Name=Search}" />
                    <Run
                        FontWeight="Bold"
                        Foreground="{ThemeResource AccentTextFillColorPrimaryBrush}"
                        Text="{x:Bind ViewModel.Keyword, Mode=OneWay}" />
                </TextBlock>
            </app:PageHeader.Title>
        </app:PageHeader>

        <muxc:NavigationView
            x:Name="Nav"
            Style="{StaticResource TopNavigationViewStyle}"
            Grid.Row="1"
            Margin="{x:Bind CoreViewModel.PageHorizontalPadding, Converter={StaticResource HorizontalThicknessConverter}, Mode=OneWay}"
            ItemInvoked="OnNavItemInvokedAsync"
            MenuItemsSource="{x:Bind ViewModel.Items}"
            SelectedItem="{x:Bind ViewModel.CurrentModule, Mode=OneWay}">
            <muxc:NavigationView.MenuItemTemplate>
                <DataTemplate x:DataType="search:ISearchModuleItemViewModel">
                    <muxc:NavigationViewItem Content="{x:Bind}" IsEnabled="{x:Bind IsEnabled, Mode=OneWay}">
                        <muxc:NavigationViewItem.ContentTemplate>
                            <DataTemplate x:DataType="search:ISearchModuleItemViewModel">
                                <TextBlock Text="{x:Bind Title, Mode=OneWay}" />
                            </DataTemplate>
                        </muxc:NavigationViewItem.ContentTemplate>
                    </muxc:NavigationViewItem>
                </DataTemplate>
            </muxc:NavigationView.MenuItemTemplate>
        </muxc:NavigationView>

        <Grid
            x:Name="FilterContainer"
            Grid.Row="2"
            Margin="0,12,0,0"
            Padding="{x:Bind CoreViewModel.PageHorizontalPadding, Converter={StaticResource HorizontalThicknessConverter}, Mode=OneWay}"
            Visibility="{x:Bind ViewModel.IsCurrentFilterEmpty, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}">
            <muxc:ItemsRepeater x:Name="FilterRepeater" ItemsSource="{x:Bind ViewModel.CurrentFilters}">
                <muxc:ItemsRepeater.Layout>
                    <muxc:UniformGridLayout
                        x:Name="FilterRepeaterLayout"
                        ItemsStretch="Uniform"
                        MinColumnSpacing="12"
                        MinItemWidth="120"
                        MinRowSpacing="12" />
                </muxc:ItemsRepeater.Layout>
                <muxc:ItemsRepeater.ItemTemplate>
                    <DataTemplate x:DataType="search:ISearchFilterViewModel">
                        <ComboBox
                            AutomationProperties.Name="{x:Bind Filter.Name}"
                            HorizontalAlignment="Stretch"
                            DataContext="{x:Bind}"
                            ItemsSource="{x:Bind Filter.Conditions}"
                            PlaceholderText="{x:Bind Filter.Name}"
                            SelectedItem="{x:Bind CurrentCondition, Mode=OneWay}"
                            SelectionChanged="OnFilterItemSelectionChanged" />
                    </DataTemplate>
                </muxc:ItemsRepeater.ItemTemplate>
            </muxc:ItemsRepeater>
        </Grid>

        <Grid
            x:Name="ContentContainer"
            Grid.Row="3"
            Margin="0,8,0,0">
            <Grid Margin="0,0,0,12">
                <controls:SearchVideoView
                    x:Name="VideoView"
                    DataContext="{x:Bind ViewModel}"
                    ItemsSource="{x:Bind ViewModel.Videos}"
                    Visibility="{x:Bind ViewModel.IsVideoModuleShown, Mode=OneWay}" />
                <controls:SearchPgcView
                    x:Name="BangumiView"
                    DataContext="{x:Bind ViewModel}"
                    ItemsSource="{x:Bind ViewModel.Animes}"
                    Visibility="{x:Bind ViewModel.IsAnimeModuleShown, Mode=OneWay}" />
                <controls:SearchPgcView
                    x:Name="MovieView"
                    DataContext="{x:Bind ViewModel}"
                    ItemsSource="{x:Bind ViewModel.Movies}"
                    Visibility="{x:Bind ViewModel.IsMovieModuleShown, Mode=OneWay}" />
                <controls:SearchArticleView
                    x:Name="ArticleView"
                    DataContext="{x:Bind ViewModel}"
                    ItemsSource="{x:Bind ViewModel.Articles}"
                    Visibility="{x:Bind ViewModel.IsArticleModuleShown, Mode=OneWay}" />
                <controls:SearchUserView
                    x:Name="UserView"
                    DataContext="{x:Bind ViewModel}"
                    ItemsSource="{x:Bind ViewModel.Users}"
                    Visibility="{x:Bind ViewModel.IsUserModuleShown, Mode=OneWay}" />
                <controls:SearchLiveView
                    x:Name="LiveView"
                    DataContext="{x:Bind ViewModel}"
                    ItemsSource="{x:Bind ViewModel.Lives}"
                    Visibility="{x:Bind ViewModel.IsLiveModuleShown, Mode=OneWay}" />
            </Grid>
        </Grid>

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

        <controls:OverlayLoadingPanel
            Grid.Row="3"
            IsBarActive="False"
            IsRingActive="{x:Bind ViewModel.IsReloadingModule, Mode=OneWay}" />

        <controls:ErrorPanel
            Grid.Row="3"
            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>

        <controls:ErrorPanel
            Grid.Row="3"
            Text="{loc:Locale Name=NoSpecificData}"
            Visibility="{x:Bind ViewModel.IsCurrentContentEmpty, Mode=OneWay}" />
    </Grid>
</base:SearchPageBase>
