import 'package:cargocalculaterapp/router/app_routes.dart';
import 'package:cargocalculaterapp/router/name_routes.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'core/app_bloc/app_bloc.dart';
import 'core/theme/theme_data.dart';
import 'generated/l10n.dart';

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

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AppBloc, AppState>(
      builder: (context, state) {
        return KeyboardDismissOnTap(
          child: MaterialApp(
            navigatorKey: rootNavigatorKey,
            initialRoute: Routes.initial,
            onGenerateRoute: AppRoutes.onGenerateRoute,
            title: 'CargoApp',
            debugShowCheckedModeBanner: false,
            themeMode: ThemeMode.values.byName(
              state.themeMode?.name ?? ThemeMode.light.name,
            ),
            theme: state.themeMode == ThemeMode.light ? lightTheme : darkTheme,
            darkTheme: darkTheme,
            locale: Locale(state.appLocale),
            supportedLocales: AppLocalization.delegate.supportedLocales,
            localizationsDelegates: const [
              AppLocalization.delegate,
              GlobalCupertinoLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
            ],
          ),
        );
      },
    );
  }
}
