import re

from calibre.web.feeds.recipes import BasicNewsRecipe


class JournalofHospitalMedicine(BasicNewsRecipe):

    title = 'Journal of Hospital Medicine'
    __author__ = 'Kovid Goyal'
    description = 'Medical news'
    timefmt = ' [%d %b, %Y]'
    needs_subscription = True
    language = 'en'

    no_stylesheets = True
    keep_only_tags = [dict(id=['articleTitle', 'articleMeta', 'fulltext'])]
    remove_tags = [dict(attrs={'class': 'licensedContent'})]

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        br.open('https://onlinelibrary.wiley.com/')
        br.select_form(nr=0)
        br['login'] = self.username
        br['password'] = self.password
        response = br.submit()
        raw = response.read()
        if '<h2>LOGGED IN</h2>' not in raw:
            raise Exception('Login failed. Check your username and password')
        return br

    # TO GET ARTICLE TOC
    def johm_get_index(self):
        return self.index_to_soup('https://shmpublications.onlinelibrary.wiley.com/toc/15535606/current')

    # To parse article toc
    def parse_index(self):
        soup = self.johm_get_index()
        toc = soup.find(id='issueTocGroups')
        feeds = []
        for group in toc.findAll('li', id=re.compile(r'group\d+')):
            gtitle = group.find(attrs={'class': 'subSectionHeading'})
            if gtitle is None:
                continue
            gtitle = self.tag_to_string(gtitle)
            arts = group.find(attrs={'class': 'articles'})
            if arts is None:
                continue
            self.log('Found section:', gtitle)
            articles = []
            for art in arts.findAll(attrs={'class': lambda x: x and 'tocArticle'
                                           in x}):
                a = art.find('a', href=True)
                if a is None:
                    continue
                url = a.get('href')
                if url.startswith('/'):
                    url = 'http://onlinelibrary.wiley.com' + url
                url = url.replace('/abstract', '/full')
                title = self.tag_to_string(a)
                a.extract()
                pm = art.find(attrs={'class': 'productMenu'})
                if pm is not None:
                    pm.extract()
                desc = self.tag_to_string(art)
                self.log('\tFound article:', title, 'at', url)
                articles.append({'title': title, 'url': url, 'description': desc,
                                 'date': ''})
            if articles:
                feeds.append((gtitle, articles))

        return feeds
