<Grid
    x:Class="Pixeval.Controls.NovelItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:CommunityToolkit.WinUI.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:labs="using:CommunityToolkit.Labs.WinUI.MarkdownTextBlock"
    xmlns:local="using:Pixeval.Controls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:model="using:Pixeval.CoreApi.Model"
    xmlns:pixeval="using:Pixeval.AppManagement"
    Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
    PointerEntered="NovelItem_OnPointerEntered"
    PointerExited="NovelItem_OnPointerExited"
    mc:Ignorable="d">
    <Grid.Resources>
        <ExponentialEase
            x:Key="EasingFunction"
            EasingMode="EaseOut"
            Exponent="12" />
        <!--  ReSharper disable once Xaml.RedundantResource  -->
        <Storyboard x:Key="ThumbnailStoryboard">
            <DoubleAnimation
                EasingFunction="{StaticResource EasingFunction}"
                Storyboard.TargetName="Image"
                Storyboard.TargetProperty="Opacity"
                From="0"
                To="1"
                Duration="0:0:2" />
        </Storyboard>
    </Grid.Resources>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <controls:ConstrainedBox Grid.Column="0" AspectRatio="427:600">
        <local:LazyImage
            x:Name="Image"
            CornerRadius="{StaticResource ControlLeftCornerRadius}"
            Source="{x:Bind ViewModel.ThumbnailSource, Mode=OneWay}" />
    </controls:ConstrainedBox>
    <local:HeartButton
        x:Name="HeartButton"
        Grid.Column="0"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Command="{x:Bind ViewModel.BookmarkCommand, Mode=OneWay}"
        CommandParameter="{x:Bind}"
        IsTapEnabled="False"
        State="{x:Bind ViewModel.IsBookmarkedDisplay, Mode=OneWay}" />
    <controls:DockPanel
        Grid.Column="1"
        Padding="10"
        HorizontalSpacing="10"
        VerticalSpacing="10">
        <TextBlock
            x:Name="TitleTextBlock"
            controls:DockPanel.Dock="Top"
            MaxLines="3"
            Style="{StaticResource CaptionStrongTextBlockStyle}"
            Text="{x:Bind ViewModel.Title, Mode=OneWay}"
            TextTrimming="CharacterEllipsis"
            TextWrapping="WrapWholeWords" />
        <controls:DockPanel controls:DockPanel.Dock="Top">
            <TextBlock
                controls:DockPanel.Dock="Right"
                Foreground="DarkGray"
                MaxLines="1"
                Style="{StaticResource BodyTextBlockStyle}"
                Text="{x:Bind local:C.CultureDateTimeOffsetDateFormatter(ViewModel.PublishDate, pixeval:AppSettings.CurrentCulture), Mode=OneWay}"
                TextTrimming="CharacterEllipsis"
                TextWrapping="WrapWholeWords" />
            <TextBlock
                x:Name="AuthorTextBlock"
                Foreground="DarkGray"
                MaxLines="1"
                Style="{StaticResource BodyTextBlockStyle}"
                Text="{x:Bind ViewModel.User.Name, Mode=OneWay}"
                TextTrimming="CharacterEllipsis"
                TextWrapping="WrapWholeWords" />
        </controls:DockPanel>
        <controls:DockPanel
            controls:DockPanel.Dock="Bottom"
            HorizontalSpacing="10"
            LastChildFill="False">
            <local:IconText
                controls:DockPanel.Dock="Right"
                Symbol="TextAlignLeft"
                Text="{x:Bind local:C.IntEllipsis(ViewModel.TextLength), Mode=OneWay}" />
            <local:IconText
                controls:DockPanel.Dock="Right"
                Symbol="Heart"
                Text="{x:Bind local:C.IntEllipsis(ViewModel.TotalBookmarks), Mode=OneWay}" />
            <local:IconText
                controls:DockPanel.Dock="Right"
                Symbol="Eye"
                Text="{x:Bind local:C.IntEllipsis(ViewModel.TotalView), Mode=OneWay}" />
        </controls:DockPanel>
        <controls:DockPanel
            controls:DockPanel.Dock="Bottom"
            HorizontalSpacing="3"
            LastChildFill="False">
            <local:PixevalBadge
                controls:DockPanel.Dock="Right"
                Mode="Ai"
                Visibility="{x:Bind local:C.ToVisibility(ViewModel.IsAiGenerated), Mode=OneWay}" />
            <local:PixevalBadge
                controls:DockPanel.Dock="Right"
                Mode="{x:Bind ViewModel.XRestrictionCaption, Mode=OneWay}"
                Visibility="{x:Bind local:C.ToVisibility(ViewModel.IsXRestricted), Mode=OneWay}" />
        </controls:DockPanel>
        <ItemsControl
            x:Name="TagsList"
            controls:DockPanel.Dock="Top"
            ItemsSource="{x:Bind ViewModel.Tags, Mode=OneWay}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <controls:WrapPanel HorizontalSpacing="5" VerticalSpacing="5" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate x:DataType="model:Tag">
                    <Border
                        Padding="2"
                        Background="#7F7F7F7F"
                        CornerRadius="{StaticResource ControlCornerRadius}">
                        <TextBlock Style="{StaticResource CaptionTextBlockStyle}" Text="{x:Bind Name}" />
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </controls:DockPanel>
    <Popup
        x:Name="NovelItemPopup"
        Grid.Column="0"
        HorizontalOffset="-5"
        VerticalOffset="-5">
        <!--  310 是实际试出的高度  -->
        <Grid
            Height="310"
            Padding="10"
            Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
            ColumnSpacing="10"
            CornerRadius="{StaticResource ControlCornerRadius}"
            PointerEntered="NovelItemPopup_OnPointerEntered"
            PointerExited="NovelItem_OnPointerExited">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <controls:DockPanel Grid.Column="0" VerticalSpacing="5">
                <CommandBar
                    Margin="-5"
                    HorizontalAlignment="Left"
                    controls:DockPanel.Dock="Bottom"
                    DefaultLabelPosition="Collapsed"
                    OverflowButtonVisibility="Collapsed">
                    <AppBarButton Click="OpenNovel_OnClicked" Command="{x:Bind OpenNovelCommand}" />
                    <AppBarButton Click="AddToBookmark_OnClicked" Command="{x:Bind ViewModel.AddToBookmarkCommand, Mode=OneWay}" />
                    <AppBarButton Command="{x:Bind ViewModel.SaveCommand, Mode=OneWay}" CommandParameter="{x:Bind}" />
                    <AppBarButton Command="{x:Bind ViewModel.OpenInWebBrowserCommand, Mode=OneWay}" />
                </CommandBar>
                <Grid>
                    <controls:ConstrainedBox Grid.Row="0" AspectRatio="427:600">
                        <local:LazyImage x:Name="PopupImage" Source="{x:Bind ViewModel.ThumbnailSource, Mode=OneWay}" />
                    </controls:ConstrainedBox>
                    <local:HeartButton
                        x:Name="PopupHeartButton"
                        Grid.Row="0"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Command="{x:Bind ViewModel.BookmarkCommand, Mode=OneWay}"
                        CommandParameter="{x:Bind}"
                        State="{x:Bind ViewModel.IsBookmarkedDisplay, Mode=OneWay}" />
                </Grid>
            </controls:DockPanel>
            <controls:DockPanel
                Grid.Column="1"
                HorizontalSpacing="10"
                VerticalSpacing="10">
                <TextBlock
                    x:Name="PopupTitleTextBlock"
                    controls:DockPanel.Dock="Top"
                    MaxLines="1"
                    Style="{StaticResource CaptionStrongTextBlockStyle}"
                    Text="{x:Bind ViewModel.Title, Mode=OneWay}"
                    TextTrimming="CharacterEllipsis"
                    TextWrapping="WrapWholeWords" />
                <Button
                    x:Name="PopupAuthorButton"
                    Padding="2"
                    controls:DockPanel.Dock="Top"
                    Background="Transparent"
                    BorderBrush="Transparent"
                    Click="OpenUserInfoPage_OnClicked"
                    CornerRadius="{StaticResource ControlCornerRadius}">
                    <TextBlock
                        Foreground="DarkGray"
                        MaxLines="1"
                        Style="{StaticResource BodyTextBlockStyle}"
                        Text="{x:Bind ViewModel.User.Name, Mode=OneWay}"
                        TextTrimming="CharacterEllipsis"
                        TextWrapping="WrapWholeWords" />
                </Button>
                <ScrollView>
                    <StackPanel Spacing="10">
                        <ItemsControl
                            x:Name="PopupTagsList"
                            controls:DockPanel.Dock="Top"
                            ItemsSource="{x:Bind ViewModel.Tags, Mode=OneWay}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <controls:WrapPanel HorizontalSpacing="5" VerticalSpacing="5" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate x:DataType="model:Tag">
                                    <Button
                                        Padding="2"
                                        Background="#7F7F7F7F"
                                        Click="TagButton_OnClicked"
                                        CornerRadius="{StaticResource ControlCornerRadius}">
                                        <TextBlock Style="{StaticResource CaptionTextBlockStyle}" Text="{x:Bind Name}" />
                                    </Button>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                        <labs:MarkdownTextBlock Text="{x:Bind ViewModel.Caption}">
                            <labs:MarkdownTextBlock.Config>
                                <labs:MarkdownConfig />
                            </labs:MarkdownTextBlock.Config>
                        </labs:MarkdownTextBlock>
                    </StackPanel>
                </ScrollView>
            </controls:DockPanel>
        </Grid>
    </Popup>
</Grid>
