// import 'package:drift/drift.dart' hide Column;
import 'package:drift/drift.dart' hide Column;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sendtrain/daos/sessions_dao.dart';
import 'package:sendtrain/database/database.dart';
import 'package:sendtrain/widgets/generic/elements/generic_progress_indicator.dart';
import 'package:sendtrain/widgets/sessions/session_card.dart';
import 'package:collection/collection.dart';

class SessionsScreen extends StatefulWidget {
  const SessionsScreen({super.key});

  @override
  State<SessionsScreen> createState() => _SessionsScreenState();
}

class _SessionsScreenState extends State<SessionsScreen> {
  Widget getSessionCard(session) {
    if (session != null) {
      return SessionCard(session: session);
    } else {
      return Padding(
          padding: EdgeInsets.all(15),
          child: Icon(Icons.do_not_disturb_alt_outlined));
    }
  }

  @override
  Widget build(BuildContext context) {
    SessionsDao dao = SessionsDao(Provider.of<AppDatabase>(context));

    return StreamBuilder<List<Session>>(
        stream: dao.watch(),
        builder: (context, snapshot) {
          if (snapshot.hasData && snapshot.data!.isNotEmpty) {
            final sessions = snapshot.data!;
            final pending = sessions.where((session) =>
                session.status == SessionStatus.completed ||
                session.status == SessionStatus.missed);
            final upcoming = sessions.firstWhereOrNull(
                (session) => session.status == SessionStatus.pending);
            final current = sessions.firstWhereOrNull(
                (session) => session.status == SessionStatus.started);

            if (current == null && upcoming != null) {
              dao.createOrUpdate(SessionsCompanion(
                id: Value(upcoming.id),
                title: Value(upcoming.title),
                content: Value(upcoming.content),
                status: Value(SessionStatus.started),
                address: Value(upcoming.address),
                date: Value(upcoming.date)
              ));
            }

            List<Widget> previousSessions = List.generate(pending.length,
                (i) => SessionCard(type: 1, session: pending.elementAt(i)));

            Widget upcomingSession = getSessionCard(upcoming);
            Widget currentSession = getSessionCard(current);

            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                const Padding(
                    padding: EdgeInsets.fromLTRB(15, 5, 0, 0),
                    child: Text(
                        style: TextStyle(
                            fontSize: 25, fontWeight: FontWeight.bold),
                        'Current:')),
                currentSession,
                const Padding(
                    padding: EdgeInsets.fromLTRB(15, 30, 0, 0),
                    child: Text(
                        style: TextStyle(
                            fontSize: 25, fontWeight: FontWeight.bold),
                        'Upcoming:')),
                upcomingSession,
                const Padding(
                    padding: EdgeInsets.fromLTRB(15, 30, 0, 0),
                    child: Text(
                        style: TextStyle(
                            fontSize: 25, fontWeight: FontWeight.bold),
                        'Previous:')),
                SizedBox(
                    width: double.infinity,
                    height: 160,
                    child: GridView.count(
                        padding: const EdgeInsets.fromLTRB(15, 10, 0, 0),
                        scrollDirection: Axis.horizontal,
                        crossAxisSpacing: 5,
                        mainAxisSpacing: 5,
                        crossAxisCount: 1,
                        children: previousSessions))
              ],
            );
          } else {
            return GenericProgressIndicator();
          }
        });
  }
}
