// import 'package:chuck_interceptor/chuck_interceptor.dart';
import 'package:cargocalculaterapp/features/auth/presentation/bloc/auth_confirm/auth_confirm_bloc.dart';
import 'package:cargocalculaterapp/features/auth/presentation/bloc/sign_up/sign_up_bloc.dart';
import 'package:cargocalculaterapp/features/auth/presentation/pages/auth_confirm/auth_confirm_page.dart';
import 'package:cargocalculaterapp/features/auth/presentation/pages/sign_up/sign_up_page.dart';
import 'package:cargocalculaterapp/features/calculator/presentation/bloc/calculator_info/calculator_info_bloc.dart';
import 'package:cargocalculaterapp/features/calculator/presentation/pages/calculator_info/calculator_info_page.dart';
import 'package:cargocalculaterapp/features/home/presentation/bloc/notification/notification_bloc.dart';
import 'package:cargocalculaterapp/features/home/presentation/pages/comment/comment_page.dart';
import 'package:cargocalculaterapp/features/main/presentation/pages/main_page.dart';
import 'package:cargocalculaterapp/features/profile/presentation/pages/profile_page.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../core/local_source/local_source.dart';
import '../features/auth/presentation/bloc/auth/auth_bloc.dart';
import '../features/auth/presentation/pages/auth/auth_page.dart';
import '../features/auth/presentation/pages/auth_confirm/argument/auth_confirm_argument.dart';
import '../features/calculator/presentation/arguments/calculator_info_argument.dart';
import '../features/home/presentation/bloc/comment/comment_bloc.dart';
import '../features/home/presentation/bloc/order_single/order_single_bloc.dart';
import '../features/home/presentation/pages/arguments/comment_argument.dart';
import '../features/home/presentation/pages/arguments/image_page_argument.dart';
import '../features/home/presentation/pages/arguments/order_info_argument.dart';
import '../features/home/presentation/pages/home_page.dart';
import '../features/home/presentation/pages/image/image_page.dart';
import '../features/home/presentation/pages/notification/notification_page.dart';
import '../features/home/presentation/pages/order_info/order_info_page.dart';
import '../features/initial/pages/initial_page.dart';
import '../features/language/presentation/argument/language_argument.dart';
import '../features/language/presentation/pages/language/app_language_page.dart';
import '../injector_container.dart';
import 'name_routes.dart';

final rootNavigatorKey = GlobalKey<NavigatorState>();
final localSource = sl<LocalSource>();

// final Chuck chuck = Chuck(
//   showNotification: true,
//   showInspectorOnShake: false,
//   darkTheme: false,
//   navigatorKey: rootNavigatorKey,
// );

class AppRoutes {
  AppRoutes._();

  static Route<dynamic> onGenerateRoute(RouteSettings settings) {
    if (kDebugMode) {
      print("route : ${settings.name}");
    }
    switch (settings.name) {
      case Routes.initial:
        return buildPageWithNoTransition(child: const InitialPage());
      case Routes.language:
        return buildPageWithNoTransition(
          child: AppLanguagePage(
            languageArgument: settings.arguments is LanguageArgument
                ? settings.arguments as LanguageArgument
                : null,
          ),
        );
      case Routes.auth:
        return MaterialPageRoute(
          builder: (_) => BlocProvider<AuthBloc>(
            create: (_) => sl<AuthBloc>(),
            child: const AuthPage(),
          ),
        );
      case Routes.signUp:
        return MaterialPageRoute(
          builder: (_) => BlocProvider<SignUpBloc>(
            create: (_) => sl<SignUpBloc>(),
            child: const SignUpPage(),
          ),
        );
      case Routes.authConfirm:
        return MaterialPageRoute(
          builder: (_) => BlocProvider<AuthConfirmBloc>(
            create: (_) => sl<AuthConfirmBloc>(),
            child: AuthConfirmPage(
              argument: settings.arguments is AuthConfirmArgument
                  ? settings.arguments as AuthConfirmArgument
                  : null,
            ),
          ),
        );
      case Routes.orderInfo:
        return MaterialPageRoute(
          builder: (_) => BlocProvider<OrderSingleBloc>(
            create: (_) => sl<OrderSingleBloc>(),
            child: OrderInfoPage(
              argument: settings.arguments is OrderInfoArgument
                  ? settings.arguments as OrderInfoArgument
                  : null,
            ),
          ),
        );
      case Routes.main:
        return MaterialPageRoute(builder: (_) => const MainPage());
      case Routes.calculationInfo:
        return MaterialPageRoute(
          builder: (_) => BlocProvider<CalculatorInfoBloc>(
            create: (context) => sl<CalculatorInfoBloc>(),
            child: CalculatorInfoPage(
              argument: settings.arguments is CalculatorInfoArgument
                  ? settings.arguments as CalculatorInfoArgument
                  : null,
            ),
          ),
        );
      case Routes.comment:
        return MaterialPageRoute(
          builder: (_) => BlocProvider<CommentBloc>(
            create: (context) => sl<CommentBloc>(),
            child: CommentPage(
              argument: settings.arguments is CommentArgument
                  ? settings.arguments as CommentArgument
                  : null,
            ),
          ),
        );
      case Routes.notification:
        return MaterialPageRoute(
          builder: (_) => BlocProvider<NotificationBloc>(
            create: (context) => sl<NotificationBloc>(),
            child: const NotificationPage(),
          ),
        );
      case Routes.image:
        return MaterialPageRoute(
          builder: (_) => ImagePage(
            argument: settings.arguments is ImagePageArgument
                ? settings.arguments as ImagePageArgument
                : null,
          ),
        );
      default:
        return buildPageWithNoTransition(child: const InitialPage());
    }
  }

  static Route<dynamic> onShellGenerateRoute(RouteSettings settings) {
    if (kDebugMode) {
      print("route shell :  ${settings.name}");
    }
    switch (settings.name) {
      case Routes.initial:
        return buildPageWithDefaultTransition(child: const HomePage());
      case Routes.home:
        return buildPageWithDefaultTransition(child: const HomePage());
      case Routes.profile:
        return buildPageWithDefaultTransition(child: const ProfilePage());
      default:
        return buildPageWithDefaultTransition(child: const HomePage());
    }
  }
}

PageRouteBuilder buildPageWithDefaultTransition<T>({required Widget child}) {
  return PageRouteBuilder<T>(
    pageBuilder: (_, __, ___) => child,
    transitionsBuilder: (context, animation, secondaryAnimation, child) =>
        FadeTransition(opacity: animation, child: child),
  );
}

PageRouteBuilder buildPageWithNoTransition<T>({required Widget child}) {
  return PageRouteBuilder<T>(
    reverseTransitionDuration: Duration.zero,
    transitionDuration: Duration.zero,
    pageBuilder: (_, __, ___) => child,
    transitionsBuilder: (context, animation, secondaryAnimation, child) =>
        FadeTransition(opacity: animation, child: child),
  );
}
