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/extensions/string_extensions.dart';
import 'package:sendtrain/helpers/date_time_helpers.dart';
import 'package:sendtrain/helpers/media_helpers.dart';
import 'package:sendtrain/helpers/widget_helpers.dart';
import 'package:sendtrain/widgets/builders/dialogs.dart';
import 'package:sendtrain/widgets/generic/elements/card_content.dart';
import 'package:sendtrain/widgets/generic/elements/card_image.dart';
import 'package:sendtrain/widgets/sessions/session_view.dart';
import 'package:collection/collection.dart';

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

  final Session session;
  final List<MediaItem> mediaItems;

  @override
  State<SessionCardFull> createState() => _SessionCardFullState();
}

class _SessionCardFullState extends State<SessionCardFull> {
  // late final List<MediaItem> mediaItems;
  // late final MediaItem? sessionImage;
  // late final Session session;

  String sessionTitle(Session session) {
    String title = session.title.toTitleCase();

    if (session.address != null) title = "$title @ ${session.address}";

    return title;
  }

  // @override
  // initState() {
  //   super.initState();
  //   session = widget.session;
  //   mediaItems = widget.mediaItems;
  //   sessionImage = mediaItems
  //       .firstWhereOrNull((mediaItem) => mediaItem.type == MediaType.location);
  // }

  @override
  Widget build(BuildContext context) {
    final Session session = widget.session;
    final List<MediaItem> mediaItems = widget.mediaItems;
    final MediaItem? sessionImage = mediaItems
        .firstWhereOrNull((mediaItem) => mediaItem.type == MediaType.location);

    return Card.outlined(
      color: (session.status == SessionStatus.started)
          ? Theme.of(context).colorScheme.primaryContainer
          : Theme.of(context).colorScheme.surfaceContainerLow,
      margin: const EdgeInsets.fromLTRB(15, 15, 15, 0),
      clipBehavior: Clip.hardEdge,
      child: InkWell(
          splashColor: Colors.deepPurple,
          onLongPress: () => showMediaDetailWidget(context, sessionImage!),
          onTap: () =>
              showGenericDialog(SessionView(session: session), context),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              ListTile(
                contentPadding: EdgeInsets.only(left: 8),
                leading: CardImage(
                    image: findMediaByType(mediaItems, MediaType.location),
                    padding: EdgeInsets.only(left: 5, top: 5)),
                title: Text(maxLines: 1, sessionTitle(session)),
                subtitle: Text(
                    maxLines: 1, dateFormat.format(session.date as DateTime)),
                trailing: IconButton(
                  padding: EdgeInsets.all(0),
                  alignment: Alignment.topCenter,
                  icon: Icon(Icons.close_rounded),
                  onPressed: () {
                    showRemovalDialog(
                        'Session Removal',
                        'Would you like to permanently remove this session?',
                        context, () {
                      SessionsDao(
                              Provider.of<AppDatabase>(context, listen: false))
                          .remove(session);
                    }).then((result) {
                      setState(() {});
                    });
                  },
                ),
              ),
              CardContent(content: session.content)
            ],
          )),
    );
  }
}
