from typing import List, Tuple

import requests
from bs4 import BeautifulSoup

from .source import Source
from ...models import Chapter, Novel, Metadata


class ScribbleHub(Source):
    __name__ = 'Scribble Hub'
    base_urls = ['https://www.scribblehub.com']

    def novel(self, url: str) -> Tuple[Novel, List[Chapter], List[Metadata]]:
        soup = self.soup(url)

        synopsis_paragraphs = [element.text for element in soup.find('div', {'class': 'wi_fic_desc'}).find_all('p')]

        novel = Novel(
            title=soup.find('div', {'class': 'fic_title'}).text.strip(),
            author=soup.find('span', {'class': 'auth_name_fic'}).text.strip(),
            synopsis='\n'.join(synopsis_paragraphs),
            thumbnail_url=soup.find('div', {'class': 'fic_image'}).find('img')['src'],
            url=url
        )

        id_ = int(url.split('/')[4])
        chapters = self.parse_toc(id_)

        return novel, chapters, []

    def chapter(self, chapter: Chapter):
        soup = self.soup(chapter.url)

        chapter.title = soup.select_one('.chapter-title').text.strip()
        chapter.paragraphs = str(soup.select_one('#chp_raw'))

    def parse_toc(self, id_: int) -> List[Chapter]:

        response = requests.post(
            'https://www.scribblehub.com/wp-admin/admin-ajax.php',
            data={
                'action': 'wi_gettocchp',
                'strSID': id_,
            },
        )

        soup = BeautifulSoup(response.content, 'lxml')
        chapter_elements = soup.find_all('li')

        chapters = []
        for i, element in enumerate(reversed(chapter_elements)):
            a = element.find('a')

            chapter = Chapter(
                index=i,
                title=a.text.strip(),
                url=a['href']
            )

            chapters.append(chapter)

        return chapters
