import urllib.parse
from pathlib import Path

import pandas

from UltraStarSongFileParser import UltraStarSongFileParser
import requests
from bs4 import BeautifulSoup


class TXTDownloader:
    def __init__(self, sessid: str, outdir: Path, cache: bool):
        self.sessid = sessid
        self.outdir = outdir
        self.txtparser = UltraStarSongFileParser()
        self.cache = cache

    def download(self, url: str, artist: str, song: str, spotify_uri: str, gap: str, video_gap: str, start: str, end: str, language: str, year: str):
        if (Path(self.outdir) / f"{artist} - {song}.txt").is_file() and self.cache:
            return

        url = url.replace("detail", "gettxt")

        with requests.Session() as s:
            page = s.post(url, data={'wd': 1}, cookies={'PHPSESSID': self.sessid})
            soup = BeautifulSoup(page.content, 'html.parser')
            txt = soup.find(name="textarea").text

            Path(self.outdir).mkdir(parents=True, exist_ok=True)
            with open(Path(self.outdir) / f"{artist} - {song}.txt", "w", encoding="utf-8") as f:
                f.write(txt)

            self.fix_file(Path(self.outdir) / f"{artist} - {song}.txt", artist, song, spotify_uri, gap, video_gap, start, end, language, year)

    def fix_file(self, file: Path, artist: str, song: str, spotify_uri: str, gap: str, video_gap: str, start: str, end: str, language: str, year: str):
        txt = self.txtparser.parse_file(str(file), encoding="utf-8")
        txt.mp3 = str(file.with_suffix(".mp3").relative_to(file.parent))
        txt.video = str(file.with_suffix(".mp4").relative_to(file.parent))
        txt.cover = str(file.with_suffix(".jpg").relative_to(file.parent))

        if type(gap) == str and gap != " ":
            txt.gap = gap

        if type(video_gap) == str and video_gap != " ":
            txt.videogap = video_gap

        if type(start) == str and start != " ":
            txt.start = start

        if type(end) == str and end != " ":
            txt.end = end

        if type(language) == str and language != " ":
            txt.language = language

        if type(artist) == str and artist != " ":
            txt.artist = artist

        if type(song) == str and song != " ":
            txt.title = song

        if type(year) == str and year != " ":
            txt.year = year

        txt.songid = spotify_uri

        txt.dump(file)