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

class PlayerVersionObserver {
  final MethodChannel _pluginChannel = const MethodChannel('tech.nut/plugin');
  final _versionStreamController = StreamController<String>();

  Stream<String> get versionStream => _versionStreamController.stream.asBroadcastStream();

  PlayerVersionObserver() {
    _pluginChannel.setMethodCallHandler(_pluginVersionHandler);
    _pluginChannel.invokeMethod("getPlayerVersion");
  }

  Future<dynamic> _pluginVersionHandler(MethodCall call) async {
    switch (call.method) {
      case 'pluginVersion':
        final version = call.arguments['version'];
        if (version != null) {
          _versionStreamController.add(version);
        }
        break;
      default:
        break;
    }
  }
}