import 'package:fis_common/event/event_type.dart';
import 'package:fis_lib_media_rt/base/channel.dart';
import 'package:fis_lib_media_rt/interface/index.dart';
import 'package:tencent_trtc_cloud/trtc_cloud_def.dart';

import 'capturer/index.dart';
import 'client_getter_mixin.dart';

class TrtcPublishChannel extends BasePublishChannel
    with TrtcChannelMixin, TrtcClientGetterMixin {
  TrtcPublishChannel({required super.member});

  TRTCVideoEncParam? videoEncParam;

  @override
  void setAudioMute(bool isMute) async {
    await client.muteLocalAudio(isMute);
    super.setAudioMute(isMute);
  }

  @override
  void setVideoMute(bool isMute) async {
    await client.muteLocalVideo(isMute);
    super.setVideoMute(isMute);
  }

  @override
  void setVolume(int volume) {
    // TODO: implement setVolume
  }

  @override
  int get volume => 100;
}

class TrtcPlayChannel extends BasePlayChannel
    with TrtcChannelMixin, TrtcClientGetterMixin {
  TrtcPlayChannel({required super.member});
  int _volume = 100;

  @override
  void setAudioMute(bool isMute) async {
    await client.muteRemoteAudio(member.userId!, isMute);
    super.setAudioMute(isMute);
  }

  @override
  void setVideoMute(bool isMute) async {
    await client.muteRemoteVideoStream(member.userId!, isMute);
    super.setVideoMute(isMute);
  }

  @override
  Future<void> play() async {
    if (viewId < 0) {
      throw FRealTimeException(
        message:
            "PlayChannel-$id (userId: ${member.userId}) has not a valid view id",
      );
    }
    await client.startRemoteView(
      member.userId!,
      TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_BIG,
      viewId,
    );

    return super.play();
  }

  @override
  Future<void> stop() async {
    await client.stopRemoteView(
      member.userId!,
      TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_BIG,
    );
    return super.stop();
  }

  @override
  void setVolume(int volume) async {
    await client.setRemoteAudioVolume(
      member.userId!,
      volume,
    );
    _volume = volume;
  }

  @override
  int get volume => _volume;
}

mixin TrtcChannelMixin on BaseChannel {
  TrtcCapturer? _capturer;
  int _viewId = -1;
  int _textureId = -1;

  final FEventHandler<int> onTextureIdChanged = FEventHandler<int>();
  final FEventHandler<int> onViewIdChanged = FEventHandler<int>();

  /// 渲染纹理id
  int get textureId => _textureId;
  set textureId(int val) {
    if (val != _textureId) {
      _textureId = val;
      _onUpdateTextureId(val);
    }
  }

  /// 渲染View id
  int get viewId => _viewId;
  set viewId(int val) {
    if (val != _viewId) {
      _viewId = val;
      onViewIdChanged.emit(this, val);
      _onUpdateViewId();
    }
  }

  /// 是否本地推流通道
  bool get isLocal => this is IPublishChannel;

  @override
  ICapturer createCapture() {
    _capturer ??= TrtcCapturer(channel: this);
    return _capturer!;
  }

  void resetViewId() {
    _viewId = -1;
  }

  void _onUpdateViewId() {
    updateReadyState(viewId > -1);
  }

  void _onUpdateTextureId(int textureId) {
    onTextureIdChanged.emit(this, textureId);
  }
}
