using OF_DL.Models.Dtos.Purchased;
using MessageDtos = OF_DL.Models.Dtos.Messages;
using CommonEntities = OF_DL.Models.Entities.Common;
using MessageEntities = OF_DL.Models.Entities.Messages;
using PurchasedEntities = OF_DL.Models.Entities.Purchased;


namespace OF_DL.Models.Mappers;

public static class PurchasedMapper
{
    public static PurchasedEntities.Purchased FromDto(PurchasedDto? dto)
    {
        PurchasedEntities.Purchased mapped = new() { HasMore = dto?.HasMore ?? false };

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

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

        return mapped;
    }

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

    private static CommonEntities.FromUser? MapFromUser(FromUserDto? dto)
    {
        if (dto == null || dto.Id == 0)
        {
            return null;
        }

        return new CommonEntities.FromUser { Id = dto.Id };
    }

    private static List<MessageEntities.Medium>? MapMedia(List<MessageDtos.MediumDto>? media) =>
        media?.Select(MessagesMapper.MapMedium).ToList();
}
