import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tour_travel_agr/helper/argument_route/verification_otp.dart';
import 'package:tour_travel_agr/main.dart';
import 'package:tour_travel_agr/page/history/history.dart';
import 'package:tour_travel_agr/page/profile/change_password/change_password.dart';
import 'package:tour_travel_agr/page/forgot_password/forgot_password.dart';
import 'package:tour_travel_agr/page/login/login.dart';
import 'package:tour_travel_agr/page/otp_verification/otp_verification.dart';
import 'package:tour_travel_agr/page/home/home.dart';
import 'package:tour_travel_agr/page/profile/edit_profile/edit_profile.dart';
import 'package:tour_travel_agr/page/profile/profile.dart';
import 'package:tour_travel_agr/page/register/register.dart';
import 'package:tour_travel_agr/page/reimbursement/reimbursement.dart';
import 'package:tour_travel_agr/page/reset_password/reset_password.dart';
import 'package:tour_travel_agr/page/reset_password/reset_success.dart';
import 'package:tour_travel_agr/page/splash/splash.dart';

class Routes {
  static const String splashRoute = "/splash";
  static const String loginRoute = "/login";
  static const String registerRoute = "/register";
  static const String verificationRoute = "/verification";
  static const String forgotPasswordRoute = "/forgot-password";
  static const String resetPasswordRoute = "/reset-password";
  static const String changePasswordRoute = "/change-password";
  static const String homeRoute = "/home";
  static const String profileRoute = "/profile";
  static const String editProfileRoute = "/edit-profile";
  static const String reimburseRoute = "/reimbursement";
  static const String historyRoute = "/history";
  static const String resetPasswordSuccess = "/reset-success";
}

class RouteGenerator {
  static Route<dynamic> getRoute(RouteSettings routeSettings) {
    // print(routeSettings.name);
    if (routeSettings.name == Routes.splashRoute) {
      return pageRouteCustom(const SplashView(), nameRoute: Routes.splashRoute);
    } else if (routeSettings.name == Routes.loginRoute) {
      return pageRouteCustom(LoginView(), nameRoute: Routes.loginRoute);
    } else if (routeSettings.name == Routes.registerRoute) {
      return pageRouteCustom(const RegisterView(),
          nameRoute: Routes.registerRoute);
    } else if (routeSettings.name == Routes.verificationRoute) {
      VerificationArguments args =
          routeSettings.arguments as VerificationArguments;
      return pageRouteCustom(
        OtpVerificationView(
          id: args.id,
          phone: args.phone,
        ),
        nameRoute: Routes.verificationRoute,
        routeSettings: routeSettings,
      );
    } else if (routeSettings.name == Routes.forgotPasswordRoute) {
      return pageRouteCustom(const ForgotPasswordView(),
          nameRoute: Routes.forgotPasswordRoute);
    } else if (routeSettings.name!.contains(Routes.resetPasswordRoute)) {
      List<String> splitPath =
          routeSettings.name!.split("${Routes.resetPasswordRoute}/");
      if (splitPath.length == 2) {
        return pageRouteCustom(
            ResetPasswordView(
              idPath: splitPath[1],
            ),
            nameRoute: routeSettings.name!);
      } else {
        return unDefinedRoute();
      }
    } else if (routeSettings.name == Routes.homeRoute) {
      return pageRouteCustom(const HomeView(), nameRoute: Routes.homeRoute);
    } else if (routeSettings.name == Routes.profileRoute) {
      return pageRouteCustom(const ProfileView(),
          nameRoute: Routes.profileRoute);
    } else if (routeSettings.name == Routes.changePasswordRoute) {
      return pageRouteCustom(const ChangePasswordView(),
          nameRoute: Routes.changePasswordRoute);
    } else if (routeSettings.name == Routes.editProfileRoute) {
      return pageRouteCustom(const EditProfileView(),
          nameRoute: Routes.editProfileRoute);
    } else if (routeSettings.name == Routes.reimburseRoute) {
      return pageRouteCustom(const ReimbursementView(),
          nameRoute: Routes.reimburseRoute);
    } else if (routeSettings.name == Routes.historyRoute) {
      return pageRouteCustom(const HistoryView(),
          nameRoute: Routes.historyRoute);
    } else if (routeSettings.name == Routes.resetPasswordSuccess) {
      return pageRouteCustom(const ResetPasswrodSuccessView(),
          nameRoute: Routes.resetPasswordSuccess);
    } else {
      return unDefinedRoute();
    }
  }

  static void setTitle(String title) {
    SystemChrome.setApplicationSwitcherDescription(
      ApplicationSwitcherDescription(label: "$title | $titleApp"),
    );
  }

  static PageRouteBuilder<dynamic> pageRouteCustom(
    Widget target, {
    required String nameRoute,
    String title = "",
    RouteSettings? routeSettings,
  }) {
    if (nameRoute == Routes.verificationRoute) {
      VerificationArguments args =
          routeSettings!.arguments as VerificationArguments;

      return PageRouteBuilder(
        pageBuilder: (context, a, b) => OtpVerificationView(
          id: args.id,
          phone: args.phone,
        ),
        transitionDuration: Duration.zero,
        reverseTransitionDuration: Duration.zero,
        settings: RouteSettings(name: nameRoute.replaceFirst("/", "")),
      );
    }
    return PageRouteBuilder(
      pageBuilder: (context, a, b) => target,
      transitionDuration: Duration.zero,
      reverseTransitionDuration: Duration.zero,
      settings: RouteSettings(name: nameRoute.replaceFirst("/", "")),
    );
  }

  static Route<dynamic> unDefinedRoute() {
    return MaterialPageRoute(
      builder: (_) => Scaffold(
        appBar: AppBar(
          title: const Text("No Route Found"),
        ),
        body: const Center(
          child: Text("No Route Found"),
        ),
      ),
    );
  }
}
