import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';
import 'data/repositories/hero_repository.dart';
import 'services/api_service.dart';
import 'presentation/home_page/bloc/hero_list_bloc.dart';
import 'presentation/home_page/bloc/hero_search_bloc.dart';
import 'components/screens/hero_list_screen.dart';
import 'Components/locale/l10n/app_locale.dart';

void main() {
  final apiService = ApiService(baseUrl: 'https://assets.deadlock-api.com');
  final heroRepository = HeroRepository(apiService: apiService);

  runApp(MyApp(heroRepository: heroRepository));
}

class LocaleNotifier with ChangeNotifier {
  Locale _currentLocale = const Locale('en'); // Начальный язык - английский

  Locale get currentLocale => _currentLocale;

  void toggleLocale() {
    _currentLocale =
    _currentLocale.languageCode == 'en' ? const Locale('ru') : const Locale('en');
    notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  final HeroRepository heroRepository;

  const MyApp({Key? key, required this.heroRepository}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => LocaleNotifier(),
      child: Provider<HeroRepository>(
        create: (_) => heroRepository,
        child: BlocProvider(
          create: (_) => HeroListBloc(heroRepository)..add(FetchHeroes()),
          child: Consumer<LocaleNotifier>(
            builder: (context, localeNotifier, child) {
              return MaterialApp(
                locale: localeNotifier.currentLocale,
                localizationsDelegates: AppLocale.localizationsDelegates,
                supportedLocales: AppLocale.supportedLocales,
                home: Builder(
                  builder: (context) {
                    final heroListBloc = context.read<HeroListBloc>();
                    return BlocBuilder<HeroListBloc, HeroListState>(
                      builder: (context, state) {
                        if (state is HeroListLoaded) {
                          return MultiBlocProvider(
                            providers: [
                              BlocProvider(
                                create: (_) =>
                                    HeroSearchBloc(allHeroes: state.heroes),
                              ),
                            ],
                            child: const HeroListScreen(),
                          );
                        } else if (state is HeroListLoading) {
                          return const Scaffold(
                            body: Center(
                              child: CircularProgressIndicator(),
                            ),
                          );
                        } else if (state is HeroListError) {
                          return const Scaffold(
                            body: Center(
                              child: Text('Failed to load heroes.'),
                            ),
                          );
                        }
                        return const Scaffold(
                          body: Center(
                            child: Text('Initializing...'),
                          ),
                        );
                      },
                    );
                  },
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}
