package me.devsaki.hentoid.parsers.content;

import androidx.annotation.NonNull;

import org.jsoup.nodes.Element;

import java.util.List;

import javax.annotation.Nonnull;

import me.devsaki.hentoid.database.domains.Content;
import me.devsaki.hentoid.enums.AttributeType;
import me.devsaki.hentoid.enums.Site;
import me.devsaki.hentoid.enums.StatusContent;
import me.devsaki.hentoid.parsers.ParseHelper;
import me.devsaki.hentoid.util.AttributeMap;
import me.devsaki.hentoid.util.Helper;
import pl.droidsonroids.jspoon.annotation.Selector;

public class PururinContent extends BaseContentParser {
    @Selector(value = "head [property=og:url]", attr = "content", defValue = "")
    private String galleryUrl;
    @Selector(value = "head [property=og:image]", attr = "content")
    private String coverUrl;
    @Selector(value = "div.title", defValue = "")
    private List<String> title;
    @Selector("table.table-gallery-info tr td")
    private List<String> pages;
    @Selector(value = "table.table-gallery-info a[href*='/tags/artist']")
    private List<Element> artists;
    @Selector(value = "table.table-gallery-info a[href*='/tags/circle']")
    private List<Element> circles;
    @Selector(value = "table.table-gallery-info a[href*='/tags/content']")
    private List<Element> tags;
    @Selector(value = "table.table-gallery-info a[href*='/tags/parody']")
    private List<Element> series;
    @Selector(value = "table.table-gallery-info a[href*='/tags/character']")
    private List<Element> characters;
    @Selector(value = "table.table-gallery-info a[href*='/tags/language']")
    private List<Element> languages;
    @Selector(value = "table.table-gallery-info a[href*='/tags/category']")
    private List<Element> categories;


    private String getProtocol() {
        return galleryUrl.startsWith("https") ? "https" : "http";
    }

    public Content update(@NonNull final Content content, @Nonnull String url) {
        content.setSite(Site.PURURIN);
        String theUrl = galleryUrl.isEmpty() ? url : galleryUrl;
        if (theUrl.isEmpty()) return new Content().setStatus(StatusContent.IGNORED);

        content.setUrl(theUrl.replace(getProtocol() + "://pururin.io/gallery", ""));
        content.setCoverImageUrl(getProtocol() + ":" + coverUrl);
        content.setTitle(!title.isEmpty() ? Helper.removeNonPrintableChars(title.get(0)) : "");
        int qtyPages = 0;
        boolean pagesFound = false;
        for (String s : pages) {
            if (pagesFound) {
                qtyPages = Integer.parseInt(ParseHelper.removeBrackets(s));
                break;
            }
            if (s.trim().equalsIgnoreCase("pages")) pagesFound = true;
        }
        content.setQtyPages(qtyPages);

        AttributeMap attributes = new AttributeMap();
        ParseHelper.parseAttributes(attributes, AttributeType.ARTIST, artists, false, Site.PURURIN);
        ParseHelper.parseAttributes(attributes, AttributeType.CIRCLE, circles, false, Site.PURURIN);
        ParseHelper.parseAttributes(attributes, AttributeType.TAG, tags, false, Site.PURURIN);
        ParseHelper.parseAttributes(attributes, AttributeType.SERIE, series, false, Site.PURURIN);
        ParseHelper.parseAttributes(attributes, AttributeType.CHARACTER, characters, false, Site.PURURIN);
        ParseHelper.parseAttributes(attributes, AttributeType.LANGUAGE, languages, false, Site.PURURIN);
        ParseHelper.parseAttributes(attributes, AttributeType.CATEGORY, categories, false, Site.PURURIN);
        content.putAttributes(attributes);

        return content;
    }
}

@@ -594,120 +594,8 @@
 r %7B%0A
-    @Selector(value = %22head %5Bproperty=og:url%5D%22, attr = %22content%22, defValue = %22%22)%0A    private String galleryUrl;%0A
     
@@ -1657,16 +1657,26 @@
 rotocol(
+String url
 ) %7B%0A    
@@ -1686,24 +1686,17 @@
  return 
-galleryU
+u
 rl.start
@@ -1869,147 +1869,152 @@
     
-String theUrl = galleryUrl.isEmpty() ? url : galleryUrl;%0A        if (theUrl.isEmpty()) return new Content().setStatus(StatusContent.IGNORED
+if (url.isEmpty()) return new Content().setStatus(StatusContent.IGNORED);%0A        if (url.endsWith(%22/%22)) url = url.substring(0, url.length() - 1
 );%0A%0A
@@ -2040,12 +2040,9 @@
 Url(
-theU
+u
 rl.r
@@ -2056,24 +2056,27 @@
 getProtocol(
+url
 ) + %22://puru
@@ -2143,16 +2143,19 @@
 rotocol(
+url
 ) + %22:%22 

Fix download from pururin after choosing "open with..."