import 'package:flutter/material.dart'; import 'package:el_impostor/l10n/generated/app_localizations.dart'; import 'package:provider/provider.dart'; import '../estado/estado_juego.dart'; import '../servicios/servicio_notas.dart'; import '../tema/tema_app.dart'; class PantallaNotas extends StatefulWidget { const PantallaNotas({super.key}); @override State createState() => _PantallaNotasState(); } class _PantallaNotasState extends State { String? _jugadorSeleccionadoId; final Map _controladores = {}; final _controladorNotaLibre = TextEditingController(); bool _cargado = false; @override void dispose() { for (final c in _controladores.values) { c.dispose(); } _controladorNotaLibre.dispose(); super.dispose(); } Future _cargarNotas(String jugadorId) async { final datos = await ServicioNotas.cargarNotas(jugadorId); final notas = datos['notas'] as Map; final notaLibre = datos['notaLibre'] as String; for (final entrada in notas.entries) { if (_controladores.containsKey(entrada.key)) { _controladores[entrada.key]!.text = entrada.value; } } _controladorNotaLibre.text = notaLibre; setState(() => _cargado = true); } Future _guardarNotas() async { if (_jugadorSeleccionadoId == null) return; final notas = {}; for (final entrada in _controladores.entries) { if (entrada.value.text.isNotEmpty) { notas[entrada.key] = entrada.value.text; } } await ServicioNotas.guardarNotas( _jugadorSeleccionadoId!, notas, _controladorNotaLibre.text, ); } @override Widget build(BuildContext context) { final l10n = AppLocalizations.of(context)!; final estado = context.watch(); final partida = estado.partida; if (partida == null) return const SizedBox.shrink(); // Inicializar controladores for (final j in partida.jugadores) { _controladores.putIfAbsent(j.id, () => TextEditingController()); } return Scaffold( appBar: AppBar( title: Text(l10n.notesTitle), actions: [ if (_jugadorSeleccionadoId != null) IconButton( icon: const Icon(Icons.save), onPressed: () async { await _guardarNotas(); if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(l10n.notesSaved)), ); } }, ), ], ), body: _jugadorSeleccionadoId == null ? _construirSelectorJugador(partida) : _construirNotas(partida), ); } Widget _construirSelectorJugador(dynamic partida) { final l10n = AppLocalizations.of(context)!; return Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( l10n.whoAreYou, style: Theme.of(context).textTheme.headlineMedium, ), const SizedBox(height: 8), Text( l10n.selectYourName, style: Theme.of(context).textTheme.bodyMedium, ), const SizedBox(height: 16), Expanded( child: ListView.builder( itemCount: partida.jugadoresActivos.length, itemBuilder: (context, index) { final j = partida.jugadoresActivos[index]; return Card( child: ListTile( leading: CircleAvatar( backgroundColor: TemaApp.colorAcento, child: Text('${index + 1}', style: const TextStyle(color: Colors.white)), ), title: Text(j.nombre), trailing: const Icon(Icons.arrow_forward_ios, size: 16), onTap: () { setState(() { _jugadorSeleccionadoId = j.id; _cargado = false; }); _cargarNotas(j.id); }, ), ); }, ), ), ], ), ); } Widget _construirNotas(dynamic partida) { if (!_cargado) { return const Center(child: CircularProgressIndicator()); } final l10n = AppLocalizations.of(context)!; final jugadorActual = partida.jugadores .firstWhere((j) => j.id == _jugadorSeleccionadoId); return SingleChildScrollView( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ IconButton( icon: const Icon(Icons.arrow_back), onPressed: () async { await _guardarNotas(); setState(() { _jugadorSeleccionadoId = null; _cargado = false; for (final c in _controladores.values) { c.clear(); } _controladorNotaLibre.clear(); }); }, ), Text( l10n.notesOf(jugadorActual.nombre), style: Theme.of(context).textTheme.titleLarge, ), ], ), const SizedBox(height: 16), // Notas por jugador Text( l10n.notesAboutPlayers, style: Theme.of(context).textTheme.titleMedium?.copyWith( color: TemaApp.colorTextoSecundario, ), ), const SizedBox(height: 8), ...partida.jugadoresActivos.where((j) => j.id != _jugadorSeleccionadoId).map((j) { return Padding( padding: const EdgeInsets.only(bottom: 8), child: TextField( controller: _controladores[j.id], decoration: InputDecoration( labelText: j.nombre, prefixIcon: const Icon(Icons.person, size: 20), hintText: l10n.playerNoteHint, ), maxLines: 2, minLines: 1, ), ); }), const SizedBox(height: 16), Text( l10n.freeNote, style: Theme.of(context).textTheme.titleMedium?.copyWith( color: TemaApp.colorTextoSecundario, ), ), const SizedBox(height: 8), TextField( controller: _controladorNotaLibre, decoration: InputDecoration( hintText: l10n.freeNoteHint, prefixIcon: const Icon(Icons.note, size: 20), ), maxLines: 5, minLines: 3, ), ], ), ); } }