import 'package:json_annotation/json_annotation.dart';

import '../../../api_response.dart';
import '../chat/get_chat_response.dart';

part 'get_room_response.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({
    required bool favoritesToTop,
    required bool unreadToTop,
  }) {
    return data.toList()..sort((a, b) {
      if (favoritesToTop && a.isFavorite != b.isFavorite) {
        return a.isFavorite ? -1 : 1;
      }
      if (unreadToTop) {
        final aUnread = a.unreadMessages > 0;
        final bUnread = b.unreadMessages > 0;
        if (aUnread != bUnread) return aUnread ? -1 : 1;
      }
      return b.lastActivity.compareTo(a.lastActivity);
    });
  }
}

@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;

  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,
  @JsonValue(6)
  noteToSelf,
}

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

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

enum GetRoomResponseObjectMessageType {
  @JsonValue('comment')
  comment,
  @JsonValue('voice-message')
  voiceMessage,
  @JsonValue('comment_deleted')
  deletedComment,
  @JsonValue('system')
  system,
  @JsonValue('command')
  command,
}
