import 'dart:io';
import 'package:flutter/material.dart';
import 'package:lucide_icons/lucide_icons.dart';
import '../../../models/sake_item.dart';
import '../../../theme/app_colors.dart';
import '../../../widgets/common/munyun_like_button.dart';

/// SakeDetailScreen の SliverAppBar を担当するウィジェット
/// 画像カルーセル（複数枚）/単一画像表示、FAB付き写真編集ボタン、スクリムを含む
class SakeDetailSliverAppBar extends StatelessWidget {
  final SakeItem sake;
  final int currentImageIndex;
  final VoidCallback onToggleFavorite;
  final VoidCallback onReanalyze;
  final VoidCallback onDelete;
  final ValueChanged<int> onPageChanged;
  final VoidCallback onPhotoEdit;

  const SakeDetailSliverAppBar({
    super.key,
    required this.sake,
    required this.currentImageIndex,
    required this.onToggleFavorite,
    required this.onReanalyze,
    required this.onDelete,
    required this.onPageChanged,
    required this.onPhotoEdit,
  });

  @override
  Widget build(BuildContext context) {
    final appColors = Theme.of(context).extension<AppColors>()!;

    return SliverAppBar(
      expandedHeight: 400.0,
      floating: false,
      pinned: true,
      backgroundColor: Theme.of(context).primaryColor,
      iconTheme: const IconThemeData(color: Colors.white),
      actions: [
        MunyunLikeButton(
          isLiked: sake.userData.isFavorite,
          onTap: onToggleFavorite,
        ),
        IconButton(
          icon: const Icon(LucideIcons.refreshCw),
          color: Colors.white,
          tooltip: 'AI再解析',
          onPressed: onReanalyze,
        ),
        IconButton(
          icon: const Icon(LucideIcons.trash2),
          color: Colors.white,
          tooltip: '削除',
          onPressed: onDelete,
        ),
      ],
      flexibleSpace: FlexibleSpaceBar(
        background: Stack(
          fit: StackFit.expand,
          children: [
            sake.displayData.imagePaths.length > 1
                ? Stack(
                    fit: StackFit.expand,
                    children: [
                      PageView.builder(
                        itemCount: sake.displayData.imagePaths.length,
                        onPageChanged: onPageChanged,
                        itemBuilder: (context, index) {
                          final imageWidget = Image.file(
                            File(sake.displayData.imagePaths[index]),
                            fit: BoxFit.cover,
                          );

                          // Apply Hero only to the first image for smooth transition from Grid/List
                          if (index == 0) {
                            return Hero(
                              tag: sake.id,
                              child: imageWidget,
                            );
                          }
                          return imageWidget;
                        },
                      ),
                      // Simple Indicator
                      Positioned(
                        bottom: 16,
                        right: 16,
                        child: Container(
                          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                          decoration: BoxDecoration(
                            color: Colors.black.withValues(alpha: 0.6),
                            borderRadius: BorderRadius.circular(16),
                          ),
                          child: Text(
                            '${currentImageIndex + 1} / ${sake.displayData.imagePaths.length}',
                            style: const TextStyle(
                                color: Colors.white, fontSize: 12, fontWeight: FontWeight.bold),
                          ),
                        ),
                      ),
                      // Photo Edit Button
                      Positioned(
                        bottom: 16,
                        left: 16,
                        child: FloatingActionButton.small(
                          heroTag: 'photo_edit',
                          backgroundColor: Colors.white,
                          onPressed: onPhotoEdit,
                          child: Icon(LucideIcons.image, color: appColors.iconDefault),
                        ),
                      ),
                    ],
                  )
                : Stack(
                    fit: StackFit.expand,
                    children: [
                      Hero(
                        tag: sake.id,
                        child: sake.displayData.imagePaths.isNotEmpty
                            ? Image.file(
                                File(sake.displayData.imagePaths.first),
                                fit: BoxFit.cover,
                              )
                            : Container(
                                color: appColors.surfaceSubtle,
                                child: Icon(LucideIcons.image, size: 80, color: appColors.iconSubtle),
                              ),
                      ),
                      // Photo Edit Button for single image
                      Positioned(
                        bottom: 16,
                        left: 16,
                        child: FloatingActionButton.small(
                          heroTag: 'photo_edit_single',
                          backgroundColor: Colors.white,
                          onPressed: onPhotoEdit,
                          child: Icon(LucideIcons.image, color: appColors.iconDefault),
                        ),
                      ),
                    ],
                  ),
            // Scrim for Header Icons Visibility
            Positioned(
              top: 0,
              left: 0,
              right: 0,
              height: 100,
              child: Container(
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      Colors.black.withValues(alpha: 0.7),
                      Colors.transparent,
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
