import 'package:daniulive/controllers/interfaces/play_controller.dart';
import 'package:daniulive/controllers/merge_play_controller.dart';
import 'package:daniulive/controllers/play_controller.dart';
import 'package:daniulive/controllers/publish_controller.dart';
import 'package:fis_lib_media_rt/implementations/nt_rtmp/members.dart';
import 'package:fis_lib_media_rt/implementations/nt_rtmp/models.dart';
import 'package:fis_lib_media_rt/implementations/nt_rtmp/values.dart';
import 'package:flutter/foundation.dart';

import '../native_share.dart';
import 'base.dart';

class NTPublishChannel extends NTBasePublishChannel {
  int _volume = 100;

  NTPublishChannel({required super.member});

  @override
  void setAudioMute(bool isMute) async {
    await NTNativeShare.publishController?.muteAudio(isMute);
    super.setAudioMute(isMute);
  }

  @override
  void setVideoMute(bool isMute) async {
    await NTNativeShare.publishController?.muteVideo(isMute);
    super.setVideoMute(isMute);
  }

  @override
  void setVolume(int volume) async {
    await NTNativeShare.publishController?.setVolume(volume);
    _volume = volume;
  }

  @override
  void onViewIdChanged() {
    updateReadyState(viewId > 0);
  }

  @override
  int get volume => _volume;
}

class NTPlayChannel extends NTBasePlayChannel {
  int _volume = 100;

  NTPlayChannel({required super.member}) {
    _initMobile();
  }

  @override
  int get volume => _volume;

  IPlayController? get playController =>
      NTNativeShare.playControllers.get(member.userId!);

  @override
  void setAudioMute(bool isMute) async {
    await playController?.muteAudio(isMute);
    super.setAudioMute(isMute);
  }

  @override
  void setVideoMute(bool isMute) async {
    await playController?.muteVideo(isMute);
    super.setVideoMute(isMute);
  }

  @override
  Future<void> play() async {
    await playController?.play();
    return super.play();
  }

  @override
  Future<void> stop() async {
    await playController?.stop();
    return super.stop();
  }

  @override
  void setVolume(int volume) async {
    await playController?.setVolume(volume);
    _volume = volume;
  }

  @override
  void onViewIdChanged() {
    updateReadyState(viewId > 0);
  }

  void _initMobile() {
    final userId = member.userId!;
    if (isDevice) {
      _loadMobileDevice(userId);
    } else {
      final controller = PlayController(liveInfo.rtmpUrl, userId: userId);
      NTNativeShare.playControllers.put(userId, controller);
    }
  }

  void _loadMobileDevice(String userId) {
    DeviceRtmpLiveInfo info = liveInfo as DeviceRtmpLiveInfo;
    if (info.isMerge) {
      final subCount = (member as RtmpMemberInfo).deviceInfoList!.length - 1;
      if (isDeviceMain) {
        final controller = MergePlayController(
          liveInfo.rtmpUrl,
          userId: MERGE_DEVICE_NATIVE_SIGN,
          mergeWidth: info.mergeWidth,
          mergeHeight: info.mergeHeight,
          subViewCount: subCount,
        );
        NTNativeShare.playControllers.put(MERGE_DEVICE_NATIVE_SIGN, controller);
      }
    } else {
      final controller = PlayController(liveInfo.rtmpUrl, userId: userId);
      NTNativeShare.playControllers.put(userId, controller);
    }
  }
}
