import 'dart:convert';

import 'package:chat/models/im/custom_message_model.dart';

class CallingModel extends CustomMessageModel {
  CallingModel({
    this.businessID = CustomMessageType.CALL,
    required this.callType,
    required this.inviter,
    required this.inviteeList,
    required this.data,
    required this.timeout,
    required this.actionType,
    required this.onlineUserOnly,
    required this.isGroup,
  });

  @override
  String businessID;

  /// 通话类型 videoCall audioCall
  String callType;

  /// 邀请人
  String inviter;

  /// 被邀请人
  List<String> inviteeList;

  /// 通话时长
  int timeout;
  // 1: 邀请方发起邀请
  // 2: 邀请方取消邀请
  // 3: 被邀请方接受邀请
  // 4: 被邀请方拒绝邀请
  // 5: 邀请超时
  int actionType;

  bool onlineUserOnly;

  /// 是否是群语音
  bool isGroup;
  CallingModelData data;

  String get actionTypeText {
    final actionMessage = {
      1: "发起通话",
      2: "取消通话",
      3: "接受通话",
      4: "拒绝通话",
      5: "超时未接听",
    };
    return actionMessage[actionType] ?? "";
  }

  factory CallingModel.fromJson(Map<String, dynamic> json) => CallingModel(
        callType: jsonDecode(json['data'])['data']['cmd'],
        inviter: json['inviter'],
        inviteeList: List<String>.from(
          json['inviteeList'].map(
            (x) => x.toString(),
          ),
        ),
        data: CallingModelData.fromJson(jsonDecode(json['data'])),
        timeout: json['timeout'],
        actionType: json['actionType'],
        onlineUserOnly: json['onlineUserOnly'],
        isGroup: jsonDecode(json['data'])['is_group'],
      );

  @override
  String toJson() {
    return '';
  }
}

class CallingModelData {
  CallingModelData({
    required this.version,
    required this.callType,
    required this.data,
    required this.roomId,
    required this.isGroup,
  });

  int version;
  int callType;
  DataData data;
  int roomId;
  bool isGroup;

  factory CallingModelData.fromJson(Map<String, dynamic> json) =>
      CallingModelData(
        version: json['version'],
        callType: json['call_type'],
        data: DataData.fromJson(json['data']),
        roomId: json['room_id'],
        isGroup: json['is_group'],
      );
}

class DataData {
  DataData({
    required this.cmd,
    required this.roomId,
    required this.message,
    required this.cmdInfo,
  });

  String cmd; // videoCall  audioCall
  int roomId;
  String message;
  String cmdInfo;

  factory DataData.fromJson(Map<String, dynamic> json) => DataData(
        cmd: json['cmd'],
        roomId: json['room_id'],
        message: json['message'],
        cmdInfo: json['cmd_info'],
      );
}
