import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:provider/provider.dart';
import 'package:sendtrain/daos/media_items_dao.dart';
import 'package:sendtrain/database/database.dart' hide ActivityAction;
import 'package:sendtrain/widgets/generic/elements/generic_progress_indicator.dart';
import 'package:sendtrain/widgets/sessions/session_card_full.dart';
import 'package:sendtrain/widgets/sessions/session_card_small.dart';

class SessionCard extends StatefulWidget {
  final int type;
  final Session session;
  const SessionCard({super.key, this.type = 0, required this.session});

  @override
  State<SessionCard> createState() => _SessionCardState();
}

class _SessionCardState extends State<SessionCard> {
  @override
  Widget build(BuildContext context) {
    final int type = widget.type;
    final Session session = widget.session;

    initializeDateFormatting('en');

    return StreamBuilder<List<MediaItem>>(
        stream: MediaItemsDao(Provider.of<AppDatabase>(context))
            .watchSessionMediaItems(session.id),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            List<MediaItem> mediaItems = snapshot.data!;

            if (type == 0) {
              return SessionCardFull(session: session, mediaItems: mediaItems);
            } else {
              return SessionCardSmall(session: session, mediaItems: mediaItems);
            }
          } else {
            return GenericProgressIndicator();
          }
        });
  }
}
