<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:Scry.ViewModels"
             xmlns:models="clr-namespace:Scry.Models"
             x:Class="Scry.Views.CollectionPage"
             x:DataType="vm:CollectionViewModel"
             Title="Collection">

    <Grid RowDefinitions="Auto,*">
        <!-- Header with stats -->
        <Border Grid.Row="0"
                BackgroundColor="{StaticResource Primary}"
                Padding="16">
            <HorizontalStackLayout Spacing="32" HorizontalOptions="Center">
                <VerticalStackLayout>
                    <Label Text="{Binding TotalCards}"
                           TextColor="White"
                           FontSize="28"
                           FontAttributes="Bold"
                           HorizontalTextAlignment="Center" />
                    <Label Text="Total Cards"
                           TextColor="#CCFFFFFF"
                           FontSize="12"
                           HorizontalTextAlignment="Center" />
                </VerticalStackLayout>
                <VerticalStackLayout>
                    <Label Text="{Binding UniqueCards}"
                           TextColor="White"
                           FontSize="28"
                           FontAttributes="Bold"
                           HorizontalTextAlignment="Center" />
                    <Label Text="Unique Cards"
                           TextColor="#CCFFFFFF"
                           FontSize="12"
                           HorizontalTextAlignment="Center" />
                </VerticalStackLayout>
            </HorizontalStackLayout>
        </Border>

        <!-- Empty state -->
        <VerticalStackLayout Grid.Row="1"
                             IsVisible="{Binding IsEmpty}"
                             VerticalOptions="Center"
                             HorizontalOptions="Center"
                             Spacing="16"
                             Padding="32">
            <Label Text="📦"
                   FontSize="64"
                   HorizontalTextAlignment="Center" />
            <Label Text="Your collection is empty"
                   FontSize="20"
                   FontAttributes="Bold"
                   HorizontalTextAlignment="Center" />
            <Label Text="Scan some cards to get started!"
                   TextColor="{StaticResource Gray500}"
                   HorizontalTextAlignment="Center" />
        </VerticalStackLayout>

        <!-- Card list -->
        <CollectionView Grid.Row="1"
                        ItemsSource="{Binding Entries}"
                        IsVisible="{Binding IsEmpty, Converter={StaticResource InverseBool}}"
                        SelectionMode="None">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="models:CollectionEntry">
                    <SwipeView>
                        <SwipeView.RightItems>
                            <SwipeItems>
                                <SwipeItem Text="Delete"
                                           BackgroundColor="Red"
                                           Command="{Binding Source={RelativeSource AncestorType={x:Type vm:CollectionViewModel}}, Path=RemoveEntryCommand}"
                                           CommandParameter="{Binding}" />
                            </SwipeItems>
                        </SwipeView.RightItems>

                        <Border Margin="8,4"
                                Padding="12"
                                BackgroundColor="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource Gray900}}">
                            <Border.StrokeShape>
                                <RoundRectangle CornerRadius="8" />
                            </Border.StrokeShape>
                            <Border.GestureRecognizers>
                                <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type vm:CollectionViewModel}}, Path=ViewCardCommand}"
                                                      CommandParameter="{Binding}" />
                            </Border.GestureRecognizers>

                            <Grid ColumnDefinitions="60,*,Auto" ColumnSpacing="12">
                                <!-- Card thumbnail -->
                                <Border Grid.Column="0"
                                        HeightRequest="84"
                                        WidthRequest="60"
                                        StrokeThickness="0">
                                    <Border.StrokeShape>
                                        <RoundRectangle CornerRadius="4" />
                                    </Border.StrokeShape>
                                    <Image Source="{Binding Card.ImageUrl}"
                                           Aspect="AspectFill" />
                                </Border>

                                <!-- Card info -->
                                <VerticalStackLayout Grid.Column="1"
                                                     VerticalOptions="Center"
                                                     Spacing="4">
                                    <Label Text="{Binding Card.Name}"
                                           FontSize="16"
                                           FontAttributes="Bold"
                                           LineBreakMode="TailTruncation" />
                                    <Label Text="{Binding Card.SetName}"
                                           FontSize="12"
                                           TextColor="{StaticResource Gray500}" />
                                    <HorizontalStackLayout Spacing="8">
                                        <Label Text="{Binding Card.Rarity}"
                                               FontSize="11"
                                               TextColor="{StaticResource Gray400}" />
                                        <Label Text="• Foil"
                                               FontSize="11"
                                               TextColor="{StaticResource Primary}"
                                               IsVisible="{Binding IsFoil}" />
                                    </HorizontalStackLayout>
                                </VerticalStackLayout>

                                <!-- Quantity controls -->
                                <HorizontalStackLayout Grid.Column="2"
                                                       Spacing="8"
                                                       VerticalOptions="Center">
                                    <Button Text="-"
                                            WidthRequest="36"
                                            HeightRequest="36"
                                            Padding="0"
                                            CornerRadius="18"
                                            Command="{Binding Source={RelativeSource AncestorType={x:Type vm:CollectionViewModel}}, Path=DecrementQuantityCommand}"
                                            CommandParameter="{Binding}" />
                                    <Label Text="{Binding Quantity}"
                                           FontSize="18"
                                           FontAttributes="Bold"
                                           WidthRequest="30"
                                           HorizontalTextAlignment="Center"
                                           VerticalOptions="Center" />
                                    <Button Text="+"
                                            WidthRequest="36"
                                            HeightRequest="36"
                                            Padding="0"
                                            CornerRadius="18"
                                            Command="{Binding Source={RelativeSource AncestorType={x:Type vm:CollectionViewModel}}, Path=IncrementQuantityCommand}"
                                            CommandParameter="{Binding}" />
                                </HorizontalStackLayout>
                            </Grid>
                        </Border>
                    </SwipeView>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </Grid>

</ContentPage>
