<?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.DoctorSignaturePage"
             xmlns:vm="clr-namespace:LSFE.MobApp.ViewModels.Coverage"
             xmlns:views="clr-namespace:LSFE.MobApp.Views.CoveragePlan"
             Title="Doctor Signature">

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

            <Style x:Key="ActionButtonStyle" 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="56" />
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>

    <!-- Main Grid without ScrollView - Responsive Layout -->
    <Grid RowDefinitions="Auto,Auto,*,Auto,Auto" 
          Padding="20" 
          RowSpacing="16">

        <!-- Row 0: Doctor Info Summary Card -->
        <Border Grid.Row="0"
                BackgroundColor="White" 
                Stroke="#E5E7EB" 
                StrokeThickness="1"
                StrokeShape="RoundRectangle 12"
                Padding="16">
            <VerticalStackLayout Spacing="8">
                <Label Text="{Binding DoctorName}" 
                       FontSize="18" 
                       FontAttributes="Bold" 
                       TextColor="#111827"
                       LineBreakMode="TailTruncation"/>

                <Label Text="{Binding DoctorSpecialization}" 
                       FontSize="14" 
                       TextColor="#6B7280"
                       LineBreakMode="TailTruncation"/>

                <BoxView HeightRequest="1" 
                         BackgroundColor="#E5E7EB" 
                         Margin="0,6"/>

                <Label Text="{Binding SelectedItemsSummary}" 
                       FontSize="13" 
                       TextColor="#374151"
                       LineBreakMode="WordWrap"/>
            </VerticalStackLayout>
        </Border>

        <!-- Row 1: Tab Selection -->
        <Grid Grid.Row="1" 
              ColumnDefinitions="*,*" 
              ColumnSpacing="10">
            <Button Text="✍️ Draw Signature"
                    Style="{StaticResource TabButtonStyle}"
                    BackgroundColor="{Binding DrawTabBackgroundColor}"
                    TextColor="{Binding DrawTabTextColor}"
                    Command="{Binding SelectDrawTabCommand}"/>

            <Button Grid.Column="1"
                    Text="📷 Capture Photo"
                    Style="{StaticResource TabButtonStyle}"
                    BackgroundColor="{Binding PhotoTabBackgroundColor}"
                    TextColor="{Binding PhotoTabTextColor}"
                    Command="{Binding SelectPhotoTabCommand}"/>
        </Grid>

        <!-- Row 2: Main Content Area (Expands to fill available space) -->
        <Grid Grid.Row="2">

            <!-- Signature Drawing Pad -->
            <Border BackgroundColor="White" 
                    Stroke="#E5E7EB" 
                    StrokeThickness="2"
                    StrokeShape="RoundRectangle 12"
                    Padding="0"
                    IsVisible="{Binding IsDrawTabSelected}">

                <Grid RowDefinitions="*,Auto">
                    <!-- Drawing Canvas - Takes all available space -->
                    <Border Grid.Row="0"
                            BackgroundColor="White"
                            Margin="0">
                        <Grid>
                            <!-- Signature Pad -->
                            <views:SignaturePadView x:Name="SignaturePad"
                                                    BackgroundColor="White"
                                                    HorizontalOptions="Fill"
                                                    VerticalOptions="Fill"/>

                            <!-- Watermark/Guide Text (only shows when empty) -->
                            <Label Text="✍️ Sign here"
                                   FontSize="24"
                                   TextColor="#E5E7EB"
                                   FontAttributes="Bold"
                                   HorizontalOptions="Center"
                                   VerticalOptions="Center"
                                   InputTransparent="True"
                                   x:Name="SignatureGuide"/>
                        </Grid>
                    </Border>

                    <!-- Canvas Actions -->
                    <!-- IsVisible="{Binding ShowClearButton}"-->
                    <Border Grid.Row="1"
                        BackgroundColor="#F9FAFB"
                        Padding="16"
                        StrokeThickness="0">        
                        <Grid ColumnDefinitions="*" ColumnSpacing="10">
                            <Button Text="🗑️ Clear"
                        BackgroundColor="#EF4444"
                        TextColor="White"
                        CornerRadius="8"
                        Padding="0,12"
                        FontSize="14"
                        FontAttributes="Bold"
                        Command="{Binding ClearSignatureCommand}"/>

                            <!--  <Button Grid.Column="1"
                        Text="💾 Save Signature"
                        BackgroundColor="#0EA5E9"
                        TextColor="White"
                        CornerRadius="8"
                        Padding="0,12"
                        FontSize="14"
                        FontAttributes="Bold"
                        Command="{Binding PreviewSignatureCommand}"/> -->
                        </Grid>
                    </Border>
                </Grid>
            </Border>

            <!-- Camera Capture Section -->
            <Border BackgroundColor="White" 
                    Stroke="#E5E7EB" 
                    StrokeThickness="2"
                    StrokeShape="RoundRectangle 12"
                    Padding="16"
                    IsVisible="{Binding IsPhotoTabSelected}">

                <Grid RowDefinitions="*,Auto">

                    <!-- Camera Preview - Takes all available space -->
                    <Border Grid.Row="0"
                            BackgroundColor="#F9FAFB"
                            StrokeShape="RoundRectangle 8"
                            StrokeThickness="1"
                            Stroke="#E5E7EB"
                            Padding="8">

                        <Grid>
                            <!-- Placeholder when no photo -->
                            <VerticalStackLayout Spacing="12"
                                                 HorizontalOptions="Center"
                                                 VerticalOptions="Center"
                                                 IsVisible="{Binding HasNoSignaturePhoto}">
                                <Label Text="📷" 
                                       FontSize="64" 
                                       HorizontalOptions="Center"/>
                                <Label Text="Take or select a photo" 
                                       FontSize="15" 
                                       TextColor="#6B7280" 
                                       HorizontalOptions="Center"
                                       HorizontalTextAlignment="Center"/>
                            </VerticalStackLayout>

                            <!-- Photo Preview -->
                            <Image Source="{Binding SignaturePhotoSource}"
                                   Aspect="AspectFit"
                                   IsVisible="{Binding HasSignaturePhoto}"
                                   HorizontalOptions="Fill"
                                   VerticalOptions="Fill"/>
                        </Grid>
                    </Border>

                    <!-- Camera Action Buttons -->
                    <VerticalStackLayout Grid.Row="1" 
                                         Spacing="10" 
                                         Margin="0,16,0,0">
                        <Grid ColumnDefinitions="*,*" ColumnSpacing="10">

                            <Button Grid.Column="1"
                               Text="📷 Take Photo" 
                        BackgroundColor="#10B981"
                        TextColor="White"
                        CornerRadius="8"
                        Padding="0,12"
                        FontSize="14"
                        FontAttributes="Bold"
                        Command="{Binding TakePhotoCommand}"/>

                            <!-- Remove Photo Button -->
                            <Button Text="🗑️ Remove Photo"
                                BackgroundColor="#EF4444"
                                TextColor="White"
                                CornerRadius="8"
                                Padding="0,12"
                                FontSize="14"
                                FontAttributes="Bold"
                                Command="{Binding RemovePhotoCommand}"/>
                        </Grid>
                    </VerticalStackLayout>
                </Grid>
            </Border>
        </Grid>

        <!-- Row 3: Signature Preview (after saved) -->
        <Border Grid.Row="3"
        BackgroundColor="White" 
        Stroke="#10B981" 
        StrokeThickness="2"
        StrokeShape="RoundRectangle 12"
        Padding="16"
        IsVisible="{Binding HasFinalSignature}">

            <Grid RowDefinitions="Auto,*,Auto,Auto" RowSpacing="8">
                <Label Grid.Row="0"
               Text="✅ Signature Captured" 
               FontSize="15" 
               FontAttributes="Bold" 
               TextColor="#10B981"
               HorizontalOptions="Center"/>

                <Border Grid.Row="1"
                BackgroundColor="#F9FAFB"
                HeightRequest="120"
                StrokeShape="RoundRectangle 8"
                StrokeThickness="1"
                Stroke="#E5E7EB">
                    <Image Source="{Binding FinalSignatureSource}"
                   Aspect="AspectFit"/>
                </Border>

                <Label Grid.Row="2"
               Text="{Binding SignatureTimestamp}" 
               FontSize="11" 
               TextColor="#6B7280"
               HorizontalOptions="Center"/>

                <!-- Add Clear/Edit button -->
                <Button Grid.Row="3"
                Text="✏️ Edit Signature"
                BackgroundColor="#F59E0B"
                TextColor="White"
                CornerRadius="8"
                Padding="0,10"
                FontSize="13"
                FontAttributes="Bold"
                Margin="0,8,0,0"
                Command="{Binding ClearSignatureCommand}"
                IsVisible="{Binding IsDrawTabSelected}"/>
            </Grid>
        </Border>

        <!-- Row 4: Action Buttons -->
        <Grid Grid.Row="4" 
              ColumnDefinitions="*,*" 
              ColumnSpacing="12"
              Margin="0,0,0,0">

            <Button Text="❌ Cancel" 
                    Style="{StaticResource ActionButtonStyle}"
                    BackgroundColor="#6B7280"
                    TextColor="White"
                    Command="{Binding CancelCommand}" />

            <Button Grid.Column="1"
                    Text="✅ Confirm &amp; Save"
                    Style="{StaticResource ActionButtonStyle}"
                    BackgroundColor="#10B981"
                    TextColor="White"
                    Command="{Binding ConfirmSignatureCommand}"
                    IsEnabled="{Binding CanConfirm}"/>
        </Grid>

        <!-- Loading Overlay (Always on top) -->
        <Grid Grid.RowSpan="5"
              BackgroundColor="#80000000"
              IsVisible="{Binding IsLoading}"
              ZIndex="999">

            <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="Saving..."
                           TextColor="#0EA5E9"
                           FontAttributes="Bold"
                           FontSize="16"
                           HorizontalOptions="Center" />
                </VerticalStackLayout>
            </Border>
        </Grid>
    </Grid>
</ContentPage>