import 'dart:io';

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import 'package:grocery_app/src/data/allProduct_model.dart';
import 'package:grocery_app/src/data/myOrder.dart';
import 'package:grocery_app/src/ui/bottomnavigation/bottom_bar_widget.dart';
import 'package:grocery_app/src/ui/card_checkout/card_checkout_screen.dart';
import 'package:grocery_app/src/ui/coupons/coupons_screen.dart';
import 'package:grocery_app/src/ui/entername/enter_fullname_screen.dart';
import 'package:grocery_app/src/ui/fruitvegidetail/fruit_veggie_detail.dart';
import 'package:grocery_app/src/ui/login/login_screen.dart';
import 'package:grocery_app/src/ui/login/signup_screen.dart';
import 'package:grocery_app/src/ui/map/add_locations.dart';
import 'package:grocery_app/src/ui/myOrder/OrderDetailsScreen.dart';
import 'package:grocery_app/src/ui/myOrder/my_order.dart';
import 'package:grocery_app/src/ui/onboarding/on_boarding_screen.dart';
import 'package:grocery_app/src/ui/otp/login_otp.dart';
import 'package:grocery_app/src/ui/otp/otp_screen.dart';
import 'package:grocery_app/src/ui/payment/payment_failure_screen.dart';
import 'package:grocery_app/src/ui/payment/payment_success_screen.dart';
import 'package:grocery_app/src/ui/productdetails/product_details.dart';
import 'package:grocery_app/src/ui/splash/splash_screen.dart';
import 'package:grocery_app/src/ui/static_page/privacyandpolicy.dart';
import 'package:grocery_app/src/ui/static_page/tersandconditions.dart';
import 'package:grocery_app/utils/constants/globle_variable.dart';

