package me.devsaki.hentoid.json;

import androidx.annotation.NonNull;

import com.annimon.stream.Optional;
import com.annimon.stream.Stream;

import java.util.List;

import me.devsaki.hentoid.database.domains.Chapter;
import me.devsaki.hentoid.database.domains.ImageFile;
import me.devsaki.hentoid.enums.StatusContent;

class JsonImageFile {

    private Integer order;
    private String url;
    private String name;
    private boolean isCover;
    private boolean favourite;
    private boolean isRead;
    private StatusContent status;
    private String mimeType;
    private long pHash;

    private int chapterOrder = -1;

    private JsonImageFile() {
    }

    static JsonImageFile fromEntity(ImageFile f) {
        JsonImageFile result = new JsonImageFile();
        result.order = f.getOrder();
        result.url = f.getUrl();
        result.name = f.getName();
        result.status = f.getStatus();
        result.isCover = f.isCover();
        result.favourite = f.isFavourite();
        result.isRead = f.isRead();
        result.mimeType = f.getMimeType();
        result.pHash = f.getImageHash();
        if (f.getChapter() != null && f.getChapter().getTarget() != null)
            result.chapterOrder = f.getChapter().getTarget().getOrder();
        return result;
    }

    ImageFile toEntity(int maxPages, @NonNull List<Chapter> chapters) {
        ImageFile result = ImageFile.fromImageUrl(order, url, status, maxPages);
        result.setName(name);
        result.setIsCover(isCover);
        result.setFavourite(favourite);
        result.setRead(isRead);
        result.setMimeType(mimeType);
        result.setImageHash(pHash);

        if (!chapters.isEmpty() && chapterOrder > -1) {
            Optional<Chapter> chapter = Stream.of(chapters).filter(c -> c.getOrder().equals(chapterOrder)).findFirst();
            if (chapter.isPresent()) result.setChapter(chapter.get());
        }

        return result;
    }
}

@@ -387,16 +387,44 @@
 ng url;%0A
+    private String pageUrl;%0A
     priv
@@ -858,24 +858,65 @@
 f.getUrl();%0A
+        result.pageUrl = f.getPageUrl();%0A
         resu
@@ -1509,16 +1509,109 @@
 Pages);%0A
+        if (url.isEmpty()) result = ImageFile.fromPageUrl(order, pageUrl, status, maxPages);%0A
         

Serialize page URLs to JSON