import 'package:go_router/go_router.dart';
import 'package:sfm_app/feature/devices/device_detail/device_detail_bloc.dart';
import 'package:sfm_app/feature/devices/device_detail/device_detail_screen.dart';
import 'package:sfm_app/feature/settings/device_notification_settings/device_notification_settings_bloc.dart';
import 'package:sfm_app/feature/settings/device_notification_settings/device_notification_settings_screen.dart';
import '../app/app_constants.dart';
import '../../../feature/auth/login/bloc/login_bloc.dart';
import '../../../feature/auth/login/screen/login_screen.dart';
import '../../../feature/bell/bell_bloc.dart';
import '../../../feature/bell/bell_screen.dart';
import '../../../feature/devices/device_update/device_update_bloc.dart';
import '../../../feature/devices/device_update/device_update_screen.dart';
import '../../../feature/devices/devices_manager_bloc.dart';
import '../../../feature/devices/devices_manager_screen.dart';
import '../../../feature/error/not_found_screen.dart';
import '../../../feature/inter_family/group_detail/group_detail_bloc.dart';
import '../../../feature/inter_family/group_detail/group_detail_screen.dart';
import '../../../feature/inter_family/inter_family_bloc.dart';
import '../../../feature/inter_family/inter_family_screen.dart';
import '../../../feature/log/device_logs_bloc.dart';
import '../../../feature/log/device_logs_screen.dart';
import '../../../feature/main/main_bloc.dart';
import '../../../feature/main/main_screen.dart';
import '../../../feature/map/map_bloc.dart';
import '../../../feature/map/map_screen.dart';
import '../../../feature/settings/settings_bloc.dart';
import '../../../feature/settings/settings_screen.dart';
import '../../../product/base/bloc/base_bloc.dart';
import '../enums/app_route_enums.dart';
import '../../../product/shared/shared_transition.dart';

GoRouter goRouter() {
  return GoRouter(
    debugLogDiagnostics: true,
    errorBuilder: (context, state) => const NotFoundScreen(),
    initialLocation: ApplicationConstants.LOGIN_PATH,
    routes: <RouteBase>[
      GoRoute(
        path: ApplicationConstants.LOGIN_PATH,
        name: AppRoutes.LOGIN.name,
        builder: (context, state) => BlocProvider(
          child: const LoginScreen(),
          blocBuilder: () => LoginBloc(),
        ),
      ),
      GoRoute(
        path: ApplicationConstants.HOME_PATH,
        name: AppRoutes.HOME.name,
        builder: (context, state) => BlocProvider(
          child: const MainScreen(),
          blocBuilder: () => MainBloc(),
        ),
      ),
      GoRoute(
        path: ApplicationConstants.SETTINGS_PATH,
        name: AppRoutes.SETTINGS.name,
        pageBuilder: (context, state) => CustomTransitionPage(
          child: BlocProvider(
            child: const SettingsScreen(),
            blocBuilder: () => SettingsBloc(),
          ),
          transitionsBuilder: transitionsBottomToTop,
        ),
      ),
      GoRoute(
        path: ApplicationConstants.BELL_PATH,
        name: AppRoutes.BELL.name,
        pageBuilder: (context, state) => CustomTransitionPage(
            child: BlocProvider(
              child: const BellScreen(),
              blocBuilder: () => BellBloc(),
            ),
            transitionsBuilder: transitionsCustom1),
      ),
      GoRoute(
        path: ApplicationConstants.DEVICES_MANAGER_PATH,
        name: AppRoutes.DEVICES.name,
        builder: (context, state) => BlocProvider(
          child: const DevicesManagerScreen(),
          blocBuilder: () => DevicesManagerBloc(),
        ),
      ),
      GoRoute(
        path: '${ApplicationConstants.DEVICES_UPDATE_PATH}/:thingID',
        name: AppRoutes.DEVICE_UPDATE.name,
        pageBuilder: (context, state) => CustomTransitionPage(
            child: BlocProvider(
              child: DeviceUpdateScreen(
                thingID: state.pathParameters['thingID']!,
              ),
              blocBuilder: () => DeviceUpdateBloc(),
            ),
            transitionsBuilder: transitionsBottomToTop),
      ),
      GoRoute(
        path: '${ApplicationConstants.DEVICES_DETAIL_PATH}/:thingID',
        name: AppRoutes.DEVICE_DETAIL.name,
        pageBuilder: (context, state) => CustomTransitionPage(
            child: BlocProvider(
              child: DetailDeviceScreen(
                thingID: state.pathParameters['thingID']!,
              ),
              blocBuilder: () => DetailDeviceBloc(),
            ),
            transitionsBuilder: transitionsRightToLeft),
      ),
      GoRoute(
        path: ApplicationConstants.MAP_PATH,
        name: AppRoutes.MAP.name,
        builder: (context, state) => BlocProvider(
          child: const MapScreen(),
          blocBuilder: () => MapBloc(),
        ),
      ),
      GoRoute(
        path: ApplicationConstants.DEVICE_LOGS_PATH,
        name: AppRoutes.HISTORY.name,
        builder: (context, state) => BlocProvider(
          child: const DeviceLogsScreen(),
          blocBuilder: () => DeviceLogsBloc(),
        ),
      ),
      GoRoute(
        path: ApplicationConstants.GROUP_PATH,
        name: AppRoutes.GROUPS.name,
        builder: (context, state) => BlocProvider(
          child: const InterFamilyScreen(),
          blocBuilder: () => InterFamilyBloc(),
        ),
      ),
      GoRoute(
          path: '${ApplicationConstants.GROUP_PATH}/:groupId',
          name: AppRoutes.GROUP_DETAIL.name,
          pageBuilder: (context, state) {
            final groupId = state.pathParameters['groupId']!;
            final role = state.extra! as String;
            return CustomTransitionPage(
                child: BlocProvider(
                  child: DetailGroupScreen(group: groupId, role: role),
                  blocBuilder: () => DetailGroupBloc(),
                ),
                transitionsBuilder: transitionsRightToLeft);
          }),
      GoRoute(
        path: ApplicationConstants.DEVICE_NOTIFICATIONS_SETTINGS,
        name: AppRoutes.DEVICE_NOTIFICATION_SETTINGS.name,
        pageBuilder: (context, state) => CustomTransitionPage(
            child: BlocProvider(
              child: const DeviceNotificationSettingsScreen(),
              blocBuilder: () => DeviceNotificationSettingsBloc(),
            ),
            transitionsBuilder: transitionsRightToLeft),
      ),
    ],
  );
}
