import 'package:flutter/material.dart';
import 'package:skillcourt/pages/play_setup/play_setup.dart';
import 'package:skillcourt/pages/play_setup/play_setup_item.dart';
import 'package:skillcourt/pages/play_setup/save_game/db_helper.dart';
import 'package:skillcourt/widgets/sad_statement.dart';

import '../../main.dart';

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

  @override
  State<SavedGames> createState() => _SavedGamesState();
}

class _SavedGamesState extends State<SavedGames> {
  Future<List<PlaySetUpItem>> getAllItems() async {
    return await getIt<DBHelper>().getAllPlaySetUps();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: getAllItems(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            // While waiting for data, display a loading screen
            return CircularProgressIndicator(); // Replace with your loading widget
          } else if (snapshot.hasError) {
            // Handle errors if needed
            return Text('Error: ${snapshot.error}');
          } else {
            List<PlaySetUpItem>? allItems = snapshot.data;
            if (allItems != null && allItems.isNotEmpty) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text('Your perfect game settings ready to play'),
                  ),
                  Expanded(
                    child: ListView.builder(
                      shrinkWrap: true,
                      physics: ClampingScrollPhysics(),
                      itemCount: allItems.length,
                      itemBuilder: (BuildContext context, int index) {
                        return allItems[index].gameSessionDisplay(
                            name: allItems[index].name,
                            onDelete: () async {
                              int rowsAffected = await getIt<DBHelper>()
                                  .deletePlaySetUp(allItems[index].id!);
                              if (rowsAffected > 0) {
                                print('PlaySetUp deleted successfully.');
                              } else {
                                print('No PlaySetUp found with the given ID.');
                              }
                              print("DELETED");
                              setState(() {});
                            },
                            onSelected: () {
                              PlaySetUp.playSetUpItem = allItems[index];
                              print("Selected");
                            });
                      },
                    ),
                  ),
                ],
              );
            } else {
              return SadStatement(sadMessage: 'Nothing saved yet.');
            }
          }
        });
  }
}
