package com.rarchives.ripme.ripper.rippers;

import com.rarchives.ripme.ripper.AbstractHTMLRipper;
import com.rarchives.ripme.ui.RipStatusMessage;
import com.rarchives.ripme.utils.Http;
import org.json.JSONObject;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ErofusRipper extends AbstractHTMLRipper {

    private Document albumDoc = null;
    private Map<String,String> cookies = new HashMap<>();
    // TODO put up a wiki page on using maps to store titles
    // the map for storing the title of each album when downloading sub albums
    private Map<URL,String> urlTitles = new HashMap<>();

    private Boolean rippingSubalbums = false;

    public ErofusRipper(URL url) throws IOException {
        super(url);
    }

    @Override
    public boolean hasASAPRipping() {
        return true;
    }

    @Override
    public String getHost() {
        return "erofus";
    }

    @Override
    public String getDomain() {
        return "erofus.com";
    }

    @Override
    public String getGID(URL url) throws MalformedURLException {
        Pattern p = Pattern.compile("^https://www.erofus.com/comics/([a-zA-Z0-9\\-_]+).*$");
        Matcher m = p.matcher(url.toExternalForm());
        if (!m.matches()) {
            throw new MalformedURLException("Expected URL format: http://www.8muses.com/index/category/albumname, got: " + url);
        }
        return m.group(m.groupCount());
    }

    @Override
    public Document getFirstPage() throws IOException {
        if (albumDoc == null) {
            Connection.Response resp = Http.url(url).response();
            cookies.putAll(resp.cookies());
            albumDoc = resp.parse();
        }
        return albumDoc;
    }

    @Override
    public List<String> getURLsFromPage(Document page) {
        LOGGER.info(page);
        List<String> imageURLs = new ArrayList<>();
        int x = 1;
        if (pageContainsImages(page)) {
            LOGGER.info("Page contains images");
            ripAlbum(page);
        } else {
            // This contains the thumbnails of all images on the page
            Elements pageImages = page.select("a.a-click");
            for (Element pageLink : pageImages) {
                if (super.isStopped()) break;
                if (pageLink.attr("href").contains("comics")) {
                    String subUrl = "https://erofus.com" + pageLink.attr("href");
                    try {
                        LOGGER.info("Retrieving " + subUrl);
                        sendUpdate(RipStatusMessage.STATUS.LOADING_RESOURCE, subUrl);
                        Document subPage = Http.url(subUrl).get();
                        List<String> subalbumImages = getURLsFromPage(subPage);
                    } catch (IOException e) {
                        LOGGER.warn("Error while loading subalbum " + subUrl, e);
                    }
                }
                if (isThisATest()) break;
            }
        }


        return imageURLs;
    }

    public void ripAlbum(Document page) {
        int x = 1;
        Elements thumbs = page.select("a.a-click > div.thumbnail > img");
        for (Element thumb : thumbs) {
            String image = "https://www.erofus.com" + thumb.attr("src").replaceAll("thumb", "medium");
            try {
                Map<String,String> opts = new HashMap<String, String>();
                opts.put("subdirectory", page.title().replaceAll(" \\| Erofus - Sex and Porn Comics", "").replaceAll(" ", "_"));
                opts.put("prefix", getPrefix(x));
                addURLToDownload(new URL(image), opts);
            } catch (MalformedURLException e) {
                LOGGER.info(e.getMessage());
            }
            x++;
        }
    }

    private boolean pageContainsImages(Document page) {
        Elements pageImages = page.select("a.a-click");
        for (Element pageLink : pageImages) {
            if (pageLink.attr("href").contains("/pic/")) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void downloadURL(URL url, int index) {
        addURLToDownload(url, getPrefix(index), "", this.url.toExternalForm(), cookies);
    }
}
@@ -183,65 +183,8 @@
 tp;%0A
-import org.json.JSONObject;%0Aimport org.jsoup.Connection;%0A
 impo
@@ -208,24 +208,24 @@
 s.Document;%0A
+
 import org.j
@@ -591,349 +591,8 @@
  %7B%0A%0A
-    private Document albumDoc = null;%0A    private Map%3CString,String%3E cookies = new HashMap%3C%3E();%0A    // TODO put up a wiki page on using maps to store titles%0A    // the map for storing the title of each album when downloading sub albums%0A    private Map%3CURL,String%3E urlTitles = new HashMap%3C%3E();%0A%0A    private Boolean rippingSubalbums = false;%0A%0A
     
@@ -1427,211 +1427,34 @@
     
-if (albumDoc == null) %7B%0A            Connection.Response resp = Http.url(url).response();%0A            cookies.putAll(resp.cookies());%0A            albumDoc = resp.parse();%0A        %7D%0A        return albumDoc
+return Http.url(url).get()
 ;%0A  
@@ -3879,48 +3879,8 @@
 dex)
-, %22%22, this.url.toExternalForm(), cookies
 );%0A 

Remove some dead code