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 '../entity/player.dart';
import '../services/database.dart';
import '../widgets/labeled_icon_button.dart';

final getIt = GetIt.instance;

class Statistics extends StatefulWidget {
  static const routeName = 'statistics';

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

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

class _StatisticsState extends State<Statistics> {
  DatabaseService database = getIt<DatabaseService>();

  @override
  Widget build(BuildContext context) {
    final playerDao = database.playerDao;
    return PlatformScaffold(
      appBar: PlatformAppBar(title: const Text('Statistics')),
      body: FutureBuilder(
          future: playerDao.findAllPlayers(),
          builder: (context, AsyncSnapshot<List<Player>> snapshot) {
            if (snapshot.hasData) {
              return ListView.separated(
                  itemCount: snapshot.data!.length,
                  padding: const EdgeInsets.all(8.0),
                  itemBuilder: (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: 20)),
                                    ],
                                  ))),
                          LabeledIconButton(
                            width: 80.0,
                            label: 'Stats',
                            iconData: Icons.insert_chart,
                            materialColor: Colors.green,
                            cupertinoColor: CupertinoColors.systemGreen,
                            onPressed: () {
                              final String hits = player.hitCount.toString();
                              final String miss = player.missCount.toString();
                              final String hitPercent =
                                  player.hitPercentage.toString();
                              final String missPercent =
                                  player.missPercentage.toString();
                              final String totalShots =
                                  player.totalCount.toString();
                              final String totalPoints =
                                  player.totalPoints.toString();
                              showPlatformDialog(
                                  context: context,
                                  builder: (_) => PlatformAlertDialog(
                                        title: Text(
                                            "${player.fullName()}'s Statistics"),
                                        content: Text(
                                            'Total Shots: $totalShots\n'
                                            'Hits: $hits\n'
                                            'Hit Percentage: $hitPercent%\n'
                                            'Misses: $miss\n'
                                            'Miss Percentage: $missPercent%\n'
                                            'Total Points: $totalPoints\n'),
                                      ));
                            },
                          ),
                        ]);
                  },
                  separatorBuilder: (BuildContext context, int index) =>
                      const Divider());
            } else {
              return Center(child: PlatformCircularProgressIndicator());
            }
          }),
    );
  }
}
