import 'package:fis_lib_media_rt/base/member.dart';
import 'package:fis_lib_media_rt/base/room.dart';
import 'package:fis_lib_media_rt/fis_lib_media_rt.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'debug/GenerateTestUserSig.dart';

class TestDataStruct {
  String roomId = "1024";
  String userId = "";
  String userSig = "";
  IPublishChannel? localChannel;
  IPlayChannel? remoteChannel;
  late BaseRoom room;
  IPlayer? player;
  IPublisher? publisher;
  ICapturer? capturer;

  late VoidCallback update;

  void init() {
    FRTMediaConfig.trtc.appid = GenerateTestUserSig.sdkAppId;
  }

  bool entered = false;
  bool localStarted = false;

  void enter() async {
    sign();
    room = FRealTimeMedia.createRoom(roomId, FRealTimeMediaServiceType.trtc);
    room.entered.addListener(onEntered);
    room.exited.addListener(onExited);

    room.loadLocalMember(LocalMemberParams(
      userId: userId,
      userSig: userSig,
      extParams: {},
    ));
    room.enter();
  }

  Future<void> exit() async {
    await stopLocal();
    await room.exit();
    Future.delayed(const Duration(milliseconds: 200), update);
  }

  Future<void> startLocal() async {
    await publisher!.setProfile(PublisherProfile(
      resolutionMode: ResolutionMode.landscape,
      resolution: Resolutions.sd_480p,
      frameRate: 15,
    ));
    localStarted = true;
    update();
    await publisher!.startLocal();
  }

  Future<void> stopLocal() async {
    await publisher!.stopLocal();
    localStarted = false;
    update();
  }

  void setVideoMute(bool mute) {
    localChannel?.setVideoMute(mute);
    update();
  }

  bool get isVideoMuted => localChannel?.isMuteVideo == true;

  void initPlayer() {
    player = room.createPlayer();
    player!.channelAdded.addListener(onChannelAdded);
    player!.channelRemoved.addListener(onChannelRemoved);
  }

  void initPublisher() {
    publisher = room.createPublisher();
    localChannel = publisher!.channels.first;
  }

  void onChannelAdded(Object s, IPlayChannel channel) async {
    remoteChannel = channel;
    await remoteChannel!.play();
    update();
  }

  void onChannelRemoved(Object s, IPlayChannel channel) {
    remoteChannel?.stop();
    remoteChannel = null;
    update();
  }

  void onEntered(Object s, void e) {
    // 创建播放器和推流器
    initPublisher();
    initPlayer();
    entered = true;
    update();
  }

  void onExited(Object s, void e) {
    entered = false;
    localStarted = false;
    remoteChannel = null;
    localChannel = null;
    update();
  }

  void sign() {
    userSig = GenerateTestUserSig.genTestSig(userId);
  }

  /// 截取单帧 Vid
  void captureVid() async {
    capturer = remoteChannel!.createCapture();
    await capturer!.initCapturer();
    await capturer!.captureFrame();
  }

  /// 开始录制 Vid
  void startCaptureVid() async {
    capturer = remoteChannel!.createCapture();
    await capturer!.initCapturer();
    capturer!.startRecord();
  }

  /// 停止录制 Vid
  void stopCaptureVid() async {
    await capturer?.stopRecord();
  }
}
