import 'package:flutter/material.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/sessions/session_view.dart';
import 'package:collection/collection.dart';

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

  final Session session;
  final List<MediaItem> mediaItems;

  @override
  State<SessionCardSmall> createState() => _SessionCardSmallState();
}

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

  // @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(
        color: (session.status == SessionStatus.started)
            ? Theme.of(context).colorScheme.primaryContainer
            : Theme.of(context).colorScheme.surfaceContainerLow,
        child: InkWell(
            // overlayColor: MaterialStateColor(Colors.deepPurple as int),
            splashColor: Colors.deepPurple,
            borderRadius: const BorderRadius.all(Radius.elliptical(10, 10)),
            onLongPress: () {
              if (sessionImage != null) {
                showMediaDetailWidget(context, sessionImage);
              }
            },
            onTap: () =>
                showGenericDialog(SessionView(session: session), context),
            child: Container(
                decoration: BoxDecoration(
                  // color: const Color.fromARGB(47, 0, 0, 0),
                  borderRadius: BorderRadius.circular(10),
                  image: DecorationImage(
                      colorFilter: ColorFilter.mode(
                          Color.fromARGB(220, 41, 39, 39), BlendMode.hardLight),
                      image: findMediaByType(mediaItems, MediaType.location),
                      fit: BoxFit.cover),
                ),
                child: Align(
                    alignment: Alignment.center,
                    child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          ListTile(
                            title: Text(
                                maxLines: 3,
                                session.title.toTitleCase(),
                                textAlign: TextAlign.center),
                            subtitle: Text(
                                maxLines: 1,
                                dateFormat.format(session.date as DateTime),
                                textAlign: TextAlign.center),
                          ),
                        ])))));
  }
}
