import 'package:flutter/material.dart';

import 'package:go_router/go_router.dart';

enum Pages {
  root,
  sfactor,
  login,
  methods,
  verify,
  signup,
  signupConfirm,
  settings,
  dashboard,
  profile,
  recipients,
  users,
  roles,
  permissions,
  invitations,
}

String routerPath(String page) {
  return '/$page';
}

String routerPage(Pages page) {
  return page == Pages.root ? '/' : routerPath(page.name); 
}

String _pagePath(Pages page, {String? objectRef}) => _pagesPath([page], objectRef: objectRef);

String _pagesPath(List<Pages> pages, {String? objectRef}) {
  final path = pages.map(routerPage).join();
  return objectRef != null ? '$path/$objectRef' : path;
}

void navigateAndReplace(BuildContext context, Pages page, {String? objectRef, Object? extra}) {
  context.go(_pagePath(page, objectRef: objectRef), extra: extra);
}

void navigate(BuildContext context, Pages page, {String? objectRef, Object? extra}) {
  navigatePages(context, [page], objectRef: objectRef, extra: extra);
}

void navigatePages(BuildContext context, List<Pages> pages, {String? objectRef, Object? extra}) {
  context.push(_pagesPath(pages, objectRef: objectRef), extra: extra);
}

void navigateNamed(BuildContext context, Pages page, {String? objectRef, Object? extra}) {
  context.pushNamed(page.name, extra: extra);
}


void navigateNamedAndReplace(BuildContext context, Pages page, {String? objectRef, Object? extra}) {
  context.replaceNamed(page.name, extra: extra);
}

void navigateNext(BuildContext context, Pages page, {Object? extra}) {
  WidgetsBinding.instance.addPostFrameCallback((_) => navigate(context, page, extra: extra));
}
