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

import 'cx_1500n_plugin_platform_interface.dart';

/// An implementation of [Cx1500nPluginPlatform] that uses method channels.
class MethodChannelCx1500nPlugin extends Cx1500nPluginPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('cx_1500n_plugin');
  final _eventChannel = EventChannel('cx_1500n_plugin/connection');

  Stream<String>? _eventsStream;

  @override
  Stream<String> get events {
    _eventsStream ??=
        _eventChannel.receiveBroadcastStream().map((event) => event as String);
    return _eventsStream!;
  }

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

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

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

  @override
  Future<void> startInventory() async {
    await methodChannel.invokeMethod('startInventory');
  }

  @override
  Future<String?> stopInventory() async {
    return await methodChannel.invokeMethod<String>('stopInventory');
  }

  @override
  Future<void> setPower(int power) async {
    await methodChannel.invokeMethod('setPower', {"power": power});
  }

  @override
  Future<String> read(int membank) async {
    return await methodChannel.invokeMethod('read', {"membank": membank});
  }

  @override
  Future<void> startScan() async {
    await methodChannel.invokeMethod('startScan');
  }

  @override
  Future<void> stopScan() async {
    await methodChannel.invokeMethod('stopScan');
  }
}
