import 'dart:ui';

import 'package:flame/game.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:nes_ui/nes_ui.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:shitman/game/shitman_game.dart';
import 'package:shitman/ui/in_game_ui.dart';
import 'package:shitman/ui/touch_controls_overlay.dart';
import 'package:shitman/settings/app_settings.dart';

class AnyInputScrollBehavior extends MaterialScrollBehavior {
  // Override behavior methods and getters like dragDevices
  @override
  Set<PointerDeviceKind> get dragDevices => {
    PointerDeviceKind.touch,
    PointerDeviceKind.mouse,
    PointerDeviceKind.trackpad,
  };
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  LicenseRegistry.addLicense(() async* {
    final license = await rootBundle.loadString('google_fonts/OFL.txt');
    yield LicenseEntryWithLineBreaks(['google_fonts'], license);
  });

  // Load saved language preference
  final appSettings = AppSettings();
  await appSettings.initSettings();
  String savedLanguage = 'en';
  try {
    savedLanguage = appSettings.appSettings.getString('ui.language');
  } catch (e) {
    // Use default if not found
  }

  runApp(
    EasyLocalization(
      supportedLocales: [Locale('en'), Locale('da'), Locale('de')],
      path: 'assets/translations',
      fallbackLocale: Locale('en'),
      startLocale: Locale(savedLanguage),
      useOnlyLangCode: true,
      useFallbackTranslations: true,
      child: Shitman(),
    ),
  );
}

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

  @override
  State<Shitman> createState() => _ShitmanState();
}

class _ShitmanState extends State<Shitman> {
  late ThemeData _currentTheme;

  @override
  void initState() {
    super.initState();
    _currentTheme = flutterNesTheme(brightness: Brightness.dark);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // Recreate theme when locale changes to avoid lerp issues
    _currentTheme = flutterNesTheme(brightness: Brightness.dark);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      scrollBehavior: AnyInputScrollBehavior(),
      locale: context.locale,
      theme: _currentTheme,
      themeMode: ThemeMode.dark,
      themeAnimationDuration: Duration.zero, // Disable theme animation
      home: GameWidget(
        game: ShitmanGame(),
        initialActiveOverlays: const ['MainMenu'],
        overlayBuilderMap: {
          InGameUI.overlayID: (context, game) =>
              InGameUI(game as ShitmanGame),
          MainMenuUI.overlayID: (context, game) =>
              MainMenuUI(game as ShitmanGame),
          SettingsUI.overlayID: (context, game) =>
              SettingsUI(game as ShitmanGame),
          PauseMenuUI.overlayID: (context, game) =>
              PauseMenuUI(game as ShitmanGame),
          TouchControlsOverlayWidget.overlayID: (context, game) =>
              TouchControlsOverlayWidget(game: game as ShitmanGame),
        },
      ),
    );
  }
}
