<?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"
             x:Class="LSFE.MobApp.Pages.CoveragePlan.MarketProductPage"
             xmlns:Local="clr-namespace:LSFE.MobApp.Converters"
             xmlns:vm="clr-namespace:LSFE.MobApp.ViewModels.Coverage"
             Title="Detailing Aid and Sampling Page">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style x:Key="StatusBadgeStyle" TargetType="Border">
                <Setter Property="Padding" Value="12,6" />
                <Setter Property="StrokeShape" Value="RoundRectangle 12" />
                <Setter Property="StrokeThickness" Value="0" />
            </Style>

            <Style x:Key="CardStyle" TargetType="Border">
                <Setter Property="BackgroundColor" Value="White" />
                <Setter Property="Stroke" Value="#E5E7EB" />
                <Setter Property="StrokeThickness" Value="1" />
                <Setter Property="StrokeShape" Value="RoundRectangle 12" />
                <Setter Property="Padding" Value="20" />
                <Setter Property="Margin" Value="0,0,0,16" />
            </Style>

            <Style x:Key="MaterialCardStyle" TargetType="Border">
                <Setter Property="BackgroundColor" Value="#F9FAFB" />
                <Setter Property="Stroke" Value="#E5E7EB" />
                <Setter Property="StrokeThickness" Value="1" />
                <Setter Property="StrokeShape" Value="RoundRectangle 8" />
                <Setter Property="Padding" Value="16" />
                <Setter Property="Margin" Value="0,0,0,12" />
            </Style>

            <Style x:Key="CommonButtonStyle" TargetType="Button">
                <Setter Property="CornerRadius" Value="12" />
                <Setter Property="Padding" Value="0,16" />
                <Setter Property="FontSize" Value="16" />
                <Setter Property="FontAttributes" Value="Bold" />
                <Setter Property="HeightRequest" Value="60" />
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>

    <Grid>
        <!-- Main Scrollable Content -->
        <ScrollView>
            <VerticalStackLayout Padding="20" Spacing="20">

                <!-- Header Card with Avatar -->
                <Border BackgroundColor="White" 
                    Stroke="#E5E7EB" 
                    StrokeThickness="1"
                    StrokeShape="RoundRectangle 16"
                    Padding="24">
                    <Grid RowDefinitions="Auto,Auto" ColumnDefinitions="Auto,*,Auto">
                        <!-- Large Avatar -->
                        <Border Grid.Row="0" 
                            Grid.RowSpan="2"
                            Grid.Column="0"
                            BackgroundColor="#E0F2FE" 
                            WidthRequest="80" 
                            HeightRequest="80"
                            StrokeShape="RoundRectangle 40"
                            StrokeThickness="0"
                            VerticalOptions="Center"
                            Margin="0,0,20,0">
                            <Label Text="👨‍⚕️" 
                               FontSize="48" 
                               HorizontalOptions="Center" 
                               VerticalOptions="Center"/>
                        </Border>

                        <!-- Name and Specialization -->
                        <VerticalStackLayout Grid.Row="0" 
                                         Grid.Column="1" 
                                         Spacing="6"
                                         VerticalOptions="Center">
                            <Label Text="{Binding FullName}" 
                               FontSize="24" 
                               FontAttributes="Bold" 
                               TextColor="#111827"/>
                            <Label Text="{Binding Specialization}" 
                               FontSize="16" 
                               TextColor="#6B7280"/>
                        </VerticalStackLayout>

                        <!-- Status Badge -->
                        <Border Grid.Row="0" 
                            Grid.Column="2" 
                            BackgroundColor="{Binding StatusBackgroundColor}"
                            Padding="16,8"
                            StrokeShape="RoundRectangle 12"
                            StrokeThickness="0"
                            VerticalOptions="Start">
                            <Label Text="{Binding StatusText}" 
                               TextColor="{Binding StatusTextColor}" 
                               FontSize="13" 
                               FontAttributes="Bold"/>
                        </Border>
                    </Grid>
                </Border>

                <!--Marketing Materials-->
                <Border BackgroundColor="White" 
                    Stroke="#E5E7EB" 
                    StrokeThickness="1"
                    StrokeShape="RoundRectangle 12"
                    Padding="20">
                    <VerticalStackLayout Spacing="16">
                        <!-- Header with Buttons -->
                        <Grid ColumnDefinitions="*,Auto,Auto" ColumnSpacing="8">
                            <Label Text="Marketing Materials" 
                               FontSize="18" 
                               FontAttributes="Bold" 
                               TextColor="#111827"
                               VerticalOptions="Center"/>

                            <Button Grid.Column="1"
                                Text="✅ Check All"
                                BackgroundColor="#10B981"
                                TextColor="White"
                                CornerRadius="8"
                                Padding="12,8"
                                FontSize="14"
                                Command="{Binding CheckAllMaterialsCommand}"/>

                            <Button Grid.Column="2"
                                Text="⬇️ Download All"
                                BackgroundColor="#0EA5E9"
                                TextColor="White"
                                CornerRadius="8"
                                Padding="12,8"
                                FontSize="14"
                                Command="{Binding DownloadAllCommand}"/>
                        </Grid>

                        <!-- Display the list of materials -->
                        <CollectionView ItemsSource="{Binding Materials}">
                            <CollectionView.EmptyView>
                                <VerticalStackLayout Padding="40" Spacing="12">
                                    <Label Text="📭" 
                                       FontSize="48" 
                                       HorizontalOptions="Center"/>
                                    <Label Text="No marketing materials available" 
                                       FontSize="16" 
                                       TextColor="#6B7280" 
                                       HorizontalOptions="Center"/>
                                </VerticalStackLayout>
                            </CollectionView.EmptyView>

                            <CollectionView.ItemTemplate>
                                <DataTemplate>
                                    <Border Style="{StaticResource MaterialCardStyle}">
                                        <Grid RowDefinitions="Auto,Auto" 
                                          ColumnDefinitions="Auto,Auto,*,Auto"
                                          RowSpacing="8"
                                          ColumnSpacing="12">

                                            <!-- Checkbox -->
                                            <CheckBox Grid.Row="0"
                                                  Grid.Column="0"
                                                  IsChecked="{Binding IsChecked}"
                                                  Color="#0EA5E9"
                                                  VerticalOptions="Start"/>

                                            <!-- File Icon -->
                                            <Label Grid.Row="0"
                                               Grid.Column="1"
                                               Text="{Binding IconEmoji}"
                                               FontSize="32"
                                               VerticalOptions="Start"/>

                                            <!-- Product Name and Extension -->
                                            <VerticalStackLayout Grid.Row="0"
                                                             Grid.Column="2"
                                                             Spacing="4"
                                                             VerticalOptions="Center">
                                                <Label Text="{Binding ProductName}"
                                                   FontSize="16"
                                                   FontAttributes="Bold"
                                                   TextColor="#111827"/>
                                                <Label Text="{Binding FileExtension}"
                                                   FontSize="13"
                                                   TextColor="#6B7280"/>
                                            </VerticalStackLayout>

                                            <!-- Download Status -->
                                            <Label Grid.Row="0"
                                               Grid.Column="3"
                                               Text="{Binding DownloadButtonText}"
                                               FontSize="13"
                                               TextColor="#059669"
                                               IsVisible="{Binding IsDownloaded}"
                                               VerticalOptions="Center"/>

                                            <!-- Action Buttons -->
                                            <Grid Grid.Row="1"
                                              Grid.Column="0"
                                              Grid.ColumnSpan="4"
                                              ColumnDefinitions="*,*"
                                              ColumnSpacing="8">

                                                <Button Text="📥 Download"
                                                    BackgroundColor="#0EA5E9"
                                                    TextColor="White"
                                                    CornerRadius="6"
                                                    Padding="0,10"
                                                    FontSize="14"
                                                    Command="{Binding Source={RelativeSource AncestorType={x:Type vm:MarketProductVM}}, Path=DownloadMaterialCommand}"
                                                    CommandParameter="{Binding .}"
                                                    IsEnabled="{Binding CanDownload}"
                                                    IsVisible="{Binding IsDownloaded, Converter={StaticResource InverseBoolConverter}}"/>

                                                <Button Grid.Column="1"
                                                    Text="📂 Open"
                                                    BackgroundColor="#10B981"
                                                    TextColor="White"
                                                    CornerRadius="6"
                                                    Padding="0,10"
                                                    FontSize="14"
                                                    Command="{Binding Source={RelativeSource AncestorType={x:Type vm:MarketProductVM}}, Path=OpenMaterialCommand}"
                                                    CommandParameter="{Binding .}"/>
                                            </Grid>

                                            <!-- Loading Indicator -->
                                            <ActivityIndicator Grid.Row="0"
                                                           Grid.Column="3"
                                                           IsRunning="{Binding IsDownloading}"
                                                           IsVisible="{Binding IsDownloading}"
                                                           Color="#0EA5E9"/>
                                        </Grid>
                                    </Border>
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>

                    </VerticalStackLayout>
                </Border>

                <!--Sampling items-->
                <Border BackgroundColor="White" 
                Stroke="#E5E7EB" 
                StrokeThickness="1"
                StrokeShape="RoundRectangle 12"
                Padding="20">

                    <VerticalStackLayout Spacing="16">
                        <!-- Header for sampling  -->
                        <Grid ColumnDefinitions="*,Auto">
                            <Label Text="Sampling Items" 
                   FontSize="18" 
                   FontAttributes="Bold" 
                   TextColor="#111827"
                   VerticalOptions="Center"/>

                            <Button Grid.Column="1"
                    Text="✅ Check All"
                    BackgroundColor="#10B981"
                    TextColor="White"
                    CornerRadius="8"
                    Padding="12,8"
                    FontSize="14"
                    Command="{Binding CheckAllSamplingCommand}"/>
                        </Grid>

                        <!-- Display the list of samplings -->
                        <CollectionView ItemsSource="{Binding Samplings}">
                            <CollectionView.EmptyView>
                                <VerticalStackLayout Padding="40" Spacing="12">
                                    <Label Text="📭" 
                           FontSize="48" 
                           HorizontalOptions="Center"/>
                                    <Label Text="No sampling available" 
                           FontSize="16" 
                           TextColor="#6B7280" 
                           HorizontalOptions="Center"/>
                                </VerticalStackLayout>
                            </CollectionView.EmptyView>

                            <CollectionView.ItemTemplate>
                                <DataTemplate>
                                    <Border Style="{StaticResource MaterialCardStyle}">
                                        <Grid ColumnDefinitions="Auto,Auto,*,Auto,Auto"
                                              ColumnSpacing="12">

                                            <!-- Checkbox -->
                                            <CheckBox Grid.Column="0"
                                                      IsChecked="{Binding IsChecked}"
                                                      Color="#0EA5E9"
                                                      VerticalOptions="Center"/>

                                            <!-- Icon -->
                                            <Label Grid.Column="1"
                                                   Text="{Binding IconEmoji}"
                                                   FontSize="32"
                                                   VerticalOptions="Center"/>

                                            <!-- Description -->
                                            <Label Grid.Column="2"
                                                   Text="{Binding Description}"
                                                   FontSize="16"
                                                   FontAttributes="Bold"
                                                   TextColor="#111827"
                                                   VerticalOptions="Center"/>

                                            <!-- Qty Label -->
                                            <Label Grid.Column="3"
                                                   Text="Qty:"
                                                   FontSize="14"
                                                   TextColor="#6B7280"
                                                   VerticalOptions="Center"
                                                   Margin="0,0,4,0"/>

                                            <!-- Qty Value -->
                                            <Border Grid.Column="4"
                                                    BackgroundColor="#E0F2FE"
                                                    Padding="8,4"
                                                    StrokeShape="RoundRectangle 6"
                                                    StrokeThickness="0">
                                                <Label Text="{Binding Qty}"
                                                       FontSize="14"
                                                       FontAttributes="Bold"
                                                       TextColor="#0EA5E9"
                                                       VerticalOptions="Center"/>
                                            </Border>
                                        </Grid>
                                    </Border>
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>

                    </VerticalStackLayout>
                </Border>

                <!-- Action Buttons -->
                <Grid ColumnDefinitions="*,*" 
                  ColumnSpacing="10"
                  Margin="0,10,0,0">

                    <Button Text="❌ Close" 
                        Style="{StaticResource CommonButtonStyle}"
                        BackgroundColor="#0EA5E9"
                        TextColor="White"
                        Command="{Binding CloseCommand}" />

                    <Button Grid.Column="1"
                        Text="✍️ Signature"
                        Style="{StaticResource CommonButtonStyle}"
                        BackgroundColor="Green"
                        TextColor="White"
                        Command="{Binding DoctorSignInCommand}" />
                </Grid>

            </VerticalStackLayout>
        </ScrollView>
        <!-- Overlay always on top -->
        <Grid 
            BackgroundColor="#80000000"
            IsVisible="{Binding IsLoading}"
            ZIndex="999"
            HorizontalOptions="Fill"
            VerticalOptions="Fill">

            <Border
                StrokeShape="RoundRectangle 16"
                BackgroundColor="White"
                Padding="32"
                WidthRequest="200"
                HeightRequest="150"
                HorizontalOptions="Center"
                VerticalOptions="Center">

                <VerticalStackLayout Spacing="16"
                    HorizontalOptions="Center"
                    VerticalOptions="Center">
                    <ActivityIndicator 
                        IsRunning="True"
                        Color="#0EA5E9"
                        WidthRequest="40"
                        HeightRequest="40" />
                    <Label 
                        Text="Processing..."
                        TextColor="#0EA5E9"
                        FontAttributes="Bold"
                        FontSize="16"
                        HorizontalOptions="Center" />
                </VerticalStackLayout>
            </Border>
        </Grid>
    </Grid>
</ContentPage>