import 'dart:async';

import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_floating/floating/floating.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:preload_page_view/preload_page_view.dart';
import 'package:signalr_core/signalr_core.dart';

import '../../common/models/meeting_room_info.dart';
import '../../common/models/meeting_room_msg.dart';
import '../../common/models/meeting_room_user.dart';

class MeetingMainState {

  MeetingMainState() {
    ///Initialize variables
  }

  late TextEditingController memberNameSearchController = TextEditingController();
  late TextEditingController sendMsgController = TextEditingController();
  late PreloadPageController pageController = PreloadPageController(initialPage: 0);
  late Rx<Floating?> floating = Rx(null);
  late Rx<BuildContext?> context = Rx(null);

  /// 是否显示会议信息浮层
  late RxBool isShowMeetingInfoFloatingLayer = false.obs;

  /// 是否显示音频选择浮层
  late RxBool isShowMeetingAudioFloatingLayer = false.obs;

  /// 聊天弹窗中Listview控制器
  late ScrollController chatController = ScrollController();

  /// 当前页面状态，0：语音，1：视频，2，共享
  late RxInt pageState = 0.obs;
  /// 视频页面状态时，pageview指示器
  late RxInt pageIndex = 0.obs;

  /// 会议室编号
  late RxString roomNumber = "".obs;

  /// 会议室token
  late RxString meetingToken = "".obs;

  /// 麦克风摄像头权限是否申请成功
  late RxBool isCameraToMicrophonePermissions = false.obs;

  /// 会议室信息
  late Rx<MeetingRoomInfo?> meetingRoomInfo = Rx(null);
  /// 是否弹出showOkAlertDialog
  late RxBool isShowOkAlertDialog = false.obs;
  /// 是否正常退出（点击仅自己退出、全员结束、被移除）
  late RxBool isNormaExit = false.obs;

  /// 会议室计时相关
  late RxString duration = "".obs;
  late Rx<Stopwatch> stopwatch = Rx(Stopwatch());
  late Rx<Timer?> timer = Rx(null);

  /// Http接口获取的会议室所有用户
  late RxList<MeetingRoomUser> users = RxList([]);

  /// 搜索用户时，缓存会议室所有用户原始数据
  late RxList<MeetingRoomUser> cacheUsers = RxList([]);

  /// 是否被允许发言
  late RxBool isSpeak = false.obs;
  /// 允许发言后默认开启麦克风还是摄像头，0都关闭，1麦克风，2摄像头
  late RxInt defaulOpenState = 0.obs;
  /// 是否打开麦克风
  late RxBool isOpenMicrophone = false.obs;
  /// 麦克风音量
  late RxDouble microphoneVolume = 0.0.obs;
  /// 是否打开摄像头
  late RxBool isOpenCamera = false.obs;
  /// 是否启动屏幕共享摄像头
  late RxBool isOpenShare = false.obs;
  /// 当前视频主播ID
  late RxString remoteUid = "".obs;
  /// 当前视频主播ID-辅助判断用
  late RxString remoteAssistantUid = "".obs;
  /// 是否成功加入会议室
  late RxBool isJoinSuccess = false.obs;

  /// 当前谁在说话
  late RxString spokesman = "".obs;
  /// 当前说话音量
  late RxDouble spokesmanVolume = 0.0.obs;

  /// 聊天数据
  late RxList<MeetingRoomMsg> meetingRoomMsgs = RxList([]);

  /// 聊天消息未读数量
  late RxInt msgNum = 0.obs;

  /// signalR 长连接相关
  late RxString serviceUrl = "http://192.168.2.9:5192/session-manage".obs;
  late Rx<HubConnection?> hubConnection = Rx(null);

  /// 声网相关
  final String appId = "4a4f7be64fa1404ebda74784fe9ac381";
  Rx<RtcEngine?> rctEngine = Rx<RtcEngine?>(null);
  /// 是否自动订阅所有视频流
  late RxBool isAutoSubscribeVideo = false.obs;
  /// 是否自动订阅所有音频流
  late RxBool isAutoSubscribeAudio = false.obs;
  /// 是否发布摄像头采集的视频
  late RxBool isPublishCameraTrack = false.obs;
  /// 是否发布麦克风采集的音频
  late RxBool isPublishMicrophoneTrack = false.obs;
  /// 当前音频路由，1：听筒，3：扬声器
  late RxInt communicationMode = 1.obs;
}
