package com.rarchives.ripme.ripper.rippers;

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;

import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.rarchives.ripme.ripper.AbstractHTMLRipper;
import com.rarchives.ripme.utils.Http;

public class HentaifoundryRipper extends AbstractHTMLRipper {

    private Map<String,String> cookies = new HashMap<String,String>();
    public HentaifoundryRipper(URL url) throws IOException {
        super(url);
    }

    @Override
    public String getHost() {
        return "hentai-foundry";
    }
    @Override
    public String getDomain() {
        return "hentai-foundry.com";
    }

    @Override
    public String getGID(URL url) throws MalformedURLException {
        Pattern p = Pattern.compile("^.*hentai-foundry\\.com/pictures/user/([a-zA-Z0-9\\-_]+).*$");
        Matcher m = p.matcher(url.toExternalForm());
        if (m.matches()) {
            return m.group(1);
        }
        throw new MalformedURLException(
                "Expected hentai-foundry.com gallery format: "
                        + "hentai-foundry.com/pictures/user/USERNAME"
                        + " Got: " + url);
    }

    @Override
    public Document getFirstPage() throws IOException {
        Response resp = Http.url("http://www.hentai-foundry.com/").response();
        cookies = resp.cookies();
        resp = Http.url("http://www.hentai-foundry.com/?enterAgree=1&size=1500")
                   .referrer("http://www.hentai-foundry.com/")
                   .cookies(cookies)
                   .response();
        cookies.putAll(resp.cookies());
        sleep(500);
        resp = Http.url(url)
                   .referrer("http://www.hentai-foundry.com/")
                   .cookies(cookies)
                   .response();
        cookies.putAll(resp.cookies());
        return resp.parse();
    }

    @Override
    public Document getNextPage(Document doc) throws IOException {
        if (doc.select("li.next.hidden").size() > 0) {
            // Last page
            throw new IOException("No more pages");
        }
        Elements els = doc.select("li.next > a");
        Element first = els.first();
        String nextURL = first.attr("href");
        nextURL = "http://www.hentai-foundry.com" + nextURL;
        return Http.url(nextURL)
                   .referrer(url)
                   .cookies(cookies)
                   .get();
    }

    @Override
    public List<String> getURLsFromPage(Document doc) {
        List<String> imageURLs = new ArrayList<String>();
        Pattern imgRegex = Pattern.compile(".*/user/([a-zA-Z0-9\\-_]+)/(\\d+)/.*");
        for (Element thumb : doc.select("td > a:first-child")) {
            if (isStopped()) {
                break;
            }
            Matcher imgMatcher = imgRegex.matcher(thumb.attr("href"));
            if (!imgMatcher.matches()) {
                logger.info("Couldn't find user & image ID in " + thumb.attr("href"));
                continue;
            }
            String[] titleSplit = thumb.attr("href").split("/");
            String title = titleSplit[titleSplit.length -1];
            String user = imgMatcher.group(1),
                imageId = imgMatcher.group(2);
            String image = "http://pictures.hentai-foundry.com//";
            image += user.toLowerCase().charAt(0);
            image += "/" + user + "/" + imageId + "/" + user + "-" + imageId + title + ".png";
            imageURLs.add(image);
        }
        return imageURLs;
    }

    @Override
    public void downloadURL(URL url, int index) {
        addURLToDownload(url, getPrefix(index));
    }

}

@@ -2263,17 +2263,18 @@
 .size() 
-%3E
+!=
  0) %7B%0A  
@@ -3280,24 +3280,918 @@
 %0A           
+ Document imagePage;%0A            try %7B%0A                Response resp = Http.url(%22http://www.hentai-foundry.com/%22).response();%0A                cookies = resp.cookies();%0A                resp = Http.url(%22http://www.hentai-foundry.com/?enterAgree=1&size=1500%22)%0A                           .referrer(%22http://www.hentai-foundry.com/%22)%0A                           .cookies(cookies)%0A                           .response();%0A                cookies.putAll(resp.cookies());%0A%0A                logger.info(%22grabbing %22 + %22http://www.hentai-foundry.com%22 + thumb.attr(%22href%22));%0A                imagePage = Http.url(%22http://www.hentai-foundry.com%22 + thumb.attr(%22href%22)).cookies(cookies).get();%0A            %7D%0A%0A            catch (IOException e) %7B%0A                logger.debug(e.getMessage());%0A                logger.debug(%22Warning: imagePage is null!%22);%0A                imagePage = null;%0A            %7D%0A            //
  String%5B%5D ti
@@ -4235,32 +4235,35 @@
 /%22);%0A           
+ //
  String title = 
@@ -4299,32 +4299,35 @@
 -1%5D;%0A           
+ //
  String user = i
@@ -4349,32 +4349,35 @@
 (1),%0A           
+ //
      imageId = i
@@ -4403,24 +4403,27 @@
 %0A           
+ //
  String imag
@@ -4469,32 +4469,35 @@
 //%22;%0A           
+ //
  image += user.t
@@ -4527,24 +4527,27 @@
 %0A           
+ //
  image += %22/
@@ -4644,21 +4644,84 @@
 RLs.add(
-image
+%22http:%22 + imagePage.select(%22div.boxbody %3E center %3E img%22).attr(%22src%22)
 );%0A     

Fix null error, Now rips all file types