import 'dart:convert';
import 'dart:io';

import 'radar_attachment.dart';

class MessageData {
  final int id;
  final String? text;
  final String? audio;
  final bool writedByAdmin;
  final bool readed;
  final String createdAt;
  final RadarAttachment? radar;
  final File? audioFile;
  final int? audioDuration;

  const MessageData({
    required this.id,
    required this.writedByAdmin,
    required this.readed,
    required this.createdAt,
    this.text,
    this.audio,
    this.radar,
    this.audioFile,
    this.audioDuration,
  });

  factory MessageData.fromJson(Map<String, dynamic> json) => MessageData(
        id: json['id'],
        text: json['text'],
        audio: json['audio'],
        writedByAdmin: json['writedByAdmin'],
        readed: json['readed'],
        createdAt: json['createdAt'],
        audioDuration: json['waveform'] == null
            ? null
            : jsonDecode(json['waveform'])['duration'] ?? 0,
        radar: json['radar'] == null
            ? null
            : RadarAttachment.fromJson(json['radar']),
      );

  Map<String, dynamic> toJson() => {
        'id': id,
        'text': text,
        'audio': audio,
        'writedByAdmin': writedByAdmin,
        'readed': readed,
        'createdAt': createdAt,
        'radar': radar?.toJson(),
      };
}
