import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:latlong2/latlong.dart';
import 'package:niogu_ecommerce_v1/core/enums/action_type.dart';
import 'package:niogu_ecommerce_v1/core/widgets/custom_action_screen.dart';
import 'package:niogu_ecommerce_v1/features/account/presentation/screens/account_screen.dart';
import 'package:niogu_ecommerce_v1/features/account/presentation/screens/change_password_screen.dart';
import 'package:niogu_ecommerce_v1/features/account/presentation/screens/map_address_screen.dart';
import 'package:niogu_ecommerce_v1/features/account/presentation/screens/address_screen.dart';
import 'package:niogu_ecommerce_v1/features/account/presentation/screens/save_address_screen.dart';
import 'package:niogu_ecommerce_v1/features/auth/presentation/screens/login_screen.dart';
import 'package:niogu_ecommerce_v1/features/auth/presentation/screens/register_screen.dart';
import 'package:niogu_ecommerce_v1/features/cart/presentation/screens/cart_screen.dart';
import 'package:niogu_ecommerce_v1/features/checkout/presentation/screens/checkout_screen.dart';
import 'package:niogu_ecommerce_v1/features/checkout/presentation/screens/shipping_address_screen.dart';
import 'package:niogu_ecommerce_v1/features/favorite/presentation/screens/favorite_screen.dart';
import 'package:niogu_ecommerce_v1/features/home/domain/entities/home.dart';
import 'package:niogu_ecommerce_v1/features/home/presentation/screens/home_screen.dart';
import 'package:niogu_ecommerce_v1/features/home/presentation/screens/outlet_map_screen.dart';
import 'package:niogu_ecommerce_v1/features/home/presentation/screens/product_category_screen.dart';
import 'package:niogu_ecommerce_v1/features/home/presentation/screens/search_screen.dart';
import 'package:niogu_ecommerce_v1/features/order/domain/entities/order.dart';
import 'package:niogu_ecommerce_v1/features/order/presentation/screens/order_detail_screen.dart';
import 'package:niogu_ecommerce_v1/features/order/presentation/screens/order_screen.dart';
import 'package:niogu_ecommerce_v1/features/order/presentation/screens/rating_screen.dart';
import 'package:niogu_ecommerce_v1/features/product/domain/entities/product.dart';
import 'package:niogu_ecommerce_v1/features/product/presentation/screens/product_detail_screen.dart';
import 'package:niogu_ecommerce_v1/features/splash/presentation/screens/splash_screen.dart';
import 'package:niogu_ecommerce_v1/main_wrapper.dart';

final _rootNavigatorKey = GlobalKey<NavigatorState>();

class AppRoute {
  static final String splashScreen = 'splash';
  static final String loginScreen = 'login';
  static final String registerScreen = 'register';
  static final String homeScreen = 'home';
  static final String favoriteScreen = 'favorite';
  static final String cartScreen = 'cart';
  static final String orderScreen = 'order';
  static final String accountScreen = 'account';
  static final String searchScreen = 'search';
  static final String outletMapScreen = 'outletMap';
  static final String productCategoryScreen = 'productCategory';
  static final String productDetailScreen = 'productDetail';
  static final String checkoutScreen = 'checkout';
  static final String shippingAddressScreen = 'shippingAddress';
  static final String ratingScreen = 'rating';
  static final String orderDetailScreen = 'orderDetail';
  static final String customActionScreen = 'customAction';
  static final String changePasswordScreen = 'changePassword';
  static final String addressScreen = 'addresses';
  static final String mapAddressScreen = 'mapAddressScreen';
  static final String saveAddressScreen = 'saveAddressScreen';

