import 'dart:async';

import 'package:flutter/services.dart';

class DubbingLib {

  static final DubbingLib _instance = DubbingLib();

  static DubbingLib get instance => _instance;

  static const MethodChannel _channel =
      const MethodChannel('dubbing_lib');

  StreamController _onProgressChange;
  StreamController _onRecordProgressChange;
  StreamController _onDecodeResultChange;
  StreamController _onEvaluatorResultChange;

  Stream<int> get progressChange => _onProgressChange.stream;
  Stream<int> get recordProgressChange => _onRecordProgressChange.stream;
  Stream<Map> get decodeResultChange => _onDecodeResultChange.stream;
  Stream<Map> get evaluatorResultChange => _onEvaluatorResultChange.stream;

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  Future<void> init() {
    return _channel.invokeMethod("initSpeechSdk");
  }

  Future<void> setExtraFullScreen() {
    //todo 暂时视频播放时全屏StatusBar bug
    return _channel.invokeMethod("setExtraFullScreen");
  }

  Future<Map<dynamic, dynamic>> findIsExistCacheVideo(String videoId) {
    return _channel.invokeMethod<Map<dynamic, dynamic>>("findIsExistCacheVideo", {"videoId": videoId});
  }

  Future<String> downLoadVideo(String videoUrl) {
    return _channel.invokeMethod("downLoadVideo", {"url": videoUrl});
  }

  Future<void> pauseRecordAudio() {
    return _channel.invokeMethod("pauseRecordAudio");
  }

  /// 返回录音文件地址
  Future<String> startRecord(int index, int duration, String fileName, String content) {
    return _channel.invokeMethod("startRecord", {"duration": duration, "fileName": fileName, "index": index, "content": content});
  }

  Future<void> playRecordAudio(String filePath) {
    return _channel.invokeMethod("playRecordAudio", {"fileName": filePath});
  }

  /// 返回合成视频文件地址
  Future<String> startMixinAudio(String videoId, String videoUrl, List<int> endTimeList,
      List<String> decodeAudioPathList, List<int> durationList, String localVideoPath) {
    return _channel.invokeMethod("startMixinAudio", {"videoId": videoId, "bgmUrl": videoUrl,
      "endTimeList": endTimeList, "audioDecodePaths": decodeAudioPathList, "durationList": durationList, "videoPath": localVideoPath});
  }

  Future<void> cleanAudioData(String videoId) {
    return _channel.invokeMethod("cleanAudioData", {"videoId": videoId});
  }


  /// 初始化listener 进入录音页面时调用
  void initListener() {
    _onProgressChange = new StreamController<int>.broadcast();
    _onRecordProgressChange = new StreamController<int>.broadcast();
    _onDecodeResultChange = new StreamController<Map>.broadcast();
    _onEvaluatorResultChange = new StreamController<Map>.broadcast();
    _channel.setMethodCallHandler(_onMethodCallHandler);
  }

  /// 1,下载视频进度 2.录音进度 3.解码录音回调 4.语音评测结果
  Future _onMethodCallHandler(MethodCall call) {
    switch (call.method) {
      case "downloadUpdate":
        _onProgressChange.add(call.arguments['progress']);
        break;
      case "recordProgress":
        _onRecordProgressChange.add(call.arguments['progress']);
        break;
      case "decodeResult":
        _onDecodeResultChange.add(call.arguments);
        break;
      case "evaluatorResult":
        _onEvaluatorResultChange.add(call.arguments);
        break;
    }
    return null;
  }

  /// 关闭stream
  void closeStream() {
    _channel.setMethodCallHandler(null);
    _onProgressChange.close();
    _onRecordProgressChange.close();
    _onDecodeResultChange.close();
    _onEvaluatorResultChange.close();
  }
}
