import 'package:cabo_counter/data/game_session.dart';
import 'package:cabo_counter/utility/theme.dart' as theme;
import 'package:cabo_counter/views/round_view.dart';
import 'package:flutter/cupertino.dart';

class ActiveGameView extends StatefulWidget {
  final GameSession gameSession;
  const ActiveGameView({super.key, required this.gameSession});

  @override
  // ignore: library_private_types_in_public_api
  _ActiveGameViewState createState() => _ActiveGameViewState();
}

class _ActiveGameViewState extends State<ActiveGameView> {
  @override
  Widget build(BuildContext context) {
    List<int> sortedPlayerIndices = _getSortedPlayerIndices();
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text(widget.gameSession.gameTitle),
      ),
      child: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
              child: Text(
                'Spieler:innen',
                style: theme.createGameTitle,
              ),
            ),
            ListView.builder(
              shrinkWrap: true,
              itemCount: widget.gameSession.players.length,
              itemBuilder: (BuildContext context, int index) {
                int playerIndex = sortedPlayerIndices[index];
                return CupertinoListTile(
                  title: Row(
                    children: [
                      _getPlacementPrefix(index),
                      SizedBox(width: 5),
                      Text(
                        widget.gameSession.players[playerIndex],
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                  trailing: Row(
                    children: [
                      SizedBox(width: 5),
                      Text('${widget.gameSession.playerScores[playerIndex][0]} '
                          'Punkte')
                    ],
                  ),
                );
              },
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
              child: Text(
                'Runden',
                style: theme.createGameTitle,
              ),
            ),
            ListView.builder(
              shrinkWrap: true,
              itemCount: widget.gameSession.round,
              itemBuilder: (BuildContext context, int index) {
                return Padding(
                    padding: EdgeInsets.all(1),
                    child: CupertinoListTile(
                      title: Text(
                        'Runde ${index + 1}',
                      ),
                      trailing: index + 1 != widget.gameSession.round ||
                              widget.gameSession.finished == true
                          ? (Text('\u{2705}', style: TextStyle(fontSize: 22)))
                          : Text('\u{23F3}', style: TextStyle(fontSize: 22)),
                      onTap: () async {
                        // ignore: unused_local_variable
                        final val = await Navigator.push(
                          context,
                          CupertinoPageRoute(
                            fullscreenDialog: true,
                            builder: (context) => RoundView(
                                gameSession: widget.gameSession,
                                roundNumber: index + 1),
                          ),
                        );
                        setState(() {});
                      },
                    ));
              },
            ),
          ],
        ),
      ),
    );
  }

  /// Returns a list of player indices sorted by their scores in
  /// ascending order.
  List<int> _getSortedPlayerIndices() {
    List<int> playerIndices =
        List<int>.generate(widget.gameSession.players.length, (index) => index);
    // Sort the indices based on the summed points
    playerIndices.sort((a, b) {
      int scoreA = widget.gameSession.playerScores[a][0];
      int scoreB = widget.gameSession.playerScores[b][0];
      return scoreA.compareTo(scoreB);
    });
    return playerIndices;
  }

  /// Returns a widget that displays the placement prefix based on the index.
  /// First three places are represented by medals, and the rest are numbered.
  /// [index] is the index of the player in the descending sorted list.
  Widget _getPlacementPrefix(int index) {
    switch (index) {
      case 0:
        return Text(
          '\u{1F947}',
          style: TextStyle(fontSize: 22),
        );
      case 1:
        return Text(
          '\u{1F948}',
          style: TextStyle(fontSize: 22),
        );
      case 2:
        return Text(
          '\u{1F949}',
          style: TextStyle(fontSize: 22),
        );
      default:
        return Text(
          ' ${index + 1}.',
          style: TextStyle(fontWeight: FontWeight.bold),
        );
    }
  }
}
