package com.rarchives.ripme.ripper.rippers;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.rarchives.ripme.ripper.AbstractRipper;
import com.rarchives.ripme.ripper.DownloadThreadPool;

public class MotherlessRipper extends AbstractRipper {

    private static final String DOMAIN = "motherless.com",
                                HOST   = "motherless";
    private static final Logger logger = Logger.getLogger(MotherlessRipper.class);

    private DownloadThreadPool motherlessThreadPool;

    public MotherlessRipper(URL url) throws IOException {
        super(url);
        motherlessThreadPool = new DownloadThreadPool();
    }

    @Override
    public boolean canRip(URL url) {
        return url.getHost().endsWith(DOMAIN);
    }

    @Override
    public String getHost() {
        return HOST;
    }

    @Override
    public URL sanitizeURL(URL url) throws MalformedURLException {
        String gid = getGID(url);
        URL newURL = new URL("http://motherless.com/G" + gid);
        logger.debug("Sanitized URL from " + url + " to " + newURL);
        return newURL;
    }

    @Override
    public String getGID(URL url) throws MalformedURLException {
        Pattern p = Pattern.compile("^https?://(www\\.)?motherless\\.com/G([A-Z0-9]{6,8}).*$");
        Matcher m = p.matcher(url.toExternalForm());
        if (!m.matches()) {
            throw new MalformedURLException("Expected URL format: http://motherless.com/GXXXXXXXX");
        }
        return m.group(m.groupCount());
    }

    @Override
    public void rip() throws IOException {
        int index = 0;
        logger.info("    Retrieving " + this.url.toExternalForm());
        Document doc = Jsoup.connect(this.url.toExternalForm())
                            .userAgent(USER_AGENT)
                            .get();
        for (Element thumb : doc.select("div.thumb a.img-container")) {
            URL url = new URL("http://" + DOMAIN + thumb.attr("href"));
            index += 1;
            // Create thread for finding image at "url" page
            MotherlessImageThread mit = new MotherlessImageThread(url, index);
            motherlessThreadPool.addThread(mit);
        }
        motherlessThreadPool.waitForThreads();
        waitForThreads();
    }

    /**
     * Helper class to find and download images found on "image" pages
     */
    private class MotherlessImageThread extends Thread {
        private URL url;
        private int index;

        public MotherlessImageThread(URL url, int index) {
            super();
            this.url = url;
            this.index = index;
        }

        @Override
        public void run() {
            try {
                Document doc = Jsoup.connect(this.url.toExternalForm())
                                    .userAgent(USER_AGENT)
                                    .get();
                Pattern p = Pattern.compile("^.*__fileurl = '([^']{1,})';.*$", Pattern.DOTALL);
                Matcher m = p.matcher(doc.outerHtml());
                if (m.matches()) {
                    String file = m.group(1);
                    addURLToDownload(new URL(file), String.format("%03d_", index));
                } else {
                    logger.warn("[!] could not find '__fileurl' at " + url);
                }
            } catch (IOException e) {
                logger.error("[!] Exception while loading/parsing " + this.url, e);
            }
        }
    }

}

@@ -1481,11 +1481,15 @@
 /G(%5B
+VI%5D%5B
 A-
-Z
+F
 0-9%5D
@@ -1675,17 +1675,30 @@
 om/G
+I
 XXXXXXX
-X%22
+, got: %22 + url
 );%0A 
@@ -1835,78 +1835,164 @@
  = 0
-;%0A        logger.info(%22    Retrieving %22 + this.url.toExternalForm());%0A
+, page = 1;%0A        String nextURL = this.url.toExternalForm();%0A        while (nextURL != null) %7B%0A            logger.info(%22    Retrieving %22 + nextURL);%0A    
     
@@ -2028,43 +2028,17 @@
 ect(
-this.url.toExternalForm())%0A        
+nextURL)%0A
     
@@ -2096,32 +2096,24 @@
         
-        
 .get();%0A
         
@@ -2096,32 +2096,36 @@
         .get();%0A
+    
         for (Ele
@@ -2188,24 +2188,28 @@
             
+    
 URL url = ne
@@ -2268,16 +2268,20 @@
         
+   
+ 
 index +=
@@ -2280,24 +2280,28 @@
 index += 1;%0A
+    
             
@@ -2361,16 +2361,20 @@
         
+  
+  
 Motherle
@@ -2436,32 +2436,36 @@
 x);%0A            
+    
 motherlessThread
@@ -2485,16 +2485,244 @@
 d(mit);%0A
+            %7D%0A            // Next page%0A            nextURL = null;%0A            page++;%0A            if (doc.html().contains(%22?page=%22 + page)) %7B%0A                nextURL = this.url.toExternalForm() + %22?page=%22 + page;%0A            %7D%0A
         

Support multi-page motherless albums