  static final router = GoRouter(
    navigatorKey: _rootNavigatorKey,
    initialLocation: '/splash',
    routes: [
      GoRoute(
        name: splashScreen,
        path: '/splash',
        builder: (context, state) => const SplashScreen(),
      ),
      GoRoute(
        name: loginScreen,
        path: '/login',
        builder: (context, state) => const LoginScreen(),
      ),
      GoRoute(
        name: registerScreen,
        path: '/register',
        builder: (context, state) => const RegisterScreen(),
      ),
      StatefulShellRoute.indexedStack(
        builder: (context, state, navigationShell) {
          return MainWrapper(navigationShell: navigationShell);
        },
        branches: [
          StatefulShellBranch(
            routes: [
              GoRoute(
                name: homeScreen,
                path: '/home',
                builder: (context, state) => const HomeScreen(),
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                name: favoriteScreen,
                path: '/favorites',
                builder: (context, state) => const FavoriteScreen(),
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                name: cartScreen,
                path: '/carts',
                builder: (context, state) => const CartScreen(),
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                name: orderScreen,
                path: '/orders',
                builder: (context, state) => const OrderScreen(),
              ),
            ],
          ),
          StatefulShellBranch(
            routes: [
              GoRoute(
                name: accountScreen,
                path: '/account',
                builder: (context, state) => const AccountScreen(),
              ),
            ],
          ),
        ],
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: productDetailScreen,
        path: '/product-info',
        pageBuilder: (context, state) {
          final productDetail = state.extra as ProductDetail;
          return CustomTransitionPage(
            key: state.pageKey,
            child: ProductDetailScreen(productDetail: productDetail),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: searchScreen,
        path: '/search',
        pageBuilder: (context, state) {
          final categories = state.extra as List<CategoryItem>;
          return CustomTransitionPage(
            key: state.pageKey,
            child: SearchScreen(categories: categories),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: outletMapScreen,
        path: '/outlet-map',
        pageBuilder: (context, state) {
          final data = state.extra as Map<String, dynamic>;

          final userLocation = data['user_location'] as LatLng;

          final outlets = data['outlets'] as List<OtherOutlet>;

          return CustomTransitionPage(
            key: state.pageKey,
            child: OutletMapScreen(
              userLocation: userLocation,
              outlets: outlets,
            ),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: productCategoryScreen,
        path: '/product-categories',
        pageBuilder: (context, state) {
          final category = state.extra as CategoryItem;

          return CustomTransitionPage(
            key: state.pageKey,
            child: ProductCategoryScreen(category: category),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: checkoutScreen,
        path: '/checkout',
        pageBuilder: (context, state) {
          return CustomTransitionPage(
            key: state.pageKey,
            child: const CheckoutScreen(),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: shippingAddressScreen,
        path: '/shipping-address',
        pageBuilder: (context, state) {
          return CustomTransitionPage(
            key: state.pageKey,
            child: const ShippingAddressScreen(),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: ratingScreen,
        path: '/rating/:id',
        pageBuilder: (context, state) {
          final orderId = state.pathParameters['id'];
          final products = state.extra as List<ProductReviewItem>;

          return CustomTransitionPage(
            key: state.pageKey,
            child: RatingScreen(orderId: orderId!, products: products),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: orderDetailScreen,
        path: '/order-detail',
        pageBuilder: (context, state) {
          final orderInfo = state.extra as OrderInfo;

          return CustomTransitionPage(
            key: state.pageKey,
            child: OrderDetailScreen(orderInfo: orderInfo),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: customActionScreen,
        path: '/custom-action',
        pageBuilder: (context, state) {
          final data = state.extra as Map<String, dynamic>;

          final orderId = data['order_id'] as String?;

          final orderNumber = data['order_number'] as String;

          final type = data['type'] as ActionType;

          return CustomTransitionPage(
            key: state.pageKey,
            child: CustomActionScreen(
              orderId: orderId ?? '',
              orderNumber: orderNumber,
              type: type,
            ),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: changePasswordScreen,
        path: '/change-password',
        pageBuilder: (context, state) {
          return CustomTransitionPage(
            key: state.pageKey,
            child: const ChangePasswordScreen(),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: addressScreen,
        path: '/addresses',
        pageBuilder: (context, state) {
          return CustomTransitionPage(
            key: state.pageKey,
            child: const AddressScreen(),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: mapAddressScreen,
        path: '/map-address',
        pageBuilder: (context, state) {
          return CustomTransitionPage(
            key: state.pageKey,
            child: const MapAddressScreen(),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
      GoRoute(
        parentNavigatorKey: _rootNavigatorKey,
        name: saveAddressScreen,
        path: '/save-address',
        pageBuilder: (context, state) {
          final label = state.extra as String?;

          return CustomTransitionPage(
            key: state.pageKey,
            child: SaveAddressScreen(label: label),

            transitionsBuilder:
                (context, animation, secondaryAnimation, child) =>
                    FadeTransition(
                      opacity: CurveTween(
                        curve: Curves.easeIn,
                      ).animate(animation),
                      child: child,
                    ),
          );
        },
      ),
    ],
  );
}