/// Route names as constants
class MyRoutes {
  static GoRouter router = GoRouter(
      navigatorKey: GlobalVariable.globalScaffoldKey,
      initialLocation: SPLASH,
      routes: [
        animatedGoRoute(
          path: SPLASH,
          name: SPLASH,
          pageBuilder: (context, state) => const SplashScreen(),
        ),

        animatedGoRoute(
          path: ONBOARDING,
          name: ONBOARDING,
          pageBuilder: (context, state) => const OnBoardingScreen(),
        ),

        animatedGoRoute(
          path: LOGIN,
          name: LOGIN,
          pageBuilder: (context, state) => LoginScreen(),
        ),
        animatedGoRoute(
          path: SIGNUP,
          name: SIGNUP,
          pageBuilder: (context, state) => SingnuScreen(),
        ),
        animatedGoRoute(
          path: LOGINOTPSCREEN,
          name: LOGINOTPSCREEN,
          pageBuilder: (context, state) => const LoginOtpScreen(),
        ),

        animatedGoRoute(
          path: OTPSCREEN,
          name: OTPSCREEN,
          pageBuilder: (context, state) => const OtpScreen(),
        ),

        animatedGoRoute(
          path: FULLNAME,
          name: FULLNAME,
          pageBuilder: (context, state) => const EnterFullNameScreen(),
        ),

        animatedGoRoute(
          path: BOTTOMNAV,
          name: BOTTOMNAV,
          pageBuilder: (context, state) => const BottomBarWidget(),
        ),
        animatedGoRoute(
          path: FRUITVEGGIEDETAIL,
          name: FRUITVEGGIEDETAIL,
          pageBuilder: (context, state) => const FruitVeggieDetail(),
        ),

        animatedGoRoute(
            path: COUPONSSCREEN,
            name: COUPONSSCREEN,
            pageBuilder: (context, state) {
              final id = state.extra as String;
              return CouponsScreen(
                cartId: id,
              );
            }),
        animatedGoRoute(
          path: ADDRESSS,
          name: ADDRESSS,
          pageBuilder: (context, state) => AddLocationAddress(),
        ),

        animatedGoRoute(
          path: PRODUCTDETAILS,
          name: PRODUCTDETAILS,
          pageBuilder: (context, state) {
            // final id = state.extra as String; // Get the extra object
            // return ProductDetails(id: id);

            final args = state.extra as Map<String, dynamic>;

            return ProductDetails(
              id: args["id"],
              quantity: args["quantity"],
              price: args["price"],
            );
          },
        ),

        animatedGoRoute(
          path: TERMANDCONDITIONS,
          name: TERMANDCONDITIONS,
          pageBuilder: (context, state) => const TermsAndConditionsScreen(),
        ),

        animatedGoRoute(
          path: PRIVACY,
          name: PRIVACY,
          pageBuilder: (context, state) => const PrivacyPolicy(),
        ),

        animatedGoRoute(
          path: SUCCESSPAYMENT,
          name: SUCCESSPAYMENT,
          pageBuilder: (context, state) => PaymentSuccessScreen(),
        ),
        animatedGoRoute(
          path: PAYMENTFAILD,
          name: PAYMENTFAILD,
          pageBuilder: (context, state) => PaymentFailureScreen(),
        ),

        animatedGoRoute(
          path: MYORDER,
          name: MYORDER,
          pageBuilder: (context, state) => MyOrderScreen(),
        ),
        animatedGoRoute(
          path: ORDERDETAILS,
          name: ORDERDETAILS,
          pageBuilder: (context, state) {
            final order = state.extra as Datum; // Cast extra as Datum
            return OrderDetailsScreen(order: order);
          },
        ),

        //    animatedGoRoute(
        //     path: UPDATESTORE,
        //     name: UPDATESTORE,
        //      pageBuilder: (context, state) {
        //         final extra = state.extra as Map<String, dynamic>?;
        //         final String status = extra?['storeId'] ?? '';
        //         return UpdateStoreScreen(storeId: status);
        //       }

        //   ),

        //   animatedGoRoute(
        //     path: SUBMITSCREEN,
        //     name: SUBMITSCREEN,
        //     pageBuilder: (context, state) => const PinCreated(),
        //   ),
        //   animatedGoRoute(
        //     path: APPROVEDSTATUS,
        //     name: APPROVEDSTATUS,
        //     pageBuilder: (context, state) => const ApprovalScreen(),
        //   ),
        //   animatedGoRoute(
        //     path: SIGNUP,
        //     name: SIGNUP,
        //     pageBuilder: (context, state) => LoginHostScreen(),
        //   ),
        //   animatedGoRoute(
        //     path: DASHBOARDSCREEN,
        //     name: DASHBOARDSCREEN,
        //     pageBuilder: (context, state) => DashboardScree(),
        //   ),
        //   animatedGoRoute(
        //     path: CUSTOMERORDER,
        //     name: CUSTOMERORDER,
        //     pageBuilder: (context, state) {
        //       // Extract data from `state.extra`
        //       final Map<String, dynamic> orderDetails =
        //           state.extra as Map<String, dynamic>;

        //       return CustomerOrder(
        //         orderDetails:
        //             orderDetails, // Pass the data to the destination widget
        //       );
        //     },
        //   ),
        //   animatedGoRoute(
        //     path: PRODUCTFORMSCREEN,
        //     name: PRODUCTFORMSCREEN,
        //     pageBuilder: (context, state) => ProductFormScreen(),
        //   ),
        //   animatedGoRoute(
        //     path: PRODUCTDETAILS,
        //     name: PRODUCTDETAILS,
        //     pageBuilder: (context, state) {
        //       final Product product = state.extra as Product;
        //       return Productdetails(
        //         product: product, // Pass the `Product` object directly
        //       );
        //     },
        //   ),
        //   animatedGoRoute(
        //     path: TRANSACTIONHISTORY,
        //     name: TRANSACTIONHISTORY,
        //     pageBuilder: (context, state) => TransactionHistory(),
        //   ),
        //   animatedGoRoute(
        //     path: INSIGHTSHISTORY,
        //     name: INSIGHTSHISTORY,
        //     pageBuilder: (context, state) => InsightsHistory(),
        //   ),
        //   animatedGoRoute(
        //     path: DETAILSBUSSINESS,
        //     name: DETAILSBUSSINESS,
        //     pageBuilder: (context, state) => DetailsBussiness(),
        //   ),
        //   animatedGoRoute(
        //     path: STOREMANAGEMENT,
        //     name: STOREMANAGEMENT,
        //     pageBuilder: (context, state) => StoreManagement(),
        //   ),
        //   animatedGoRoute(
        //     path: SETTING,
        //     name: SETTING,
        //     pageBuilder: (context, state) => Settings(),
        //   ),
        //   animatedGoRoute(
        //     path: FORGETPASSWORD,
        //     name: FORGETPASSWORD,
        //     pageBuilder: (context, state) => ForgetPassword(),
        //   ),
        //   animatedGoRoute(
        //     path: VERIFYPASSWORD,
        //     name: VERIFYPASSWORD,
        //     pageBuilder: (context, state) => VerifyOtpForgetPassword(),
        //   ),
        //   animatedGoRoute(
        //     path: FORGETNEWPASSWORD,
        //     name: FORGETNEWPASSWORD,
        //     pageBuilder: (context, state) => ForgetNewPassword(),
        //   ),
        // ],
      ]);

