import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart' as material;

///免context路由操作
class WNavUtil {
  WNavUtil._();
  static WNavUtil? _instance;
  static WNavUtil? get instance {
    _instance ??= WNavUtil._();
    return _instance;
  }

  ///挂载在MateralApp上的路由键
  final navigatorKey = GlobalKey<NavigatorState>();

  ///可通过它进行免context路由跳转
  // NavigatorState get currentState => navigatorKey.currentState;
  NavigatorState? get state => navigatorKey.currentState;

  ///挂载在MateralApp上的context
  BuildContext? get context => navigatorKey.currentContext;

  ///免[context] [showModalBottomSheet]
  Future<T?> showModalBottomSheet<T>({
    required WidgetBuilder builder,
    Color? backgroundColor,
    double? elevation,
    ShapeBorder? shape,
    Clip? clipBehavior,
    Color? barrierColor,
    bool isScrollControlled = false,
    bool useRootNavigator = false,
    bool isDismissible = true,
    bool enableDrag = true,
  }) =>
      material.showModalBottomSheet<T>(
        context: context!,
        builder: builder,
        backgroundColor: backgroundColor,
        elevation: elevation,
        shape: shape,
        clipBehavior: clipBehavior,
        barrierColor: barrierColor,
        isScrollControlled: isScrollControlled,
        useRootNavigator: useRootNavigator,
        isDismissible: isDismissible,
        enableDrag: enableDrag,
      );

  bool canPop() => state!.canPop();

  void finalizeRoute(Route route) {
    return state!.finalizeRoute(route);
  }


  Future<bool> maybePop<T extends Object>([T? result]) => state!.maybePop();

  bool get mounted => state!.mounted;

  OverlayState? get overlay => state!.overlay;

  void pop<T extends Object?>([T? result]) {
    if (canPop()) return state!.pop(result);
  }

  Future<T?> popAndPushNamed<T extends Object, TO extends Object>(
    String routeName, {
    TO? result,
    Object? arguments,
  }) {
    return state!.popAndPushNamed(
      routeName,
      result: result,
      arguments: arguments,
    );
  }

  void popUntil(RoutePredicate predicate) => state!.popUntil(predicate);

  Future<T?> push<T extends Object?>(Route<T> route) => state!.push<T>(route);

  Future<T?> pushAndRemoveUntil<T extends Object>(
    Route<T> newRoute,
    RoutePredicate predicate,
  ) {
    return state!.pushAndRemoveUntil<T>(newRoute, predicate);
  }

  Future<T?> pushNamed<T extends Object>(String routeName,
      {Object? arguments}) {
    return state!.pushNamed<T>(
      routeName,
      arguments: arguments,
    );
  }

  Future<T?> pushNamedAndRemoveUntil<T extends Object>(
    String newRouteName,
    RoutePredicate predicate, {
    Object? arguments,
  }) {
    return state!.pushNamedAndRemoveUntil<T>(
      newRouteName,
      predicate,
      arguments: arguments,
    );
  }

  Future<T?> pushReplacement<T extends Object, TO extends Object>(
    Route<T> newRoute, {
    TO? result,
  }) {
    return state!.pushReplacement(
      newRoute,
      result: result,
    );
  }

  Future<T?> pushReplacementNamed<T extends Object, TO extends Object>(
    String routeName, {
    TO? result,
    Object? arguments,
  }) {
    return state!.pushReplacementNamed(
      routeName,
      result: result,
      arguments: arguments,
    );
  }

  void removeRoute(Route route) => state!.removeRoute(route);

  void removeRouteBelow(Route anchorRoute) =>
      state!.removeRouteBelow(anchorRoute);

  void replace<T extends Object>({
    required Route oldRoute,
    required Route<T> newRoute,
  }) {
    return state!.replace(
      oldRoute: oldRoute,
      newRoute: newRoute,
    );
  }

  void replaceRouteBelow<T extends Object>({
    required Route anchorRoute,
    required Route<T> newRoute,
  }) {
    return state!.replaceRouteBelow(
      anchorRoute: anchorRoute,
      newRoute: newRoute,
    );
  }

  bool get userGestureInProgress => state!.userGestureInProgress;

  ValueNotifier<bool> get userGestureInProgressNotifier =>
      state!.userGestureInProgressNotifier;
}
