import 'package:converterpro/app_router.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:converterpro/models/settings.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:translations/app_localizations.dart';
import 'package:dynamic_color/dynamic_color.dart';

void main() async {
  LicenseRegistry.addLicense(() async* {
    yield LicenseEntryWithLineBreaks([
      'Josefin Sans',
    ], await rootBundle.loadString('assets/fonts/OFL.txt'));
  });

  WidgetsFlutterBinding.ensureInitialized();
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends ConsumerWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return DynamicColorBuilder(
      builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
        if (lightDynamic != null) {
          WidgetsBinding.instance.addPostFrameCallback(
            (_) => ref.read(deviceAccentColorProvider.notifier).state =
                lightDynamic.primary,
          );
        }

        return Consumer(
          builder: (context, ref, child) {
            final colorTheme = ref.watch(actualColorThemeProvider);
            ThemeData lightTheme = ThemeData(
              colorScheme: ColorScheme.fromSeed(seedColor: colorTheme),
            );
            ThemeData darkTheme = ThemeData(
              colorScheme: ColorScheme.fromSeed(
                seedColor: colorTheme,
                brightness: Brightness.dark,
              ),
              brightness: Brightness.dark,
            );
            ThemeData amoledTheme = darkTheme.copyWith(
              scaffoldBackgroundColor: Colors.black,
              drawerTheme: const DrawerThemeData(backgroundColor: Colors.black),
              appBarTheme: const AppBarTheme(backgroundColor: Colors.black),
              bottomAppBarTheme: const BottomAppBarThemeData(
                color: Colors.black,
              ),
            );

            final pageTransitionsTheme = PageTransitionsTheme(
              builders:
                  Map<TargetPlatform, PageTransitionsBuilder>.fromIterable(
                    TargetPlatform.values,
                    value: (e) => e == TargetPlatform.android
                        ? const PredictiveBackFullscreenPageTransitionsBuilder()
                        : const FadeForwardsPageTransitionsBuilder(),
                  ),
            );
            lightTheme = lightTheme.copyWith(
              pageTransitionsTheme: pageTransitionsTheme,
            );
            darkTheme = darkTheme.copyWith(
              pageTransitionsTheme: pageTransitionsTheme,
            );
            amoledTheme = amoledTheme.copyWith(
              pageTransitionsTheme: pageTransitionsTheme,
            );
            final ThemeMode themeMode =
                ThemeMode.values[ref.watch(themeModeProvider).value ??
                    ThemeMode.system.index];
            final appLocale = ref.watch(actualLocaleProvider);
            final appRouter = ref.read(routerProvider);

            return MaterialApp.router(
              routeInformationProvider: appRouter.routeInformationProvider,
              routeInformationParser: appRouter.routeInformationParser,
              routerDelegate: appRouter.routerDelegate,
              debugShowCheckedModeBanner: false,
              title: 'Converter NOW',
              themeMode: themeMode,
              theme: lightTheme,
              darkTheme: (ref.watch(isPureDarkProvider).value ?? false)
                  ? amoledTheme
                  : darkTheme,
              supportedLocales: mapLocale.keys,
              localizationsDelegates: AppLocalizations.localizationsDelegates,
              locale: appLocale,
            );
          },
        );
      },
    );
  }
}
