<?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:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:vm="clr-namespace:Scry.ViewModels"
             x:Class="Scry.Views.ScanPage"
             x:DataType="vm:ScanViewModel"
             Title="Scan Card">

    <Grid RowDefinitions="*,Auto,Auto">
        <!-- Camera Preview -->
        <toolkit:CameraView x:Name="cameraView"
                            Grid.Row="0"
                            CameraFlashMode="Off"
                            HorizontalOptions="Fill"
                            VerticalOptions="Fill" />

        <!-- Scan overlay/guide -->
        <Border Grid.Row="0"
                Stroke="{StaticResource Primary}"
                StrokeThickness="3"
                StrokeDashArray="5,5"
                BackgroundColor="Transparent"
                WidthRequest="280"
                HeightRequest="400"
                HorizontalOptions="Center"
                VerticalOptions="Center">
            <Border.StrokeShape>
                <RoundRectangle CornerRadius="12" />
            </Border.StrokeShape>
        </Border>

        <!-- Result overlay when card is recognized -->
        <Border Grid.Row="0"
                BackgroundColor="#CC000000"
                IsVisible="{Binding HasResult}">
            <VerticalStackLayout Spacing="16"
                                 VerticalOptions="Center"
                                 HorizontalOptions="Center"
                                 Padding="24">
                <!-- Card image -->
                <Border StrokeThickness="0"
                        HeightRequest="300"
                        WidthRequest="215">
                    <Border.StrokeShape>
                        <RoundRectangle CornerRadius="12" />
                    </Border.StrokeShape>
                    <Image Source="{Binding LastScannedCard.ImageUrl}"
                           Aspect="AspectFill" />
                </Border>

                <!-- Card name and set -->
                <Label Text="{Binding LastScannedCard.Name}"
                       TextColor="White"
                       FontSize="24"
                       FontAttributes="Bold"
                       HorizontalTextAlignment="Center" />

                <Label TextColor="#AAAAAA"
                       FontSize="14"
                       HorizontalTextAlignment="Center">
                    <Label.Text>
                        <MultiBinding StringFormat="{}{0} • {1}">
                            <Binding Path="LastScannedCard.SetName" />
                            <Binding Path="LastConfidence" StringFormat="{}{0:P0} match" />
                        </MultiBinding>
                    </Label.Text>
                </Label>

                <!-- Foil toggle -->
                <HorizontalStackLayout Spacing="12" HorizontalOptions="Center">
                    <Label Text="Foil"
                           TextColor="White"
                           VerticalOptions="Center" />
                    <Switch IsToggled="{Binding IsFoil}"
                            OnColor="{StaticResource Primary}" />
                </HorizontalStackLayout>

                <!-- Action buttons -->
                <HorizontalStackLayout Spacing="16" HorizontalOptions="Center">
                    <Button Text="Cancel"
                            Style="{StaticResource OutlineButton}"
                            Command="{Binding CancelResultCommand}" />
                    <Button Text="Add to Collection"
                            Command="{Binding AddToCollectionCommand}" />
                </HorizontalStackLayout>
            </VerticalStackLayout>
        </Border>

        <!-- Status message -->
        <Border Grid.Row="1"
                BackgroundColor="{StaticResource Primary}"
                Padding="16,12"
                Margin="16,8">
            <Border.StrokeShape>
                <RoundRectangle CornerRadius="8" />
            </Border.StrokeShape>
            <Label Text="{Binding StatusMessage}"
                   TextColor="White"
                   FontSize="16"
                   HorizontalTextAlignment="Center" />
        </Border>

        <!-- Scan button -->
        <Button Grid.Row="2"
                Text="{Binding IsProcessing, Converter={StaticResource BoolToScanText}}"
                Command="{Binding CaptureAndRecognizeCommand}"
                CommandParameter="{x:Reference cameraView}"
                IsEnabled="{Binding IsProcessing, Converter={StaticResource InverseBool}}"
                Margin="16,0,16,24"
                HeightRequest="56"
                FontSize="18"
                FontAttributes="Bold" />
    </Grid>

</ContentPage>
