import 'dart:async';

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

typedef void MofunVideoViewCreatedCallback(MofunVideoViewController controller);

const _CHANNEL = 'plugins.shuangyunbang.mofun/mofunvideoview';

class MofunVideoView extends StatefulWidget {
  final MofunVideoViewCreatedCallback onMofunVideoViewCreated;

  const MofunVideoView({
    Key key,
    this.onMofunVideoViewCreated,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _MofunVideoViewState();
  }

}

class _MofunVideoViewState extends State<MofunVideoView> {
  @override
  Widget build(BuildContext context) {
    if (defaultTargetPlatform == TargetPlatform.android) {
      return AndroidView(
        viewType: _CHANNEL,
        onPlatformViewCreated: _onPlatformViewCreated,
      );
    }else if (defaultTargetPlatform == TargetPlatform.iOS) {
      return UiKitView(
        viewType: _CHANNEL,
        onPlatformViewCreated: _onPlatformViewCreated,
      );
    }
    return Text(
        '$defaultTargetPlatform is not yet supported by the text_view plugin');
  }

  void _onPlatformViewCreated(int id) {
    if (widget.onMofunVideoViewCreated == null) {
      return;
    }
    widget.onMofunVideoViewCreated(new MofunVideoViewController._(id));
  }
}

class MofunVideoViewController {
  MofunVideoViewController._(int id)
      : _channel = new MethodChannel('${_CHANNEL}_$id');

  final MethodChannel _channel;

  Future<void> setText(String text) async {
    assert(text != null);
    return _channel.invokeMethod('setText', text);
  }
}