import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sendtrain/daos/media_items_dao.dart';
import 'package:sendtrain/database/database.dart';
import 'package:sendtrain/widgets/media/media_card.dart';

class ActivityViewMedia extends StatelessWidget {
  const ActivityViewMedia({super.key, required this.activity});

  final Activity activity;

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<MediaItem>>(
      future: MediaItemsDao(Provider.of<AppDatabase>(context)).fromActivity(activity),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          List<MediaItem> mediaItems = snapshot.data!;
          List<MediaCard> mediaCards = [];

          for (int i = 0; i < mediaItems.length; i++) {
            mediaCards.add(MediaCard(media: mediaItems[i]));
          }

          return Column(
            children: [
              SizedBox(
                  width: double.infinity,
                  height: 100,
                  child: GridView.count(
                      padding: const EdgeInsets.fromLTRB(15, 0, 0, 0),
                      scrollDirection: Axis.horizontal,
                      crossAxisSpacing: 5,
                      mainAxisSpacing: 5,
                      crossAxisCount: 1,
                      children: mediaCards))
            ],
          );
        } else {
          return Container(
                alignment: Alignment.center,
                child: SizedBox(
                  height: 50.0,
                  width: 50.0,
                  child: CircularProgressIndicator(),
                ));
        }
      }
    );
  }
}