# -*- coding: utf-8 -*-
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012-2014 Bastian Kleineidam
# Copyright (C) 2015-2016 Tobias Gruetzmacher

from __future__ import absolute_import, division, print_function

from re import compile, escape
from ..scraper import _BasicScraper
from ..util import tagre
from ..helpers import bounceStarter
from .common import _WordPressScraper


class HagarTheHorrible(_BasicScraper):
    url = 'http://www.hagarthehorrible.net/'
    stripUrl = 'http://www.hagardunor.net/comicstrips_us.php?serietype=9&colortype=1&serieno=%s'
    firstStripUrl = stripUrl % '1'
    multipleImagesPerStrip = True
    imageSearch = compile(tagre("img", "src", r'(stripus\d+/(?:Hagar_The_Horrible_?|h)\d+[^ >]+)', quote=""))
    prevUrl = r'(comicstrips_us\.php\?serietype\=9\&colortype\=1\&serieno\=\d+)'
    prevSearch = compile(tagre("a", "href", prevUrl, after="Previous"))
    help = 'Index format: number'

    @classmethod
    def starter(cls):
        """Return last gallery link."""
        url = 'http://www.hagardunor.net/comics.php'
        data = cls.getPage(url)
        pattern = compile(tagre("a", "href", cls.prevUrl))
        for starturl in cls.fetchUrls(url, data, pattern):
            pass
        return starturl


class HappyJar(_WordPressScraper):
    url = 'http://www.happyjar.com/'


class HarkAVagrant(_BasicScraper):
    url = 'http://www.harkavagrant.com/'
    rurl = escape(url)
    starter = bounceStarter(
        url, compile(tagre("a", "href", r'(%sindex\.php\?id=\d+)' % rurl) +
                     tagre("img", "src", "buttonnext.png")))
    stripUrl = url + 'index.php?id=%s'
    firstStripUrl = stripUrl % '1'
    imageSearch = compile(tagre("img", "src", r'(%s[^"]+)' % rurl,
                                after='BORDER'))
    prevSearch = compile(tagre("a", "href", r'(%sindex\.php\?id=\d+)' % rurl) +
                         tagre("img", "src", "buttonprevious.png"))
    help = 'Index format: number'

    @classmethod
    def namer(cls, imageUrl, pageUrl):
        filename = imageUrl.rsplit('/', 1)[1]
        num = pageUrl.rsplit('=', 1)[1]
        return '%s-%s' % (num, filename)


class Hipsters(_WordPressScraper):
    url = 'http://www.hipsters-comic.com/'
    firstStripUrl = 'http://www.hipsters-comic.com/comic/hip01/'


class HorribleVille(_BasicScraper):
    url = 'http://horribleville.com/'
    stripUrl = url + 'd/%s.html'
    firstStripUrl = stripUrl % '20051220'
    imageSearch = compile(tagre("img", "src", r'(/comics/[^"]+)'))
    prevSearch = compile(tagre("a", "href", r'(/d/[^"]+)') +
                         tagre("img", "src", r'/images/previous\.png'))
    help = 'Index format: yyyymmdd'

@@ -1280,22 +1280,54 @@
 rturl%0A%0A%0A
+# %22Hiatus%22, navigation missing%0A
 class 
+_
 HappyJar
@@ -2356,391 +2356,4 @@
 1/'%0A
-%0A%0Aclass HorribleVille(_BasicScraper):%0A    url = 'http://horribleville.com/'%0A    stripUrl = url + 'd/%25s.html'%0A    firstStripUrl = stripUrl %25 '20051220'%0A    imageSearch = compile(tagre(%22img%22, %22src%22, r'(/comics/%5B%5E%22%5D+)'))%0A    prevSearch = compile(tagre(%22a%22, %22href%22, r'(/d/%5B%5E%22%5D+)') +%0A                         tagre(%22img%22, %22src%22, r'/images/previous%5C.png'))%0A    help = 'Index format: yyyymmdd'%0A

Remove some broken comics.