import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:dynamic_color/dynamic_color.dart';

import './config.dart';
import './router.dart';
import './providers/theme.dart';

void main() {
  runApp(ProviderScope(
    child: const MyApp()
  ));
}

class MyApp extends ConsumerWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final useDynamicColor = ref.watch(useDynamicColorProvider);
    return DynamicColorBuilder(
      builder: (light, dark) {
        final defaultLight = AppConfig.defaultLightColorScheme();
        final lightColorScheme = useDynamicColor ? (light ?? defaultLight) : defaultLight;
        final defaultDark = AppConfig.defaultDarkColorScheme();
        final darkColorScheme = useDynamicColor ? (dark ?? defaultDark) : defaultDark;

        final appBarTheme = AppBarTheme(
          backgroundColor: lightColorScheme.inversePrimary,
          foregroundColor: lightColorScheme.onInverseSurface,
          elevation: 0,
        );

        return MaterialApp.router(
          theme: ThemeData(
            useMaterial3: true,
            colorScheme: lightColorScheme,
            appBarTheme: AppBarTheme(
              backgroundColor: lightColorScheme.inversePrimary,
              // foregroundColor: lightColorScheme.onInverseSurface,
            )
          ),
          darkTheme: ThemeData(
            useMaterial3: true,
            colorScheme: darkColorScheme,
            appBarTheme: AppBarTheme(
              backgroundColor: darkColorScheme.inversePrimary,
            )
          ),
          themeMode: ref.watch(themeModeProvider).toThemeMode(),
          routerConfig: router,
        );
      },
    );
  }
}
