<ui:Page x:Class="Ink_Canvas.Windows.SettingsViews.Pages.PluginMarketplacePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
    xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
    xmlns:props="clr-namespace:Ink_Canvas.Properties"
    mc:Ignorable="d"
    d:DesignHeight="600" d:DesignWidth="900"
    Title="{x:Static props:PluginStrings.Market_Title}"
    Loaded="Page_Loaded"
    Unloaded="Page_Unloaded">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>

        <!-- 左侧：搜索 + 标签 + 插件列表 -->
        <DockPanel Grid.Column="0" Margin="16,8,0,8">
            <!-- 搜索框 + 操作按钮 -->
            <Grid DockPanel.Dock="Top" Margin="0,0,0,8">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <TextBox x:Name="SearchBox" Grid.Column="0"
                    TextChanged="SearchBox_TextChanged"
                    KeyDown="SearchBox_KeyDown"
                    Margin="0,0,8,0" Padding="8,6"/>
                <StackPanel Grid.Column="1" Orientation="Horizontal">
                    <Button ToolTip="{x:Static props:PluginStrings.Market_Refresh}"
                        Click="RefreshButton_Click" Padding="8,6" Margin="0,0,4,0">
                        <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Refresh}" FontSize="14"/>
                    </Button>
                    <Button ToolTip="{x:Static props:PluginStrings.Market_OpenPluginsFolder}"
                        Click="OpenFolderButton_Click" Padding="8,6" Margin="0,0,4,0">
                        <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.FolderOpen}" FontSize="14"/>
                    </Button>
                    <Button ToolTip="{x:Static props:PluginStrings.Market_ManageSources}"
                        Click="ManageSourcesButton_Click" Padding="8,6" Margin="0,0,4,0">
                        <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Globe}" FontSize="14"/>
                    </Button>
                    <Button ToolTip="{x:Static props:PluginStrings.Market_InstallFromLocal}"
                        Click="InstallLocalButton_Click" Padding="8,6">
                        <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" FontSize="14"/>
                    </Button>
                </StackPanel>
            </Grid>

            <!-- 加载条 -->
            <ProgressBar x:Name="LoadingBar" DockPanel.Dock="Top"
                IsIndeterminate="False" Visibility="Collapsed"
                Margin="0,0,0,4" Height="2"/>

            <!-- 当前源 + 镜像选择 -->
            <Grid DockPanel.Dock="Top" Margin="0,0,0,4">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <ComboBox x:Name="SourceCombo" Grid.Column="0"
                    SelectionChanged="SourceCombo_SelectionChanged"
                    Padding="8,6"/>
                <ComboBox x:Name="MirrorCombo" Grid.Column="1" Margin="6,0,0,0"
                    MinWidth="120"
                    SelectionChanged="MirrorCombo_SelectionChanged"
                    ToolTip="{x:Static props:PluginStrings.Market_MirrorGroup}"
                    Padding="8,6"/>
            </Grid>

            <!-- 插件列表（代码填充） -->
            <ui:ListView x:Name="PluginListBox"
                SelectionChanged="PluginListBox_SelectionChanged"
                BorderThickness="0"
                Background="Transparent"
                HorizontalContentAlignment="Stretch"
                VirtualizingPanel.ScrollUnit="Pixel"
                ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <ui:ListView.Template>
                    <ControlTemplate TargetType="ui:ListView">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                            <ScrollViewer HorizontalScrollBarVisibility="Disabled"
                                          VerticalScrollBarVisibility="Auto">
                                <ItemsPresenter HorizontalAlignment="Stretch" />
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </ui:ListView.Template>
                <ui:ListView.ItemContainerStyle>
                    <Style TargetType="ui:ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        <Setter Property="Padding" Value="0" />
                        <Setter Property="MinHeight" Value="36" />
                    </Style>
                </ui:ListView.ItemContainerStyle>
                <ui:ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel />
                    </ItemsPanelTemplate>
                </ui:ListView.ItemsPanel>
            </ui:ListView>
        </DockPanel>

        <!-- 分隔线 -->
        <Rectangle Grid.Column="1" Width="1"
            Fill="{DynamicResource DividerStrokeColorDefaultBrush}" Margin="0,16,0,16"/>

        <!-- 右侧：插件详情 -->
        <ScrollViewer x:Name="DetailScrollViewer" Grid.Column="2" VerticalScrollBarVisibility="Auto" Margin="16,8,16,8">
            <StackPanel>
                <!-- 空状态 -->
                <StackPanel x:Name="EmptyStatePanel"
                    VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,80,0,0">
                    <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Puzzle}" FontSize="48"
                        Margin="0,0,0,8" Opacity="0.4" HorizontalAlignment="Center"/>
                    <TextBlock Text="{x:Static props:PluginStrings.Market_SelectPluginHint}"
                        FontSize="14" Foreground="{DynamicResource TextFillColorTertiaryBrush}"
                        HorizontalAlignment="Center"/>
                </StackPanel>

                <!-- 详情内容 -->
                <StackPanel x:Name="DetailPanel" Visibility="Collapsed">
                    <!-- 头部：图标 + 名称 + 版本 + 作者 -->
                    <Grid Margin="0,0,0,12">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0" Width="64" Height="64" CornerRadius="10"
                            Background="{DynamicResource ControlFillColorDefaultBrush}" Margin="0,0,16,0">
                            <ui:FontIcon x:Name="DetailIcon" Icon="{x:Static ui:SegoeFluentIcons.Puzzle}"
                                FontSize="32" HorizontalAlignment="Center" VerticalAlignment="Center"
                                Opacity="0.6"/>
                        </Border>
                        <StackPanel Grid.Column="1" VerticalAlignment="Center">
                            <TextBlock x:Name="DetailName" FontSize="22" FontWeight="SemiBold"
                                TextTrimming="CharacterEllipsis"/>
                            <StackPanel Orientation="Horizontal" Margin="0,4,0,4">
                                <TextBlock x:Name="DetailVersion" FontSize="13"
                                    Foreground="{DynamicResource TextFillColorSecondaryBrush}" Margin="0,0,8,0"/>
                                <TextBlock x:Name="DetailAuthor" FontSize="13"
                                    Foreground="{DynamicResource TextFillColorSecondaryBrush}" Margin="0,0,8,0"/>
                                <ui:HyperlinkButton x:Name="DetailUrl"
                                    Content="{x:Static props:PluginStrings.Market_ProjectHomepage}"/>
                            </StackPanel>
                            <TextBlock x:Name="DetailDescription" FontSize="13" TextWrapping="Wrap"
                                Foreground="{DynamicResource TextFillColorSecondaryBrush}"/>
                        </StackPanel>
                    </Grid>

                    <!-- 操作按钮 -->
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,16">
                        <Button x:Name="DetailInstallBtn" Click="DetailInstall_Click" Padding="14,8" Margin="0,0,8,0">
                            <StackPanel Orientation="Horizontal">
                                <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Download}" FontSize="14" Margin="0,0,6,0"/>
                                <TextBlock Text="{x:Static props:PluginStrings.Market_Install}" VerticalAlignment="Center"/>
                            </StackPanel>
                        </Button>
                        <Button x:Name="DetailUpdateBtn" Click="DetailInstall_Click" Padding="14,8" Margin="0,0,8,0">
                            <StackPanel Orientation="Horizontal">
                                <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Upload}" FontSize="14" Margin="0,0,6,0"/>
                                <TextBlock Text="{x:Static props:PluginStrings.Market_Update}" VerticalAlignment="Center"/>
                            </StackPanel>
                        </Button>
                        <Button x:Name="DetailRestartBtn" Click="DetailApplyPending_Click" Padding="14,8" Margin="0,0,8,0">
                            <StackPanel Orientation="Horizontal">
                                <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Refresh}" FontSize="14" Margin="0,0,6,0"/>
                                <TextBlock Text="{x:Static props:PluginStrings.Market_ApplyPendingUpdate}" VerticalAlignment="Center"/>
                            </StackPanel>
                        </Button>
                        <ProgressBar x:Name="DetailProgress" Width="120" Height="4" VerticalAlignment="Center"/>
                    </StackPanel>

                    <!-- 统计信息 -->
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,16">
                        <StackPanel Orientation="Horizontal" Margin="0,0,16,0">
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Download}" FontSize="14"
                                Margin="0,0,4,0" Opacity="0.6"/>
                            <TextBlock x:Name="DetailDownloadCount" FontSize="12"
                                Foreground="{DynamicResource TextFillColorTertiaryBrush}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.FavoriteStar}" FontSize="14"
                                Margin="0,0,4,0" Opacity="0.6"/>
                            <TextBlock x:Name="DetailStarsCount" FontSize="12"
                                Foreground="{DynamicResource TextFillColorTertiaryBrush}"/>
                        </StackPanel>
                    </StackPanel>

                    <!-- 依赖列表 -->
                    <TextBlock x:Name="DetailDepsHeader" Text="{x:Static props:PluginStrings.Market_TabDependencies}"
                        FontSize="14" FontWeight="SemiBold" Margin="0,0,0,8" Visibility="Collapsed"/>
                    <ItemsControl x:Name="DetailDependencies" Margin="0,0,0,16">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="0,2">
                                    <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Puzzle}" FontSize="12" Margin="0,0,6,0"/>
                                    <TextBlock Text="{Binding Id}" FontSize="12" VerticalAlignment="Center" Margin="0,0,6,0"/>
                                    <TextBlock FontSize="11" VerticalAlignment="Center"
                                        Foreground="{DynamicResource TextFillColorTertiaryBrush}"
                                        Text="{Binding Version, Mode=OneWay}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

                    <!-- 说明文档 -->
                    <TextBlock x:Name="DetailReadmeHeader" Text="{x:Static props:PluginStrings.Market_TabOverview}"
                        FontSize="14" FontWeight="SemiBold" Margin="0,0,0,8"/>
                    <Border Background="{DynamicResource CardBackgroundFillColorDefaultBrush}"
                        CornerRadius="8" Padding="0"
                        PreviewMouseWheel="DetailReadmeContainer_PreviewMouseWheel">
                        <Grid>
                            <FlowDocumentScrollViewer x:Name="DetailReadmeViewer"
                                Background="Transparent"
                                VerticalScrollBarVisibility="Disabled"
                                HorizontalScrollBarVisibility="Disabled"
                                Padding="12" MinHeight="120" />
                            <TextBlock x:Name="DetailReadmeFallback" Visibility="Collapsed"
                                TextWrapping="Wrap" FontSize="13" Padding="12"
                                Foreground="{DynamicResource TextFillColorPrimaryBrush}"
                                Text="{x:Static props:PluginStrings.Market_NoReadme}"/>
                        </Grid>
                    </Border>
                </StackPanel>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</ui:Page>
