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.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

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

public class MeituriRipper extends AbstractHTMLRipper {
    public MeituriRipper(URL url) throws IOException {
        super(url);
    }

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

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

    // To use in getting URLs
    String albumID = "";

    @Override
    public String getGID(URL url) throws MalformedURLException {
        // without escape
        // ^https?://[w.]*meituri\.com/a/([0-9]+)/([0-9\.html]+)*$
        // https://www.meituri.com/a/14449/
        // also matches https://www.meituri.com/a/14449/3.html etc.
        // group 1 is 14449
        Pattern p = Pattern.compile("^https?://[w.]*meituri\\.com/a/([0-9]+)/([0-9\\.html]+)*$");
        Matcher m = p.matcher(url.toExternalForm());
        if (m.matches()) {
            albumID = m.group(1);
            return m.group(1);
        }
        throw new MalformedURLException(
                "Expected meituri.com URL format: " + "meituri.com/a/albumid/ - got " + url + "instead");
    }

    @Override
    public Document getFirstPage() throws IOException {
        return Http.url(url).get();
    }

    @Override
    public List<String> getURLsFromPage(Document doc) {
        List<String> imageURLs = new ArrayList<>();
        // Get number of images from the page
        // Then generate links according to that
        String numOfImages = "";
        // A very ugly way of getting "图片数量： 55P" from paragraphs
        // 3rd p in div.tuji
        int n = 0;
        for (Element para : doc.select("div.tuji > p")) {
            // 图片数量： 55P
            if (n == 2) {
                numOfImages = para.toString();
            }
            n++;
        }
        // ["<p>图片数量：", "55P</p>"]
        String[] splitNumOfImages = numOfImages.split(" ");
        // "55P</p>" -> "55" -> 55
        int actualNumOfImages = Integer.parseInt(splitNumOfImages[1].replace("P</p>", ""));

        // Base URL: http://ii.hywly.com/a/1/albumid/imgnum.jpg
        String baseURL = "http://ii.hywly.com/a/1/" + albumID + "/";

        // Loop through and add images to the URL list
        for (int i = 1; i <= actualNumOfImages; i++) {
            imageURLs.add(baseURL + i + ".jpg");
        }
        return imageURLs;
    }

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

@@ -916,24 +916,24 @@
 +)/(%5B0-9
+%5D+
 %5C.html
-%5D+
 )*$%0A    
@@ -1154,17 +1154,17 @@
 %5B0-9
+%5D+
 %5C%5C.html
-%5D+
 )*$%22
@@ -1813,22 +1813,19 @@
         
-Str
 in
-g
+t
  numOfIm
@@ -1831,18 +1831,17 @@
 mages = 
-%22%22
+1
 ;%0A      
@@ -1846,112 +1846,63 @@
     
-// A very ugly way of getting %22%E5%9B%BE%E7%89%87%E6%95%B0%E9%87%8F%EF%BC%9A 55P%22 from paragraphs%0A        // 3rd p in div.tuji%0A        int n = 0
+Pattern p = Pattern.compile(%22%5E%3Cp%3E%E5%9B%BE%E7%89%87%E6%95%B0%E9%87%8F%EF%BC%9A (%5B0-9%5D+)P%3C/p%3E$%22)
 ;%0A  
@@ -1972,16 +1972,19 @@
      // 
+%3Cp%3E
 %E5%9B%BE%E7%89%87%E6%95%B0%E9%87%8F%EF%BC%9A 55
@@ -1984,16 +1984,20 @@
 %E7%89%87%E6%95%B0%E9%87%8F%EF%BC%9A 55P
+%3C/p%3E
 %0A       
@@ -2005,52 +2005,30 @@
     
-if (n == 2) %7B%0A                numOfImages = 
+Matcher m = p.matcher(
 para
@@ -2038,16 +2038,17 @@
 String()
+)
 ;%0A      
@@ -2049,27 +2049,43 @@
             
-%7D%0A 
+if (m.matches()) %7B%0A
            n
@@ -2086,23 +2086,19 @@
     
- n++;%0A
       
-  %7D
+// 55
 %0A   
@@ -2106,221 +2106,83 @@
     
-// %5B%22%3Cp%3E%E5%9B%BE%E7%89%87%E6%95%B0%E9%87%8F%EF%BC%9A%22, %2255P%3C/p%3E%22%5D%0A        String%5B%5D splitNumOfImages = numOfImages.split(%22 %22);%0A        // %2255P%3C/p%3E%22 -%3E %2255%22 -%3E 55%0A        int actualNumOfImages = Integer.parseInt(splitNumOfImages%5B1%5D.replace(%22P%3C/p%3E%22, %22%22));
+        numOfImages = Integer.parseInt(m.group(1));%0A            %7D%0A        %7D
 %0A%0A  
@@ -2401,15 +2401,9 @@
  %3C= 
-actualN
+n
 umOf

Fix not downloading if numOfImgs were on 2nd row