import 'package:flutter/material.dart';
import 'package:werwolf/screens/flippingcards.dart';

import '../models/game.dart';
import '../models/role.dart';

// Main widget for game settings
class GameSettings extends StatefulWidget {
  final List<String> playernames;
  const GameSettings({required this.playernames, super.key});

  @override
  State<GameSettings> createState() => _GameSettingsState();
}

class _GameSettingsState extends State<GameSettings> {
  late Game game;

  @override
  void initState() {
    game = Game(playernames: widget.playernames); // Initialize the game with player names
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Werwolf",
          style: Theme.of(context).textTheme.titleLarge, // Use large title style from theme
        ),
        centerTitle: true,
      ),
      body: Padding(
        padding: const EdgeInsets.fromLTRB(30.0, 40.0, 30.0, 8.0),
        child: Column(
          children: [
            Text(
              "Anzahl der Spieler   ${widget.playernames.length}", // Display number of players
              style: Theme.of(context).textTheme.titleLarge, // Use large title style from theme
            ),
            const Divider(
              height: 60,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  "Anzahl der Werwölfe   ${game.getWolves()}", // Display number of werewolves
                  style: Theme.of(context).textTheme.bodyLarge, // Use large body style from theme
                ),
                IconButton(
                  onPressed: () {
                    setState(() {
                      game.decrementWolves(); // Decrease the number of werewolves
                    });
                  },
                  icon: const Icon(Icons.remove, color: Colors.white),
                ),
                IconButton(
                  onPressed: () {
                    setState(() {
                      game.incrementWolves(); // Increase the number of werewolves
                    });
                  },
                  icon: const Icon(Icons.add, color: Colors.white),
                ),
              ],
            ),
            const Divider(
              height: 60,
            ),
            Padding(
              padding: const EdgeInsets.only(top: 8.0, bottom: 20),
              child: Text(
                "Spezielle Rollen", // Display special roles section
                style: Theme.of(context).textTheme.titleLarge, // Use large title style from theme
              ),
            ),
            Expanded(
              child: ListView.builder(
                itemCount: Role.values.length,
                itemBuilder: (context, index) {
                  if (Role.values[index] != Role.dorfbewohner &&
                      Role.values[index] != Role.werwolf) {
                    Role role = Role.values[index];
                    return ListTile(
                      title: Text(
                        role.stringValue, // Display role name
                        style: Theme.of(context).textTheme.bodyLarge, // Use large body style from theme
                      ),
                      trailing: Switch(
                        value: game.specialRoles[role],
                        onChanged: (bool value) {
                          setState(() {
                            game.specialRoles[role] = value; // Toggle special role
                          });
                        },
                      ),
                    );
                  }
                  return Container(); // Return empty container if not a special role
                },
              ),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => FlipingCard(players: game.getAllPlayers()), // Start game
                  ),
                );
              },
              child: const Text(
                'Spiel starten!', // Start game button text
              ),
            ),
            const Padding(padding: EdgeInsets.all(30)),
          ],
        ),
      ),
    );
  }
}
