import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';

/// 平台（iOS/Android）主动下发路由时，通过此 Channel 传递给 Flutter。
///
/// iOS 侧调用方式：
/// ```objc
/// [engine.navigationChannel invokeMethod:@"pushRouteInformation"
///     arguments:@{@"location": @"/expense/apply"}];
/// ```
///
/// 或者通过自定义 channel：
/// ```objc
/// FlutterMethodChannel *ch = [FlutterMethodChannel
///     methodChannelWithName:@"com.tboss.oa/navigation"
///            binaryMessenger:engine.binaryMessenger];
/// [ch invokeMethod:@"navigateTo" arguments:@"/expense/apply"];
/// ```
class NavigationChannel {
  static const _channel = MethodChannel('com.tboss.oa/navigation');

  /// 开始监听平台下发的导航指令。
  ///
  /// [router] 必须是已初始化的 GoRouter 实例。
  static void startListening(GoRouter router) {
    _channel.setMethodCallHandler((call) async {
      switch (call.method) {
        case 'navigateTo':
          final route = call.arguments as String?;
          if (route != null && route.isNotEmpty) {
            router.go(route);
          }
          break;
        case 'pushRoute':
          final route = call.arguments as String?;
          if (route != null && route.isNotEmpty) {
            router.push(route);
          }
          break;
        default:
          break;
      }
    });
  }
}
