# -*- coding: utf-8 -*-
import re
from typing import Optional

from lxml import etree

from app.modules.indexer.parser import SiteParserBase, SiteSchema
from app.foundation import size as size_tools
from app.foundation import temporal as time_tools
from app.foundation import text as text_tools
from app.foundation.dom import DomUtils


class SmallHorseSiteUserInfo(SiteParserBase):
    schema = SiteSchema.SmallHorse

    def _parse_site_page(self, html_text: str):
        html_text = self._prepare_html_text(html_text)

        user_detail = re.search(r"user.php\?id=(\d+)", html_text)
        if user_detail and user_detail.group().strip():
            self._user_detail_page = user_detail.group().strip().lstrip('/')
            self.userid = user_detail.group(1)
            self._torrent_seeding_page = f"torrents.php?type=seeding&userid={self.userid}"
        self._user_traffic_page = f"user.php?id={self.userid}"

    def _parse_user_base_info(self, html_text: str):
        html_text = self._prepare_html_text(html_text)
        html = etree.HTML(html_text)
        try:
            ret = html.xpath('//a[contains(@href, "user.php")]//text()')
            if ret:
                self.username = str(ret[0])
        finally:
            if html is not None:
                del html

    def _parse_user_traffic_info(self, html_text: str):
        """
        上传/下载/分享率 [做种数/魔力值]
        :param html_text:
        :return:
        """
        html_text = self._prepare_html_text(html_text)
        html = etree.HTML(html_text)
        try:
            tmps = html.xpath('//ul[@class = "stats nobullet"]')
            if tmps:
                if tmps[1].xpath("li") and tmps[1].xpath("li")[0].xpath("span//text()"):
                    self.join_at = time_tools.normalize_datetime(tmps[1].xpath("li")[0].xpath("span//text()")[0])
                self.upload = size_tools.parse_size(str(tmps[1].xpath("li")[2].xpath("text()")[0]).split(":")[1].strip())
                self.download = size_tools.parse_size(
                    str(tmps[1].xpath("li")[3].xpath("text()")[0]).split(":")[1].strip())
                if tmps[1].xpath("li")[4].xpath("span//text()"):
                    self.ratio = text_tools.parse_float(str(tmps[1].xpath("li")[4].xpath("span//text()")[0]).replace('∞', '0'))
                else:
                    self.ratio = text_tools.parse_float(str(tmps[1].xpath("li")[5].xpath("text()")[0]).split(":")[1])
                self.bonus = text_tools.parse_float(str(tmps[1].xpath("li")[5].xpath("text()")[0]).split(":")[1])
                self.user_level = str(tmps[3].xpath("li")[0].xpath("text()")[0]).split(":")[1].strip()
                self.leeching = text_tools.parse_int(
                    (tmps[4].xpath("li")[6].xpath("text()")[0]).split(":")[1].replace("[", ""))
        finally:
            if html is not None:
                del html

    def _parse_user_detail_info(self, html_text: str):
        pass

    def _parse_user_torrent_seeding_info(self, html_text: str, multi_page: Optional[bool] = False) -> Optional[str]:
        """
         做种相关信息
         :param html_text:
         :param multi_page: 是否多页数据
         :return: 下页地址
         """
        html = etree.HTML(html_text)
        try:
            if not DomUtils.has_child_elements(html):
                return None

            size_col = 6
            seeders_col = 8

            page_seeding = 0
            page_seeding_size = 0
            page_seeding_info = []
            seeding_sizes = html.xpath(f'//table[@id="torrent_table"]//tr[position()>1]/td[{size_col}]')
            seeding_seeders = html.xpath(f'//table[@id="torrent_table"]//tr[position()>1]/td[{seeders_col}]')
            if seeding_sizes and seeding_seeders:
                page_seeding = len(seeding_sizes)

                for i in range(0, len(seeding_sizes)):
                    size = size_tools.parse_size(seeding_sizes[i].xpath("string(.)").strip())
                    seeders = text_tools.parse_int(seeding_seeders[i].xpath("string(.)").strip())

                    page_seeding_size += size
                    page_seeding_info.append([seeders, size])

            self.seeding += page_seeding
            self.seeding_size += page_seeding_size
            self.seeding_info.extend(page_seeding_info)

            # 是否存在下页数据
            next_page = None
            next_pages = html.xpath('//ul[@class="pagination"]/li[contains(@class,"active")]/following-sibling::li')
            if next_pages and len(next_pages) > 1:
                page_num = next_pages[0].xpath("string(.)").strip()
                if page_num.isdigit():
                    next_page = f"{self._torrent_seeding_page}&page={page_num}"
        finally:
            if html is not None:
                del html
        return next_page

    def _parse_message_unread_links(self, html_text: str, msg_links: list) -> Optional[str]:
        return None

    def _parse_message_content(self, html_text):
        return None, None, None
