package lou.arane.handlers;

import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lou.arane.core.Context;
import lou.arane.core.Handler;
import lou.arane.util.Uri;
import lou.arane.util.Util;

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

/**
 * Download manga from kissmanga site.
 *
 * @author LOU
 */
public class KissMangaHandler implements Handler {

    private static final String BASE_URI = "http://kissmanga.com/";

    /** Patterns of images embeded in javascript element */
    private static final Pattern IMAGE_PATTERN = Pattern.compile(
    		"lstImages\\.push\\(\"(.+)\"\\);"
    		);

	private final Context ctx;

	public KissMangaHandler(Context context) {
		this.ctx = context;
    }

	@Override
	public boolean canRun() {
		//domain must match
		String url = ctx.source.toString();
		return url.startsWith(BASE_URI);
	}

	@Override
	public void doRun() {
		ctx.downloadChapterList();
		downloadChapters();
		downloadImages();
	}

    /**
     * Download chapter pages by extracting their urls from the master html file
     */
    private void downloadChapters() {
        Document chapters = Util.parseHtml(ctx.chapterList, BASE_URI);
        for (Element chapterAddr : chapters.select("table[class=listing] a[href]")) {
            Uri chapterUri = new Uri(chapterAddr.absUrl("href"));
            String chapterName = Util.join(chapterUri.getFilePath(), "_");
            if (!chapterName.endsWith(".html")) chapterName += ".html";
            Path chapterPath = ctx.chaptersDir.resolve(chapterName);
            ctx.add(chapterUri, chapterPath);
        }
        ctx.download();
    }

    /**
     * Download images for each chapter. The image urls are extracted from
     * javascript's content using regex.
     */
    private void downloadImages() {
        for (Path chapterHtml : Util.findHtmlFiles(ctx.chaptersDir)) {
            String chapterName = chapterHtml.getFileName().toString();
            chapterName = Util.removeFileExtension(chapterName);
            Document chapter = Util.parseHtml(chapterHtml);
            for (Element script : chapter.select("script[type=text/javascript]")) {
                Matcher matcher = IMAGE_PATTERN.matcher(script.html());
                while (matcher.find()) {
                    Uri imageUri = new Uri(matcher.group(1));
                    String imageName = chapterName + "_" + imageUri.getFileName();
                    imageName = Util.padNumericSequences(imageName, 3);
                    Path imagePath = ctx.imagesDir.resolve(imageName);
                    ctx.add(imageUri, imagePath);
                }
            }
        }
        ctx.download();
    }
}

@@ -298,16 +298,347 @@
 ement;%0A%0A
+/** FIXME%0A * Find out why downloading from kissmanga failed.%0A * url = http://kissmanga.com/Manga/Lighter%0A * %3Cp%3E%0A * The error code is 403, which means the site refuses to handle the requests.%0A * This is probably some security measure.%0A * Need time to find out how to bypass this.%0A * %3Cp%3E%0A * Maybe making a fake browser request?%0A */%0A%0A
 /**%0A * D

Add a task for later