import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_lib_media_rt/interface/index.dart';
import 'package:get/get.dart';

import 'consultation_member.dart';

class FLiveCourseMember {
  List<IChannel>? channelInfos;
  String? id;
  String? name;
  LiveMemberEnum memberType;
  String? headImageToken;
  UserStatusEnum userStatusType;
  bool mute;
  bool videoOpend;
  bool isTeacher;
  bool isExpertUser;
  bool isAssistantUser;
  MemberStatus memberStatus;
  String? loginServerUrl;
  LoginSource loginSource;
  LiveDataDTO? liveData;
  List<VideoDeviceOutputInfo>? videoDeviceInfos;
  bool isControllingParameter;
  bool mergedChannel;
  int mergedVideoOutputWidth;
  int mergedVideoOutputHeight;
  int index;
  TransactionStatusEnum liveProtocol;
  bool isOldPlatform;

  FLiveCourseMember({
    this.id,
    this.name,
    this.memberType = LiveMemberEnum.User,
    this.headImageToken,
    this.userStatusType = UserStatusEnum.NotOnline,
    this.mute = false,
    this.videoOpend = false,
    this.isTeacher = false,
    this.isExpertUser = false,
    this.isAssistantUser = false,
    this.memberStatus = MemberStatus.Default,
    this.loginServerUrl,
    this.loginSource = LoginSource.PC,
    this.liveData,
    this.videoDeviceInfos,
    this.isControllingParameter = false,
    this.mergedChannel = false,
    this.mergedVideoOutputWidth = 0,
    this.mergedVideoOutputHeight = 0,
    this.channelInfos,
    this.liveProtocol = TransactionStatusEnum.TRTC,
    this.index = 0,
    this.isOldPlatform = false,
  });

  factory FLiveCourseMember.fromDto(LiveCourseMember e) {
    return FLiveCourseMember(
      id: e.id,
      name: e.name,
      memberType: e.memberType,
      headImageToken: e.headImageToken,
      userStatusType: e.userStatusType,
      mute: e.mute,
      videoOpend: e.videoOpend,
      isTeacher: e.isTeacher,
      isExpertUser: e.isExpertUser,
      isAssistantUser: e.isAssistantUser,
      memberStatus: MemberStatus.values
              .firstWhereOrNull((element) => element.index == e.status.index) ??
          MemberStatus.Default,
      loginServerUrl: e.loginServerUrl,
      loginSource: e.loginSource,
      liveData: e.liveData,
      videoDeviceInfos: e.videoDeviceInfos,
      isControllingParameter: e.isControllingParameter,
      mergedChannel: e.mergedChannel,
      mergedVideoOutputHeight: e.mergedVideoOutputHeight,
      mergedVideoOutputWidth: e.mergedVideoOutputWidth,
      liveProtocol: e.liveProtocol,
    );
  }

  FLiveCourseMember fixMergedVideoOutputSize() {
    /// 修正老版本单图合流模式下的总尺寸
    /// TODO(Loki): 使用isOldPlatform代替宽高为0的判断
    if (mergedVideoOutputHeight == 0 &&
        mergedVideoOutputWidth == 0 &&
        videoDeviceInfos != null &&
        videoDeviceInfos!.length == 1) {
      int width = videoDeviceInfos![0].outputWidth;
      int height = videoDeviceInfos![0].outputHeight;
      if (width <= 640 && height <= 480) {
        mergedVideoOutputWidth = 640;
        mergedVideoOutputHeight = 480;
      } else if (width <= 960 && height <= 544) {
        mergedVideoOutputWidth = 960;
        mergedVideoOutputHeight = 540;
      } else if (width <= 960 && height <= 720) {
        mergedVideoOutputWidth = 960;
        mergedVideoOutputHeight = 720;
      } else if (width <= 1280 && height <= 720) {
        mergedVideoOutputWidth = 1280;
        mergedVideoOutputHeight = 720;
      } else {
        mergedVideoOutputWidth = 1920;
        mergedVideoOutputHeight = 1080;
      }
    }

    /// 修正老版本双路合流模式下的总尺寸
    if (mergedVideoOutputHeight == 0 &&
        mergedVideoOutputWidth == 0 &&
        videoDeviceInfos != null &&
        videoDeviceInfos!.length == 2) {
      if (videoDeviceInfos![0].videoDeviceSourceType ==
          VideoDeviceSourceTypeEnum.Desktop) {
        int width = videoDeviceInfos![0].outputWidth;
        int height = videoDeviceInfos![0].outputHeight;
        int subWidth = videoDeviceInfos![1].outputWidth;
        int subHeight = videoDeviceInfos![1].outputHeight;
        if (width <= 640 && height <= 480) {
          mergedVideoOutputWidth = 640;
          mergedVideoOutputHeight = 480;
        } else if (width <= 960 && height <= 544) {
          mergedVideoOutputWidth = 1280;
          mergedVideoOutputHeight = 720;
        } else if (width <= 960 && height <= 720) {
          mergedVideoOutputWidth = 1440;
          mergedVideoOutputHeight = 720;
        } else if (width <= 1280 && height <= 720) {
          mergedVideoOutputWidth = 1920;
          mergedVideoOutputHeight = 1080;
        } else {
          mergedVideoOutputWidth = 1920;
          mergedVideoOutputHeight = 1080;
        }

        /// 特殊情况：魔盒下无论画面为多大，总画面都是1920*1080
        /// 魔盒下的摄像头为 640*480
        if (subWidth == 640 && subHeight == 480) {
          mergedVideoOutputWidth = 1920;
          mergedVideoOutputHeight = 1080;
        }
      }
    }
    return this;
  }
}
