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/dao/player_dao.dart';
import 'package:skillcourt/entity/player.dart';
import 'package:skillcourt/pages/player_registration.dart';
import 'package:skillcourt/services/database.dart';
import 'package:skillcourt/widgets/labeled_icon_button.dart';

final getIt = GetIt.instance;

class PlayerList extends StatefulWidget {
  static const String routeName = 'player-list';

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

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

class _PlayerListState extends State<PlayerList> {
  final DatabaseService database = getIt<DatabaseService>();

  final _title = 'Registered Players';

  @override
  Widget build(BuildContext context) {
    final PlayerDao dao = database.playerDao;
    return FutureBuilder<List<Player>>(
        future: dao.findAllPlayers(),
        builder: (BuildContext context, AsyncSnapshot<List<Player>> snapshot) {
          final List<Widget> actions = [
            IconButton(
                icon: Icon(context.platformIcons.personAdd),
                onPressed: () {
                  Navigator.pushNamed(context, PlayerRegistration.routeName,
                          arguments: PlayerRegistrationArguments(-1))
                      .then((value) => setState(() {}));
                })
          ];

          return PlatformScaffold(
              appBar: PlatformAppBar(
                title: Text(_title),
                trailingActions: actions,
              ),
              body: (snapshot.hasData)
                  ? ListView.separated(
                      itemCount: snapshot.data!.length,
                      padding: const EdgeInsets.all(8.0),
                      itemBuilder: (BuildContext context, int index) {
                        final Player player = snapshot.data!.elementAt(index);

                        return Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Expanded(
                                flex: 2,
                                child: Padding(
                                    padding: const EdgeInsets.fromLTRB(
                                        5.0, 0.0, 0.0, 0.0),
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Text(player.fullName(),
                                            style: const TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 14)),
                                        Text(player.email)
                                      ],
                                    ))),
                            LabeledIconButton(
                                width: 60.0,
                                label: 'Edit',
                                iconData: context.platformIcons.edit,
                                materialColor: Colors.green,
                                cupertinoColor: CupertinoColors.systemGreen,
                                onPressed: () => Navigator.pushNamed(
                                        context, PlayerRegistration.routeName,
                                        arguments: PlayerRegistrationArguments(
                                            player.id!))
                                    .then((value) => setState(() {}))),
                            LabeledIconButton(
                                width: 84.0,
                                label: 'Remove',
                                iconData: context.platformIcons.delete,
                                materialColor: Colors.red,
                                cupertinoColor: CupertinoColors.systemRed,
                                onPressed: () async {
                                  await database.playerDao.deletePlayer(player);
                                  setState(() => {});
                                })
                          ],
                        );
                      },
                      separatorBuilder: (BuildContext context, int index) =>
                          const Divider())
                  : const Center(child: Text('No Players Registered')));
        });
  }
}
