# -*- coding: utf-8 -*-
# Copyright (C) 2004-2008 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

import re

from ..scraper import _ParserScraper
from ..helpers import bounceStarter


class _WLPComics(_ParserScraper):
    imageSearch = '//center/*/img[contains(@alt, " Comic")]'
    prevSearch = '//a[contains(text(), "Previous ")]'
    nextSearch = '//a[contains(text(), "Next ")]'
    starter = bounceStarter
    help = 'Index format: nnn'

    def __init__(self, name):
        super(_WLPComics, self).__init__('WLP/' + name)

    def namer(self, image_url, page_url):
        return (page_url.rsplit('/', 1)[-1].split('.')[0] + '_' +
                image_url.rsplit('/', 1)[-1])

    def getIndexStripUrl(self, index):
        return self.url + '%s.html' % index


class ChichiChan(_WLPComics):
    url = 'http://www.wlpcomics.com/adult/chichi/'
    adult = True


class ChocolateMilkMaid(_WLPComics):
    # Newer pages seem to be broken
    baseurl = 'http://www.wlpcomics.com/adult/cm/'
    url = baseurl + '264.html'
    adult = True

    def getIndexStripUrl(self, index):
        return self.baseurl + '%s.html' % index

    def link_modifier(self, fromurl, tourl):
        """Bugfix for self-referencing pages..."""
        if tourl == fromurl:
            return re.sub(r'/(\d+)\.ht',
                          lambda m: '/%03i.ht' % (int(m.group(1)) - 1), tourl)
        if '263.html' in fromurl and '265.html' in tourl:
            return self.baseurl + '264.html'
        return tourl


class MaidAttack(_WLPComics):
    url = 'http://www.wlpcomics.com/general/maidattack/'


class PeterIsTheWolfAdult(_WLPComics):
    url = 'http://www.peteristhewolf.com/adult/home.html'
    adult = True


class PeterIsTheWolfGeneral(_WLPComics):
    url = 'http://www.peteristhewolf.com/general/'


class Stellar(_WLPComics):
    url = 'http://www.wlpcomics.com/adult/stellar/'
    adult = True

    def link_modifier(self, fromurl, tourl):
        """Bugfix for empty page..."""
        if tourl == self.url + '075.html':
            return self.url + '074.html'
        return tourl

@@ -1767,33 +1767,38 @@
 WLPComics):%0A    
-u
+stripU
 rl = 'http://www
@@ -1827,129 +1827,999 @@
 ult/
-home.html'%0A    adult = True%0A%0A%0Aclass PeterIsTheWolfGeneral(_WLPComics):%0A    url = 'http://www.peteristhewolf.com/general/'
+%25s.html'%0A    url = stripUrl %25 'home'%0A    firstStripUrl = stripUrl %25 '001'%0A    multipleImagesPerStrip = True%0A    adult = True%0A%0A    def getPrevUrl(self, url, data):%0A        # Fix loop in site navigation%0A        if url == self.stripUrl %25 '194':%0A            return self.stripUrl %25 '193'%0A        return super(PeterIsTheWolfAdult, self).getPrevUrl(url, data)%0A%0A%0Aclass PeterIsTheWolfGeneral(_WLPComics):%0A    url = 'http://www.peteristhewolf.com/general/'%0A    stripUrl = url + '%25s.html'%0A    firstStripUrl = stripUrl %25 '001'%0A%0A    def getPrevUrl(self, url, data):%0A        # Fix loops in site navigation%0A        if url == self.stripUrl %25 '406':%0A            return self.stripUrl %25 '405'%0A        if url == self.stripUrl %25 '230':%0A            return self.stripUrl %25 '229'%0A        if url == self.stripUrl %25 '229':%0A            return self.stripUrl %25 '228'%0A        if url == self.stripUrl %25 '153':%0A            return self.stripUrl %25 '152'%0A        return super(PeterIsTheWolfGeneral, self).getPrevUrl(url, data)
 %0A%0A%0Ac

Fix WLP/PeterIsTheWolf