  /// Route constants
  static const SPLASH = "/";
  static const FULLNAME = "/fullname";
  static const BOTTOMNAV = "/bottomnav";
  static const HOME = "/home";
  static const FRUITVEGGIEDETAIL = "/FruitVeggieDetail";
  static const PRODUCTDETAILS = "/productdetails";
  static const LOGINOTPSCREEN = "/loginotpscreen";
  static const COUPONSSCREEN = "/CouponsScreen";
  static const ADDRESSS = "/address";
  static const LOGIN = "/login";
  static const SIGNUP = "/signup";
  static const ONBOARDING = "/onboarding";
  static const SELECTPAYMENTSCREEN = "/paymnetscreen";
  static const SUCCESSPAYMENT = "/paymentSuccessScreen";
  static const PAYMENTFAILD = "/paymentfailed";
  static const MYORDER = "/myorder";

  static const ORDERDETAILS = "/OrderDetailsScreen";

  static const TERMANDCONDITIONS = "/termsandcondition";

  static const PRIVACY = "/privacyandpolicy";

  // static const SETUPBUSSINESS = "/setupbussiness";
  // static const CREATESTORE = "/createStore";
  // static const SUBMITSCREEN = "/submitscreen";
  // static const APPROVEDSTATUS = "/approvedstatus";

  // static const DASHBOARDSCREEN = "/dashboardscreen";
  // static const CUSTOMERORDER = "/customerorder";

  // static const PRODUCTFORMSCREEN = "/productformscreen";

  // static const TRANSACTIONHISTORY = "/transationhistory";

  // static const INSIGHTSHISTORY = "/insightshistory";

  // static const DETAILSBUSSINESS = "/detailsbussiness";

  // static const STOREMANAGEMENT = "/storemanagement";

  // static const SETTING = "/settings";

  // static const FORGETPASSWORD = "/forgetpassword";
  // static const VERIFYPASSWORD = "/verifypassword";

  // static const FORGETNEWPASSWORD = "/forgetnewpassword";

  // static const UPDATESTORE = "/updatestore";
  static const OTPSCREEN = "/otpscreen";
}

GoRoute animatedGoRoute({
  required String path,
  required String name,
  ExitCallback? onExitPage,
  required Widget Function(BuildContext, GoRouterState) pageBuilder,
}) {
  return GoRoute(
    path: path,
    name: name,
    onExit: onExitPage,
    pageBuilder: (context, state) => CustomTransitionPage<void>(
      key: state.pageKey,
      transitionDuration: const Duration(milliseconds: 400),
      child: pageBuilder(context, state),
      transitionsBuilder: (_, animation, __, child) {
        return FadeTransition(
          opacity: animation,
          child: child,
        );
      },
    ),
  );
}

class CustomSlideTransition extends CustomTransitionPage<void> {
  CustomSlideTransition({super.key, required super.child})
      : super(
          transitionDuration: const Duration(milliseconds: 250),
          transitionsBuilder: (_, animation, __, child) {
            return SlideTransition(
              position: animation.drive(
                Tween(begin: const Offset(1.5, 0), end: Offset.zero).chain(
                  CurveTween(curve: Curves.bounceIn),
                ),
              ),
              child: child,
            );
          },
        );
}
