package ch.zomg.mangaroll.query;

import com.google.gson.Gson;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URI;

/**
 * Created by Simon on 2016/03/30.
 */
public abstract class Fetcher {
    private boolean hasMore = true;
    private String thumb = "";
    private String name = "";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getThumb() {
        return thumb;
    }

    public void setThumb(String thumb) {
        this.thumb = thumb;
    }

    public boolean hasMore() {
        return hasMore;
    }

    public abstract boolean isContainerProvider();

    protected void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public String getID() {
        return name == null ? "" : name;
    }

    public static Fetcher getInitialFetcher(String descriptorString) {
        Gson gson = new Gson();
        Descriptor descriptor = gson.fromJson(descriptorString, Descriptor.class);
        if (descriptor != null) {
            if (descriptor.getType() == Descriptor.Type.CONTAINER) {
                return new ContainerFetcher(descriptor);
            } else if(descriptor.getType() == Descriptor.Type.MANGA) {
                return new MangaFetcher(descriptor);
            }
        }
        return null;
    }

    protected String extractURL(URI base, Elements element) {
        return element.size() > 0 ? extractURL(base, element.get(0)) : "";

    }
    protected String extractURL(URI base, Element element) {
        String result = element.attr("href");
        if(result.isEmpty()) {
            result = element.attr("src");
        }

        if(!result.isEmpty()) {
            return base.resolve(result).toString();
        }

        return result;
    }

    protected String extractName(Elements elements) {
        String result = "";
        if(elements.size() > 0) {
            result = elements.get(0).text().trim();
        }
        return result;
    }
}

@@ -27,16 +27,42 @@
 query;%0A%0A
+import android.util.Log;%0A%0A
 import c
@@ -82,16 +82,16 @@
 n.Gson;%0A
-
 %0Aimport 
@@ -170,16 +170,80 @@
 net.URI;
+%0Aimport java.util.regex.Pattern;%0Aimport java.util.regex.Matcher;
 %0A%0A/**%0A *
@@ -374,24 +374,24 @@
 thumb = %22%22;%0A
-
     private 
@@ -403,24 +403,108 @@
 g name = %22%22;
+%0A    private static Pattern PATTERN_CSS_URL = Pattern.compile(%22url%5C%5C('?(.+?)'?%5C%5C)%22);
 %0A%0A    public
@@ -1886,32 +1886,248 @@
 c%22);%0A        %7D%0A%0A
+        if(result.isEmpty()) %7B%0A            Matcher matcher = PATTERN_CSS_URL.matcher(element.attr(%22style%22));%0A            if(matcher.find()) %7B%0A                result = matcher.group(1).trim();%0A            %7D%0A        %7D%0A
         if(!resu

Add support for CSS background thumbs.