import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

import '../features/mapflow/application/place_cubit.dart';
import '../features/mapflow/data/auth_repository.dart';
import '../features/mapflow/data/mapflow_graphql_client.dart';
import '../features/mapflow/data/places_repository.dart';
import '../features/mapflow/data/voice_experiences_repository.dart';
import '../shared/location/current_location.dart';
import 'router/app_router.dart';
import 'theme/mapflow_theme.dart';

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

  @override
  State<MapflowApp> createState() => _MapflowAppState();
}

class _MapflowAppState extends State<MapflowApp> {
  late final MapflowGraphqlClient _graphqlClient;
  late final AuthRepository _authRepository;
  late final PlacesRepository _placesRepository;
  late final VoiceExperiencesRepository _voiceExperiencesRepository;
  late final PlaceCubit _placeCubit;
  late final GoRouter _router;

  @override
  void initState() {
    super.initState();
    _graphqlClient = MapflowGraphqlClient();
    _authRepository = AuthRepository(client: _graphqlClient);
    _placesRepository = PlacesRepository(client: _graphqlClient);
    _voiceExperiencesRepository = VoiceExperiencesRepository(
      client: _graphqlClient,
    );
    _placeCubit = PlaceCubit(
      authRepository: _authRepository,
      placesRepository: _placesRepository,
      voiceExperiencesRepository: _voiceExperiencesRepository,
      location: CurrentLocation(),
    )..load();
    _router = createAppRouter();
  }

  @override
  void dispose() {
    _router.dispose();
    _placeCubit.close();
    _graphqlClient.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final shortcuts =
        Map<ShortcutActivator, Intent>.of(WidgetsApp.defaultShortcuts)
          ..[const SingleActivator(LogicalKeyboardKey.tab)] =
              const NextFocusIntent()
          ..[const SingleActivator(LogicalKeyboardKey.tab, shift: true)] =
              const PreviousFocusIntent();

    return MultiRepositoryProvider(
      providers: [
        RepositoryProvider.value(value: _authRepository),
        RepositoryProvider.value(value: _placesRepository),
        RepositoryProvider.value(value: _voiceExperiencesRepository),
      ],
      child: BlocProvider.value(
        value: _placeCubit,
        child: MaterialApp.router(
          title: 'MapFlow',
          debugShowCheckedModeBanner: false,
          routerConfig: _router,
          scrollBehavior: const MaterialScrollBehavior().copyWith(
            scrollbars: true,
            dragDevices: {
              PointerDeviceKind.touch,
              PointerDeviceKind.mouse,
              PointerDeviceKind.trackpad,
              PointerDeviceKind.stylus,
            },
          ),
          theme: MapflowTheme.dark(),
          builder: (context, child) {
            return Shortcuts(
              shortcuts: shortcuts,
              child: FocusTraversalGroup(
                policy: ReadingOrderTraversalPolicy(),
                child: child ?? const SizedBox.shrink(),
              ),
            );
          },
        ),
      ),
    );
  }
}
