import 'package:flutter/material.dart';
import 'package:flutter/services.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/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";
}

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) {
      return pageRouteCustom(const OtpVerificationView(),
          nameRoute: Routes.verificationRoute);
    } 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 {
      return unDefinedRoute();
    }
    // switch (routeSettings.name) {
    //   case Routes.splashRoute:
    //     return pageRouteCustom(const SplashView(),
    //         nameRoute: Routes.splashRoute);
    //   case Routes.loginRoute:
    //     return pageRouteCustom(LoginView(), nameRoute: Routes.loginRoute);
    //   case Routes.registerRoute:
    //     return pageRouteCustom(const RegisterView(),
    //         nameRoute: Routes.registerRoute);
    //   case Routes.verificationRoute:
    //     return pageRouteCustom(const OtpVerificationView(),
    //         nameRoute: Routes.verificationRoute);
    //   case Routes.forgotPasswordRoute:
    //     return pageRouteCustom(const ForgotPasswordView(),
    //         nameRoute: Routes.forgotPasswordRoute);
    //   case Routes.resetPasswordRoute:
    //     return pageRouteCustom(const ResetPasswordView(),
    //         nameRoute: Routes.resetPasswordRoute);
    //   case Routes.homeRoute:
    //     return pageRouteCustom(const HomeView(), nameRoute: Routes.homeRoute);
    //   case Routes.profileRoute:
    //     return pageRouteCustom(const ProfileView(),
    //         nameRoute: Routes.profileRoute);
    //   case Routes.changePasswordRoute:
    //     return pageRouteCustom(const ChangePasswordView(),
    //         nameRoute: Routes.changePasswordRoute);
    //   case Routes.editProfileRoute:
    //     return pageRouteCustom(const EditProfileView(),
    //         nameRoute: Routes.editProfileRoute);
    //   case Routes.reimburseRoute:
    //     return pageRouteCustom(const ReimbursementView(),
    //         nameRoute: Routes.reimburseRoute);
    //   case Routes.historyRoute:
    //     return pageRouteCustom(const HistoryView(),
    //         nameRoute: Routes.historyRoute);
    //   default:
    //     return unDefinedRoute();
    // }
  }

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

  static PageRouteBuilder<dynamic> pageRouteCustom(Widget target,
      {required String nameRoute, String title = ""}) {
    // setTitle(title);
    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"),
        ),
      ),
    );
  }
}
