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 = MapAuthor(dto.Author),
            Text = dto.Text,
            Price = dto.Price,
            IsOpened = dto.IsOpened,
            IsArchived = dto.IsArchived,
            Media = MapMedia(dto.Media),
            Preview = dto.Preview
        };

    private static Entities.Common.Author? MapAuthor(AuthorDto? dto)
    {
        if (dto == null)
        {
            return null;
        }

        return new Entities.Common.Author { Id = dto.Id };
    }

    private static List<Medium>? MapMedia(List<MediumDto>? media)
    {
        if (media == null)
        {
            return null;
        }

        return 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)
    {
        if (dto == null)
        {
            return null;
        }

        return new Entities.Common.Files { Full = MapFull(dto.Full), Drm = MapDrm(dto.Drm) };
    }

    private static Entities.Common.Full? MapFull(FullDto? dto) =>
        dto == null ? null : new Entities.Common.Full { Url = dto.Url };

    private static Entities.Common.Drm? MapDrm(DrmDto? dto)
    {
        if (dto == null)
        {
            return null;
        }

        return new Entities.Common.Drm
        {
            Manifest = MapManifest(dto.Manifest), Signature = MapSignature(dto.Signature)
        };
    }

    private static Entities.Common.Manifest? MapManifest(ManifestDto? dto) =>
        dto == null ? null : new Entities.Common.Manifest { Dash = dto.Dash };

    private static Entities.Common.Signature? MapSignature(SignatureDto? dto) =>
        dto == null ? null : new Entities.Common.Signature { Dash = MapDash(dto.Dash) };

    private static Entities.Common.Dash? MapDash(DashDto? dto)
    {
        if (dto == null)
        {
            return null;
        }

        return new Entities.Common.Dash
        {
            CloudFrontPolicy = dto.CloudFrontPolicy,
            CloudFrontSignature = dto.CloudFrontSignature,
            CloudFrontKeyPairId = dto.CloudFrontKeyPairId
        };
    }
}
