import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:farolero/l10n/generated/app_localizations.dart';
import 'package:provider/provider.dart';
import 'estado/estado_juego.dart';
import 'servicios/servicio_historial_partidas.dart';
import 'servicios/servicio_idioma.dart';
import 'servicios/servicio_nearby.dart';
import 'servicios/servicio_perfil_usuario.dart';
import 'tema/componentes_farolero.dart';
import 'tema/tema_app.dart';
import 'pantallas/pantalla_principal.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.light,
      systemNavigationBarColor: TemaApp.colorFondo,
    ),
  );
  runApp(const FaroleroApp());
}

class FaroleroApp extends StatelessWidget {
  const FaroleroApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (_) => EstadoJuego()..cargarBanco(),
        ),
        ChangeNotifierProvider(
          create: (_) => ServicioIdioma()..cargar(),
        ),
        ChangeNotifierProvider(
          create: (_) => ServicioPerfilUsuario()..cargar(),
        ),
        ChangeNotifierProvider(
          create: (_) => ServicioHistorialPartidas()..cargar(),
        ),
        ChangeNotifierProvider(
          create: (_) => ServicioNearby(),
        ),
      ],
      child: Consumer<ServicioIdioma>(
        builder: (context, servicioIdioma, _) {
          return MaterialApp(
            title: 'Farolero',
            theme: TemaApp.obtenerTema(),
            debugShowCheckedModeBanner: false,
            locale: servicioIdioma.locale,
            localizationsDelegates: const [
              AppLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: ServicioIdioma.localesSoportados,
            home: const PantallaCarga(),
          );
        },
      ),
    );
  }
}

class PantallaCarga extends StatelessWidget {
  const PantallaCarga({super.key});

  @override
  Widget build(BuildContext context) {
    final estado = context.watch<EstadoJuego>();
    final l10n = AppLocalizations.of(context);

    if (estado.cargando || estado.banco == null) {
      return Scaffold(
        body: FondoFarolero(
          intenso: true,
          child: SafeArea(
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 34, vertical: 28),
              child: Column(
                children: [
                  const Spacer(flex: 2),
                  const Icon(
                    Icons.lightbulb,
                    color: TemaApp.colorNaranja,
                    size: 86,
                  ),
                  const SizedBox(height: 18),
                  const LogoFarolero(size: 58),
                  const Spacer(flex: 3),
                  ClipRRect(
                    borderRadius: BorderRadius.circular(4),
                    child: const LinearProgressIndicator(
                      minHeight: 8,
                      color: TemaApp.colorNaranja,
                      backgroundColor: TemaApp.colorSuperficie,
                    ),
                  ),
                  const SizedBox(height: 14),
                  Text(
                    l10n?.loadingWords ?? 'Cargando palabras...',
                    style: Theme.of(context).textTheme.titleMedium?.copyWith(
                      color: TemaApp.colorDorado,
                    ),
                    textAlign: TextAlign.center,
                  ),
                  const Spacer(),
                ],
              ),
            ),
          ),
        ),
      );
    }

    return const PantallaPrincipal();
  }
}
