import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:mini_tias/providers/gallery_provider.dart';
import 'package:mini_tias/widgets/image_detail_dialog.dart';
import 'package:mini_tias/widgets/image_grid.dart';

/// 一覧画面．撮影済み画像のサムネイルグリッドを表示する．
class GalleryScreen extends StatefulWidget {
  const GalleryScreen({super.key});

  @override
  State<GalleryScreen> createState() => _GalleryScreenState();
}

class _GalleryScreenState extends State<GalleryScreen> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      context.read<GalleryProvider>().loadImages();
    });
  }

  @override
  Widget build(BuildContext context) {
    final galleryProvider = context.watch<GalleryProvider>();

    if (galleryProvider.isLoading) {
      return const Center(child: CircularProgressIndicator());
    }

    if (galleryProvider.images.isEmpty) {
      return const Center(child: Text('撮影した画像がありません'));
    }

    return ImageGrid(
      images: galleryProvider.images,
      onImageTap: (file) {
        showDialog(
          context: context,
          builder: (_) => ImageDetailDialog(
            file: file,
            onDelete: () {
              galleryProvider.deleteImage(file);
            },
          ),
        );
      },
    );
  }
}
