import 'package:flutter/material.dart';
import 'package:shared/utils/log_utils.dart';

class MyNavigatorObserver extends NavigatorObserver {

  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPush(route, previousRoute);

    Log.d('NavigationRecord{route: ${getRouteName(route)}, action: didPush ,'
        ' previousRoute: ${getPreviousRouteName(previousRoute)}, '
        'parameters: ${getParameters(route)}}');
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPop(route, previousRoute);
    Log.d('NavigationRecord{route: ${getRouteName(route)}, action: didPush ,'
        ' previousRoute: ${getPreviousRouteName(previousRoute)}, '
        'parameters: ${getParameters(route)}}');
  }

  @override
  void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didRemove(route, previousRoute);
    Log.d('NavigationRecord{route: ${getRouteName(route)}, action: didPush ,'
        ' previousRoute: ${getPreviousRouteName(previousRoute)}, '
        'parameters: ${getParameters(route)}}');
  }

  @override
  void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    Log.d('NavigationRecord{route: ${getRouteName(newRoute)}, action: didPush ,'
        ' previousRoute: ${getPreviousRouteName(oldRoute)}, '
        'parameters: ${getParameters(newRoute)}}');
  }

  /// 获取路由参数
  static Map<String, dynamic> getParameters(Route<dynamic>? route) {
    final params = <String, dynamic>{};
    if (route?.settings.arguments != null) {
      params['arguments'] = route!.settings.arguments;
    }
    if (route?.settings.name != null) {
      params['name'] = route!.settings.name;
    }
    return params;
  }

  /// 获取路由名称
  static String getRouteName(Route<dynamic>? route) {
    return route?.settings.name ?? 'Unknown route';
  }

  /// 获取前一个路由名称
  static String getPreviousRouteName(Route<dynamic>? previousRoute) {
    return previousRoute?.settings.name ?? 'Unknown previous route';
  }


}