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:go_router/go_router.dart';
import 'package:wyatt_clean_code/core/design_system/theme.dart';
import 'package:wyatt_clean_code/core/extensions/build_context_extension.dart';
import 'package:wyatt_clean_code/core/flavors/flavor_settings.dart';
import 'package:wyatt_clean_code/core/routes/router.dart';
import 'package:wyatt_clean_code/core/utils/screen_util.dart';
import 'package:wyatt_clean_code/data/repositories/counter_repository_impl.dart';
import 'package:wyatt_clean_code/domain/repositories/counter_repository.dart';

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

  final GoRouter _router = GoRouter(
    initialLocation: '/',
    routes: AppRouter.routes,
    debugLogDiagnostics: true,
    errorBuilder: (_, __) => const ColoredBox(
      color: Colors.red,
    ),
  );

  Widget _bannerFlavor(Widget child) {
    final flavorInstance = FlavorSettings.get();
    if (flavorInstance.displayBanner && !kReleaseMode) {
      return Directionality(
        textDirection: TextDirection.ltr,
        child: Banner(
          location: BannerLocation.topEnd,
          message: flavorInstance.flavor.short,
          color: flavorInstance.flavor.color,
          child: child,
        ),
      );
    }
    return child;
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init();
    return MultiRepositoryProvider(
      providers: [
        RepositoryProvider<CounterRepository>(
          lazy: true,
          create: (context) => CounterRepositoryImpl(),
        ),
      ],
      child: _bannerFlavor(
        MaterialApp.router(
          title: 'Wyatt Demo',
          theme: AppTheme.light,
          debugShowCheckedModeBanner: false,
          routerDelegate: _router.routerDelegate,
          routeInformationParser: _router.routeInformationParser,
          routeInformationProvider: _router.routeInformationProvider,
          localizationsDelegates: const [
            AppLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: AppLocalizations.supportedLocales,
        ),
      ),
    );
  }
}
