import 'package:json_annotation/json_annotation.dart';

import '../../../apiResponse.dart';
import '../chat/getChatResponse.dart';

part 'getRoomResponse.g.dart';

@JsonSerializable(explicitToJson: true)
class GetRoomResponse extends ApiResponse {
  Set<GetRoomResponseObject> data;

  GetRoomResponse(this.data);

  factory GetRoomResponse.fromJson(Map<String, dynamic> json) => _$GetRoomResponseFromJson(json);
  Map<String, dynamic> toJson() => _$GetRoomResponseToJson(this);

  List<GetRoomResponseObject> sortBy({bool lastActivity = true, required bool favoritesToTop, required bool unreadToTop}) {
    for (var chat in data) {
      final buffer = StringBuffer();

      if(favoritesToTop) {
        buffer.write(chat.isFavorite ? 'b' : 'a');
      }
      if(unreadToTop) {
        buffer.write(chat.unreadMessages > 0 ? 'b' : 'a');
      }

      buffer.write(chat.lastActivity);

      chat.sort = buffer.toString();
    }

    return data.toList()..sort((a, b) => b.sort!.compareTo(a.sort!));
  }
}

@JsonSerializable(explicitToJson: true)
class GetRoomResponseObject {
  int id;
  String token;
  GetRoomResponseObjectConversationType type;
  String name;
  String displayName;
  String description;
  int participantType;
  int participantFlags;
  int readOnly;
  int listable;
  int lastPing;
  String sessionId;
  bool hasPassword;
  bool hasCall;
  int callFlag;
  bool canStartCall;
  bool canDeleteConversation;
  bool canLeaveConversation;
  int lastActivity;
  bool isFavorite;
  GetRoomResponseObjectParticipantNotificationLevel notificationLevel;
  int unreadMessages;
  bool unreadMention;
  bool unreadMentionDirect;
  int lastReadMessage;
  int lastCommonReadMessage;
  GetChatResponseObject lastMessage;
  String? status;
  String? statusIcon;
  String? statusMessage;
  String? sort;

  GetRoomResponseObject(
      this.id,
      this.token,
      this.type,
      this.name,
      this.displayName,
      this.description,
      this.participantType,
      this.participantFlags,
      this.readOnly,
      this.listable,
      this.lastPing,
      this.sessionId,
      this.hasPassword,
      this.hasCall,
      this.callFlag,
      this.canStartCall,
      this.canDeleteConversation,
      this.canLeaveConversation,
      this.lastActivity,
      this.isFavorite,
      this.notificationLevel,
      this.unreadMessages,
      this.unreadMention,
      this.unreadMentionDirect,
      this.lastReadMessage,
      this.lastCommonReadMessage,
      this.lastMessage,
      this.status,
      this.statusIcon,
      this.statusMessage);

  factory GetRoomResponseObject.fromJson(Map<String, dynamic> json) => _$GetRoomResponseObjectFromJson(json);
  Map<String, dynamic> toJson() => _$GetRoomResponseObjectToJson(this);
}

enum GetRoomResponseObjectConversationType {
  @JsonValue(1) oneToOne,
  @JsonValue(2) group,
  @JsonValue(3) public,
  @JsonValue(4) changelog,
  @JsonValue(5) deleted,
}

enum GetRoomResponseObjectParticipantNotificationLevel {
  @JsonValue(0) defaultLevel,
  @JsonValue(1) alwaysNotify,
  @JsonValue(2) notifyOnMention,
  @JsonValue(3) neverNotify,
}

// @JsonSerializable(explicitToJson: true)
// class GetRoomResponseObjectMessage {
//   int id;
//   String token;
//   GetRoomResponseObjectMessageActorType actorType;
//   String actorId;
//   String actorDisplayName;
//   int timestamp;
//   String message;
//   String systemMessage;
//   GetRoomResponseObjectMessageType messageType;
//   bool isReplyable;
//   String referenceId;
//
//
//   GetRoomResponseObjectMessage(
//       this.id,
//       this.token,
//       this.actorType,
//       this.actorId,
//       this.actorDisplayName,
//       this.timestamp,
//       this.message,
//       this.systemMessage,
//       this.messageType,
//       this.isReplyable,
//       this.referenceId);
//
//   factory GetRoomResponseObjectMessage.fromJson(Map<String, dynamic> json) => _$GetRoomResponseObjectMessageFromJson(json);
//   Map<String, dynamic> toJson() => _$GetRoomResponseObjectMessageToJson(this);
// }

enum GetRoomResponseObjectMessageActorType {
  @JsonValue('deleted_users') deletedUsers,
  @JsonValue('users') user,
  @JsonValue('guests') guest,
  @JsonValue('bots') bot,
  @JsonValue('bridged') bridge,
}

enum GetRoomResponseObjectMessageType {
  @JsonValue('comment') comment,
  @JsonValue('comment_deleted') deletedComment,
  @JsonValue('system') system,
  @JsonValue('command') command,
}
