import os
import re

import requests

from bs4 import BeautifulSoup
from lxml import etree

from src.exceptions.download_exceptions import FileAlreadyExistException
from src.parsers.base_parser import BaseParser


class OkParser(BaseParser):
    BASE_DIR = os.path.abspath(f"downloads/Odnoklassniki")

    def get_video_link(self):
        try:
            resp = requests.get(self.params["link"])
            resp.encoding = self.BASE_ENCODING
            soup = BeautifulSoup(resp.text, 'lxml')
            if "topic" in self.params["link"]:
                dom = etree.HTML(str(soup))
                elements_with_video_id = dom.xpath(
                    "//div[@class='mlr_cnt']/div[contains(@data-l, 'gA,VIDEO,mB,movie,ti,')]/div[@class='vid-card "
                    "vid-card__xl']/div[@class='video-card_n-w']/a[contains(@onclick, 'OK.VideoPlayer.openMovie')]")
                links = ["https://ok.ru/video/" + re.findall('\d+', elem.get("onclick"))[0] for elem in elements_with_video_id]
            else:
                required_div = [div for div in soup.find_all('div', {'class': 'invisible'}) if len(div['class']) < 2][0]
                video_tags = required_div.find('span').find_all_next('span', {'itemprop': "video"})
                links = [video_tag.find('a').get("href") for video_tag in video_tags]
            return links
        except Exception as ex:
            raise

    def video_download(self):
        base_link = self.params["link"]
        links = self.get_video_link()
        file_paths = []
        for link in links:
            try:
                self.params["link"] = link
                file_path = super().video_download()
                file_paths.append(file_path)
            except FileAlreadyExistException as ex:
                file_paths.append(ex.message)
                continue
        self.params["link"] = base_link
        return file_paths

