import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:get_it/get_it.dart';
import 'package:skillcourt/entity/club.dart';
import 'package:skillcourt/pages/club_registration.dart';
import 'package:skillcourt/services/database.dart';
import 'package:skillcourt/widgets/labeled_icon_button.dart';

final getIt = GetIt.instance;

class ClubList extends StatefulWidget {
  static String routeName = 'club-list';

  const ClubList({Key? key}) : super(key: key);

  @override
  _ClubListState createState() => _ClubListState();
}

class _ClubListState extends State<ClubList> {
  DatabaseService database = getIt<DatabaseService>();

  late Club ownerClub;

  @override
  Widget build(BuildContext context) {
    final Icon icon = Icon(context.platformIcons.personAdd);

    final clubDao = database.clubDao;

    return PlatformScaffold(
      appBar: PlatformAppBar(
        title: const Text('Club List'),
        trailingActions: [
          IconButton(
              icon: icon,
              onPressed: () {
                Navigator.pushNamed(context, ClubRegistration.routeName,
                        arguments: ClubRegistrationArguments(-1))
                    .then((value) => setState(() {}));
              })
        ],
      ),
      body: FutureBuilder(
        future: clubDao.findAllClubs(),
        builder: (context, AsyncSnapshot<List<Club>> snapshot) {
          if (snapshot.hasData) {
            return ListView.separated(
                itemCount: snapshot.data!.length,
                padding: const EdgeInsets.all(8.0),
                itemBuilder: (context, int index) {
                  final Club club = snapshot.data!.elementAt(index);
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Expanded(
                          child: Container(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Padding(
                              padding:
                                  const EdgeInsets.only(left: 8.0, right: 8.0),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    club.name,
                                    style: const TextStyle(
                                        fontSize: 16,
                                        fontWeight: FontWeight.bold),
                                    maxLines: 2,
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ],
                              ),
                            ),
                            Padding(
                                padding: const EdgeInsets.only(
                                    left: 8.0, right: 8.0),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(
                                      '${club.ownerFirstName} ${club.ownerLastName}',
                                      style: const TextStyle(
                                        fontSize: 14,
                                      ),
                                      maxLines: 2,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                  ],
                                ))
                          ],
                        ),
                      )),
                      LabeledIconButton(
                          width: 60.0,
                          label: 'Edit',
                          iconData: context.platformIcons.edit,
                          materialColor: Colors.green,
                          cupertinoColor: CupertinoColors.systemGreen,
                          onPressed: () => Navigator.pushNamed(
                                  context, ClubRegistration.routeName,
                                  arguments:
                                      ClubRegistrationArguments(club.id!))
                              .then((value) => setState(() {}))),
                      LabeledIconButton(
                          width: 84.0,
                          label: 'Remove',
                          iconData: context.platformIcons.delete,
                          materialColor: Colors.red,
                          cupertinoColor: CupertinoColors.systemRed,
                          onPressed: () async {
                            await database.clubDao.deleteClub(club);
                            setState(() => snapshot.data!.remove(club));
                          })
                    ],
                  );
                },
                separatorBuilder: (BuildContext context, int index) =>
                    const Divider());
          } else {
            return const Center(child: CircularProgressIndicator());
          }
        },
      ),
    );
  }
}
