import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sendtrain/daos/activities_dao.dart';
import 'package:sendtrain/database/database.dart';
import 'package:sendtrain/helpers/widget_helpers.dart';
import 'package:sendtrain/widgets/activities/activity_card.dart';
import 'package:sendtrain/widgets/generic/elements/add_card_generic.dart';
import 'package:sendtrain/widgets/generic/elements/generic_progress_indicator.dart';
import 'package:sendtrain/widgets/sessions/session_activities_editor.dart';

class SessionViewActivities extends StatefulWidget {
  const SessionViewActivities({super.key, required this.session});

  final Session session;

  @override
  State<SessionViewActivities> createState() => _SessionViewActivitiesState();
}

class _SessionViewActivitiesState extends State<SessionViewActivities> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<List<Activity>>(
        stream: ActivitiesDao(Provider.of<AppDatabase>(context))
            .watchSessionActivities(widget.session.id),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            final activities = snapshot.data!;
            if (activities.isNotEmpty) {
              return Expanded(
                  child: ListView.builder(
                // shrinkWrap: true,
                padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                itemCount: activities.length,
                itemBuilder: (BuildContext context, int index) {
                  return ActivityCard(
                      activity: activities[index], session: widget.session);
                },
              ));
            } else {
              return AddCardGeneric(
                  title: 'Add an Activity!',
                  description:
                      'Here you can associate one or more activities that you can follow along with during your session.',
                  action: () {
                    showEditorSheet(
                        context,
                        SessionActivitiesEditor(
                            session: widget.session, callback: () {}));
                  });
            }
          } else {
            return GenericProgressIndicator();
          }
        });
  }
}
