import 'package:flutter/material.dart';

/// 全局导航服务
/// 用于在非Widget上下文中进行页面导航
class NavigationService {
  static final NavigationService _instance = NavigationService._internal();
  
  factory NavigationService() => _instance;
  
  NavigationService._internal();
  
  static NavigationService get instance => _instance;
  
  final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
  
  /// 获取当前上下文
  BuildContext? get context => navigatorKey.currentContext;
  
  /// 获取Navigator
  NavigatorState? get navigator => navigatorKey.currentState;
  
  /// 导航到指定路由
  Future<T?>? navigateTo<T>(String routeName, {Object? arguments}) {
    return navigator?.pushNamed<T>(routeName, arguments: arguments);
  }
  
  /// 替换当前路由
  Future<T?>? replaceWith<T extends Object?>(String routeName, {Object? arguments}) {
    return navigator?.pushReplacementNamed<T, T>(routeName, arguments: arguments);
  }
  
  /// 导航到指定路由并清除所有历史
  Future<T?>? navigateToAndClearStack<T extends Object?>(String routeName, {Object? arguments}) {
    return navigator?.pushNamedAndRemoveUntil<T>(
      routeName,
      (route) => false,
      arguments: arguments,
    );
  }
  
  /// 返回上一页
  void goBack<T>([T? result]) {
    if (navigator?.canPop() ?? false) {
      navigator?.pop<T>(result);
    }
  }
  
  /// 返回到指定路由
  void popUntil(String routeName) {
    navigator?.popUntil(ModalRoute.withName(routeName));
  }
  
  /// 显示SnackBar
  void showSnackBar(String message, {
    Duration duration = const Duration(seconds: 2),
    SnackBarAction? action,
  }) {
    final scaffoldMessenger = ScaffoldMessenger.of(context!);
    // 清除之前的所有SnackBar
    scaffoldMessenger.clearSnackBars();
    scaffoldMessenger.showSnackBar(
      SnackBar(
        content: Text(message),
        duration: duration,
        action: action,
        behavior: SnackBarBehavior.floating,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
        ),
      ),
    );
  }
  
  /// 显示错误SnackBar
  void showErrorSnackBar(String message) {
    final scaffoldMessenger = ScaffoldMessenger.of(context!);
    // 清除之前的所有SnackBar
    scaffoldMessenger.clearSnackBars();
    scaffoldMessenger.showSnackBar(
      SnackBar(
        content: Text(message),
        backgroundColor: Colors.red,
        duration: const Duration(seconds: 2),
        behavior: SnackBarBehavior.floating,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
        ),
      ),
    );
  }
  
  /// 显示成功SnackBar
  void showSuccessSnackBar(String message) {
    final scaffoldMessenger = ScaffoldMessenger.of(context!);
    // 清除之前的所有SnackBar
    scaffoldMessenger.clearSnackBars();
    scaffoldMessenger.showSnackBar(
      SnackBar(
        content: Text(message),
        backgroundColor: Colors.green,
        duration: const Duration(seconds: 2),
        behavior: SnackBarBehavior.floating,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
        ),
      ),
    );
  }
  
  /// 显示对话框
  Future<T?> showDialogWidget<T>(Widget dialog) {
    return showDialog<T>(
      context: context!,
      builder: (context) => dialog,
    );
  }
  
  /// 显示底部弹窗
  Future<T?> showBottomSheetWidget<T>(Widget bottomSheet) {
    return showModalBottomSheet<T>(
      context: context!,
      builder: (context) => bottomSheet,
    );
  }
}
