<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="using:ZonyLrcTools.Desktop.ViewModels"
             xmlns:loc="using:ZonyLrcTools.Desktop.Infrastructure.Localization"
             x:Class="ZonyLrcTools.Desktop.Views.Pages.AlbumDownloadPage"
             x:DataType="vm:AlbumDownloadViewModel">

    <Grid RowDefinitions="Auto,Auto,*,Auto">
        <!-- Title -->
        <StackPanel Grid.Row="0" Margin="0,0,0,20">
            <TextBlock Text="{Binding AlbumTitle}"
                       FontSize="24"
                       FontWeight="Bold" />
            <TextBlock Text="{Binding AlbumDescription}"
                       FontSize="13"
                       Opacity="0.6"
                       Margin="0,4,0,0" />
        </StackPanel>

        <!-- Card 1: Form Controls -->
        <Border Grid.Row="1"
                Background="{DynamicResource CardBackgroundBrush}"
                CornerRadius="8"
                BoxShadow="0 2 8 0 #0D000000"
                Padding="24,20"
                Margin="0,0,0,16">
            <Grid RowDefinitions="Auto,Auto,Auto,Auto" ColumnDefinitions="Auto,*,Auto"
                  >
                <!-- Row 0: Folder Selection -->
                <TextBlock Grid.Row="0" Grid.Column="0"
                           Text="{Binding AlbumFolderLabel}"
                           VerticalAlignment="Center"
                           FontSize="13"
                           FontWeight="Medium" />
                <TextBox Grid.Row="0" Grid.Column="1"
                         Text="{Binding SelectedFolderPath}"
                         Watermark="{Binding AlbumSelectFolder}"
                         IsReadOnly="True"
                         Margin="16,0" />
                <Button Grid.Row="0" Grid.Column="2"
                        Content="{Binding AlbumBrowse}"
                        Command="{Binding SelectFolderCommand}"
                        MinWidth="80"
                        HorizontalAlignment="Stretch" />

                <!-- Separator -->
                <Border Grid.Row="1" Grid.ColumnSpan="3"
                        Height="1"
                        Background="{DynamicResource SeparatorBrush}"
                        Opacity="0.5"
                        Margin="0,16" />

                <!-- Row 2: Parallel Count + Download Button -->
                <TextBlock Grid.Row="2" Grid.Column="0"
                           Text="{Binding AlbumParallel}"
                           VerticalAlignment="Center"
                           FontSize="13"
                           FontWeight="Medium" />
                <NumericUpDown Grid.Row="2" Grid.Column="1"
                               Value="{Binding ParallelCount}"
                               Minimum="1"
                               Maximum="32"
                               Increment="1"
                               Width="100"
                               FormatString="0"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Center"
                               Margin="16,0" />
                <StackPanel Grid.Row="2" Grid.Column="2"
                            Orientation="Horizontal" Spacing="8">
                    <Button Command="{Binding StartDownloadCommand}"
                            IsVisible="{Binding !IsDownloading}"
                            Classes="accent"
                            MinWidth="120">
                        <StackPanel Orientation="Horizontal" Spacing="6">
                            <PathIcon Data="{StaticResource DownloadIcon}"
                                      Width="14" Height="14" />
                            <TextBlock Text="{Binding AlbumStartDownload}"
                                       VerticalAlignment="Center" />
                        </StackPanel>
                    </Button>
                    <Button Command="{Binding CancelDownloadCommand}"
                            Content="{Binding AlbumStopDownload}"
                            IsVisible="{Binding IsDownloading}"
                            MinWidth="100" />
                </StackPanel>

                <!-- Row 3: Progress -->
                <StackPanel Grid.Row="3" Grid.ColumnSpan="3"
                            IsVisible="{Binding IsDownloading}"
                            Spacing="8" Margin="0,16,0,0">
                    <ProgressBar Value="{Binding ProgressPercentage}"
                                 Maximum="100" Height="6" />
                    <TextBlock HorizontalAlignment="Center" FontSize="12" Opacity="0.7">
                        <Run Text="{Binding CompletedCount}" />
                        <Run Text=" / " />
                        <Run Text="{Binding TotalCount}" />
                    </TextBlock>
                </StackPanel>
            </Grid>
        </Border>

        <!-- Card 2: File List with Empty State -->
        <Panel Grid.Row="2">
            <Border Background="{DynamicResource CardBackgroundBrush}"
                    CornerRadius="8"
                    BoxShadow="0 2 8 0 #0D000000"
                    ClipToBounds="True">
                <DataGrid ItemsSource="{Binding MusicFiles}"
                          AutoGenerateColumns="False"
                          IsReadOnly="True"
                          GridLinesVisibility="Horizontal"
                          BorderThickness="0"
                          Background="Transparent">
                    <DataGrid.Columns>
                        <DataGridTextColumn x:DataType="vm:MusicFileViewModel"
                                            Header="{loc:Localize Column_SongName}"
                                            Binding="{Binding Name}"
                                            Width="*" />
                        <DataGridTextColumn x:DataType="vm:MusicFileViewModel"
                                            Header="{loc:Localize Column_Artist}"
                                            Binding="{Binding Artist}"
                                            Width="200" />
                        <DataGridTextColumn x:DataType="vm:MusicFileViewModel"
                                            Header="{loc:Localize Column_Status}"
                                            Binding="{Binding StatusMessage}"
                                            Width="120" />
                    </DataGrid.Columns>
                </DataGrid>
            </Border>

            <!-- Empty State Overlay (Margin top to preserve DataGrid header) -->
            <Border Background="{DynamicResource CardBackgroundBrush}"
                    CornerRadius="0,0,8,8"
                    Margin="0,36,0,0"
                    IsVisible="{Binding HasNoFiles}"
                    IsHitTestVisible="False">
                <StackPanel VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            Spacing="16">
                    <Border Background="{DynamicResource ContentAreaBrush}"
                            CornerRadius="4"
                            Width="50" Height="40"
                            HorizontalAlignment="Center">
                        <PathIcon Data="{StaticResource ImageIcon}"
                                  Width="24" Height="24"
                                  Opacity="0.3" />
                    </Border>
                    <TextBlock Text="{Binding EmptyStateText}"
                               Opacity="0.4"
                               FontSize="13"
                               HorizontalAlignment="Center" />
                </StackPanel>
            </Border>
        </Panel>

        <!-- Card 3: Status Bar -->
        <Border Grid.Row="3"
                Background="{DynamicResource CardBackgroundBrush}"
                BoxShadow="0 2 8 0 #0D000000"
                Padding="24,12"
                Margin="0,16,0,0"
                CornerRadius="8">
            <Grid ColumnDefinitions="Auto,*,Auto,Auto">
                <StackPanel Grid.Column="0" Orientation="Horizontal" Spacing="4">
                    <TextBlock Text="{Binding CommonTotal}" FontSize="13" FontWeight="Medium" />
                    <TextBlock Text="{Binding TotalCount}" FontWeight="Bold" FontSize="13" />
                    <TextBlock Text="{Binding CommonFiles}" FontSize="13" Opacity="0.7" />
                </StackPanel>

                <Panel Grid.Column="1" />

                <StackPanel Grid.Column="2" Orientation="Horizontal" Spacing="6" Margin="0,0,24,0">
                    <Border Width="4" Height="16" CornerRadius="2"
                            Background="{DynamicResource SuccessColorBrush}"
                            VerticalAlignment="Center" />
                    <TextBlock Text="{Binding CommonSuccess}" FontSize="13" />
                    <TextBlock Text="{Binding CompletedCount}"
                               FontWeight="Bold" FontSize="13"
                               Foreground="{DynamicResource SuccessColorBrush}" />
                </StackPanel>

                <StackPanel Grid.Column="3" Orientation="Horizontal" Spacing="6">
                    <Border Width="4" Height="16" CornerRadius="2"
                            Background="{DynamicResource ErrorColorBrush}"
                            VerticalAlignment="Center" />
                    <TextBlock Text="{Binding CommonFailed}" FontSize="13" />
                    <TextBlock Text="{Binding FailedCount}"
                               FontWeight="Bold" FontSize="13"
                               Foreground="{DynamicResource ErrorColorBrush}" />
                </StackPanel>
            </Grid>
        </Border>
    </Grid>

</UserControl>
