import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:loader_overlay/loader_overlay.dart';

import '../application/auth/auth_bloc.dart';
import '../application/language/language_bloc.dart';
import '../common/theme/theme.dart';
import '../common/constant/app_constant.dart';
import '../injection.dart';
import '../l10n/app_localizations.dart';
import 'components/loading/loading_overlay.dart';
import 'router/app_router.dart';
import 'router/app_router_observer.dart';

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

  @override
  State<AppWidget> createState() => _AppWidgetState();
}

class _AppWidgetState extends State<AppWidget> {
  final _appRouter = getIt<AppRouter>();

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(create: (context) => getIt<LanguageBloc>()),
        BlocProvider(create: (context) => getIt<AuthBloc>()),
      ],
      child: GlobalLoaderOverlay(
        useDefaultLoading: false,
        overlayWidgetBuilder: (progress) => LoadingOverlay(),
        overlayColor: AppColor.black.withOpacity(0.35),
        child: BlocBuilder<LanguageBloc, LanguageState>(
          builder: (context, state) {
            return MaterialApp.router(
              debugShowCheckedModeBanner: false,
              title: AppConstant.appName,
              locale: state.language.locale,
              supportedLocales: AppLocalizations.supportedLocales,
              localizationsDelegates: AppLocalizations.localizationsDelegates,
              theme: ThemeApp.theme,
              routerConfig: _appRouter.config(
                navigatorObservers: () => <NavigatorObserver>[
                  AppRouteObserver(),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
