using OF_DL.Models.Dtos.Archived;
using OF_DL.Models.Dtos.Common;
using OF_DL.Models.Entities.Archived;

namespace OF_DL.Models.Mappers;

public static class ArchivedMapper
{
    public static Archived FromDto(ArchivedDto? dto)
    {
        Archived mapped = new() { HasMore = dto?.HasMore ?? false, TailMarker = dto?.TailMarker };

        if (dto?.List == null)
        {
            return mapped;
        }

        foreach (ListItemDto entry in dto.List)
        {
            mapped.List.Add(MapList(entry));
        }

        return mapped;
    }

    private static ListItem MapList(ListItemDto dto) =>
        new()
        {
            Id = dto.Id,
            PostedAt = dto.PostedAt,
            Author = CommonMapper.MapAuthor(dto.Author),
            Text = dto.Text,
            Price = dto.Price,
            IsOpened = dto.IsOpened,
            IsArchived = dto.IsArchived,
            Media = MapMedia(dto.Media),
            Preview = dto.Preview
        };

    private static List<Medium>? MapMedia(List<MediumDto>? media) =>
        media == null ? null : media.Select(MapMedium).ToList();

    private static Medium MapMedium(MediumDto dto) =>
        new()
        {
            Id = dto.Id,
            Type = dto.Type,
            CanView = dto.CanView,
            Files = MapFiles(dto.Files),
            Preview = dto.Preview
        };

    private static Entities.Common.Files? MapFiles(FilesDto? dto) => dto == null
        ? null
        : new Entities.Common.Files { Full = CommonMapper.MapFull(dto.Full), Drm = CommonMapper.MapDrm(dto.Drm) };
}
