import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'core/theme/app_theme.dart';
import 'features/auth/presentation/bloc/auth_bloc.dart';
import 'features/auth/presentation/bloc/auth_event.dart';
import 'features/chat/presentation/bloc/chat_bloc.dart';
import 'features/chat/presentation/bloc/chat_event.dart';
import 'internal/di/injection_container.dart' as di;
import 'features/settings/presentation/bloc/settings_bloc.dart';
import 'features/settings/presentation/bloc/settings_event.dart';
import 'features/settings/presentation/bloc/settings_state.dart';
import 'l10n/app_localizations.dart';
import 'internal/router/main_screen.dart';
import 'features/auth/presentation/pages/login_page.dart';
import 'features/auth/presentation/bloc/auth_state.dart';

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

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<AuthBloc>(
          create: (_) => di.sl<AuthBloc>()..add(const AuthEvent.authChecked()),
        ),
        BlocProvider<ChatBloc>(
          create: (_) => di.sl<ChatBloc>()..add(const ChatEvent.started()),
        ),
        BlocProvider<SettingsBloc>(
          create: (_) => di.sl<SettingsBloc>()..add(const SettingsEvent.started()),
        ),
      ],
      child: BlocBuilder<SettingsBloc, SettingsState>(
        builder: (context, state) {
          final locale = state.maybeWhen(
            loaded: (_, __, languageCode, ___, ____) => Locale(languageCode),
            orElse: () => const Locale('ru'),
          );

          return MaterialApp(
            title: 'Messenger',
            debugShowCheckedModeBanner: false,
            theme: AppTheme.lightTheme,
            darkTheme: AppTheme.darkTheme,
            themeMode: ThemeMode.system,
            locale: locale,
            supportedLocales: const [
              Locale('ru'),
              Locale('en'),
            ],
            localizationsDelegates: const [
              AppLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            home: BlocListener<AuthBloc, AuthState>(
              listener: (context, authState) {
                authState.maybeWhen(
                  authenticated: (userId) {
                    context.read<ChatBloc>().add(ChatEvent.started(userId: userId));
                  },
                  unauthenticated: () {
                    context.read<ChatBloc>().add(const ChatEvent.cacheCleared());
                  },
                  orElse: () {},
                );
              },
              child: BlocBuilder<AuthBloc, AuthState>(
                builder: (context, authState) {
                  return authState.maybeWhen(
                    authenticated: (_) => const MainScreen(),
                    orElse: () => const LoginPage(),
                  );
                },
              ),
            ),
          );
        },
      ),
    );
  }
}
