using System;
using System.Collections.Generic;

namespace Knot.Contracts.Messaging.Domain;

public class MediaMessage : Message
{
    public override string Type => MediaType.ToString().ToLower();
    public override string? Content { get; protected set; }
    public string? Caption { get => Content; private set => Content = value; }
    public MediaType MediaType { get; private set; }
    
    private List<Media> _media = new();
    public IReadOnlyCollection<Media> Media => _media.AsReadOnly();

    private MediaMessage() : base() { MediaType = MediaType.File; }

    public MediaMessage(Guid id, Guid chatId, Guid senderId, MediaType mediaType, string? caption, Guid? replyToId, Guid? forwardedFromId, DateTime createdAt, bool isImported)
        : base(id, chatId, senderId, replyToId, forwardedFromId, createdAt, isImported) { MediaType = mediaType; Caption = caption; }

    public MediaMessage(Guid id, Guid chatId, Guid senderId, string mediaType, string? caption, Guid? replyToId, Guid? forwardedFromId, DateTime createdAt, bool isImported)
        : this(id, chatId, senderId, Enum.TryParse<MediaType>(mediaType, true, out var mt) ? mt : MediaType.File, caption, replyToId, forwardedFromId, createdAt, isImported) { }

    public void AddMedia(string type, string url, string? filename, long? size, string? duration = null) => _media.Add(new Media { Type = type, Url = url, Filename = filename, FileId = filename, Size = size, Duration = duration });

    public override void Edit(string newCaption) => base.Edit(newCaption);

    public override void Delete() { Caption = null; base.Delete(); }
}
