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

import 'classify_photo_platform_interface.dart';

/// An implementation of [ClassifyPhotoPlatform] that uses method channels.
class MethodChannelClassifyPhoto extends ClassifyPhotoPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('classify_photo');

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<List<Map<String, dynamic>>?> getPhoto() async {
    try {
      print('Flutter: 调用 getPhoto 方法');
      final List<dynamic>? result = await methodChannel.invokeMethod<List<dynamic>>(
        'getPhoto',
      );

      print('Flutter: 收到原生返回结果: $result');

      if (result == null) {
        print('Flutter: 结果为空');
        return null;
      }

      return result.map((group) => Map<String, dynamic>.from(group)).toList();
    } on PlatformException catch (e) {
      print('Flutter: Platform Exception: ${e.message}');
      rethrow;
    } catch (e) {
      print('Flutter: Error: $e');
      rethrow;
    }
  }
}
