import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'l10n/app_localizations.dart';
import 'ui/main_menu.dart';
import 'utils/colors.dart';
import 'utils/settings_manager.dart';
import 'utils/score_manager.dart';
import 'utils/google_play_games_manager.dart';
import 'utils/locale_manager.dart';
import 'utils/app_lifecycle_manager.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SettingsManager.init();
  await ScoreManager.initialize();
  await LocaleManager.init();
  
  // Initialize Google Play Games (silent init, don't require sign-in)
  await GooglePlayGamesManager.instance.initialize();
  
  // Initialize app lifecycle manager for game suspension
  AppLifecycleManager.instance.initialize();
  
  runApp(const ZenTapApp());
}

class ZenTapApp extends StatefulWidget {
  const ZenTapApp({super.key});

  @override
  State<ZenTapApp> createState() => _ZenTapAppState();
}

class _ZenTapAppState extends State<ZenTapApp> {
  static _ZenTapAppState? _instance;

  @override
  void initState() {
    super.initState();
    _instance = this;
    // Set up locale change callback
    LocaleManager.setLocaleChangeCallback(() {
      setState(() {});
    });
  }

  @override
  void dispose() {
    _instance = null;
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ZenTap',
      debugShowCheckedModeBanner: false,
      locale: LocaleManager.currentLocale,
      localizationsDelegates: const [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: LocaleManager.supportedLocales,
      theme: ThemeData(
        // Use ZenTap color scheme
        colorScheme: ColorScheme.dark(
          primary: ZenColors.buttonBackground,
          secondary: ZenColors.defaultLink,
          surface: ZenColors.uiElements,
          onPrimary: ZenColors.buttonText,
          onSecondary: ZenColors.white,
          onSurface: ZenColors.primaryText,
        ),
        scaffoldBackgroundColor: ZenColors.appBackground,
        appBarTheme: const AppBarTheme(
          backgroundColor: ZenColors.appBackground,
          foregroundColor: ZenColors.primaryText,
          elevation: 0,
          systemOverlayStyle: SystemUiOverlayStyle.light,
        ),
        textTheme: const TextTheme(
          bodyLarge: TextStyle(color: ZenColors.primaryText),
          bodyMedium: TextStyle(color: ZenColors.primaryText),
          bodySmall: TextStyle(color: ZenColors.secondaryText),
          titleLarge: TextStyle(color: ZenColors.primaryText),
          titleMedium: TextStyle(color: ZenColors.primaryText),
          titleSmall: TextStyle(color: ZenColors.primaryText),
        ),
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            backgroundColor: ZenColors.buttonBackground,
            foregroundColor: ZenColors.buttonText,
            elevation: 4,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12),
            ),
          ),
        ),
        dialogTheme: DialogThemeData(
          backgroundColor: ZenColors.uiElements,
          titleTextStyle: const TextStyle(
            color: ZenColors.primaryText,
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
          contentTextStyle: TextStyle(
            color: ZenColors.secondaryText,
            fontSize: 16,
          ),
        ),
        useMaterial3: true,
      ),
      home: const MainMenu(),
    );
  }
}
