import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:starting_template/core/dependency_injection/get_it.dart';
import 'package:starting_template/core/flavors/flavor.dart';
import 'package:starting_template/core/routes/router.dart';
import 'package:starting_template/data/repositories/counter_repository_impl.dart';
import 'package:starting_template/domain/repositories/counter_repository.dart';
import 'package:starting_template/gen/app_localizations.dart';
import 'package:starting_template/presentation/features/counter/blocs/counter_bloc/counter_bloc.dart';
import 'package:wyatt_bloc_helper/wyatt_bloc_helper.dart';

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

  Widget _flavorBanner(Widget child) {
    final flavor = Flavor.get();
    if (flavor.banner != null && !kReleaseMode) {
      return Directionality(
        textDirection: TextDirection.ltr,
        child: Banner(
          location: BannerLocation.topEnd,
          message: flavor.banner!,
          color: flavor.bannerColor,
          child: child,
        ),
      );
    }

    return child;
  }

  @override
  Widget build(BuildContext context) => MultiProvider(
      repositoryProviders: [
        RepositoryProvider<CounterRepository>(
          create: (_) => CounterRepositoryImpl(counterDataSource: getIt()),
        ),
      ],
      blocProviders: [
        BlocProvider<CounterBloc>(
          create: (_) => CounterBloc(),
        ),
      ],
      child: _flavorBanner(
        MaterialApp.router(
          title: 'Display Name',
          debugShowCheckedModeBanner: false,
          routerDelegate: AppRouter.router.routerDelegate,
          routeInformationParser: AppRouter.router.routeInformationParser,
          routeInformationProvider: AppRouter.router.routeInformationProvider,
          localizationsDelegates: const [
            AppLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: AppLocalizations.supportedLocales,
        ),
      ),
    );
}
