from ..twosteps_extractor import *
from bs4 import BeautifulSoup

# A class dedicated to get events from Le Fotomat'
# URL: https://www.lefotomat.com/
class CExtractor(TwoStepsExtractor):
    nom_lieu = "Le Fotomat'"

    def category_fotomat2agenda(self, category):
        if not category:
            return None
        mapping = {"Concerts": "Fêtes & Concerts"}
        mapping_tag = {"Concerts": "🎵 concert"}
        if category in mapping:
            return mapping[category], mapping_tag[category]
        else:
            return None, None

    def build_event_url_list(self, content):
        soup = BeautifulSoup(content, "xml")

        events = soup.select("item")
        for e in events:
            e_url = e.find("link").contents[0]
            self.add_event_url(e_url)

            title = e.find("title").contents[0]
            self.add_event_title(e_url, title)

            category, tag = self.category_fotomat2agenda(e.find("category").contents[0])
            if category:
                self.add_event_category(e_url, category)
            if tag:
                self.add_event_tag(e_url, tag)

    def add_event_from_content(
        self,
        event_content,
        event_url,
        url_human=None,
        default_values=None,
        published=False,
    ):
        soup = BeautifulSoup(event_content, "html.parser")
        image = soup.select("div.post-content img.wp-post-image")
        if image:
            image = image[0]["src"]
        else:
            image = None
        desc = soup.select("head meta[name=description]")[0]["content"]
        start_day = Extractor.parse_french_date(desc.split("-")[0])
        start_time = Extractor.parse_french_time(desc.split("-")[1])
        end_time = Extractor.parse_french_time(desc.split("-")[2])
        end_day = Extractor.guess_end_day(start_day, start_time, end_time)

        location = self.nom_lieu
        descriptions = soup.select("div.vce-col-content")
        if descriptions:
            descriptions = [d.get_text() for d in descriptions]
            description = max(descriptions, key=len)
        else:
            description = None

        article = soup.select("article.post")
        tags = []
        for c in article[0]["class"]:
            if c.startswith("category-"):
                tag = "-".join(c.split("-")[1:])
                if tag != "concerts":
                    tags.append(tag)

        url_human = event_url

        self.add_event_with_props(
            default_values,
            event_url,
            None,
            None,
            start_day,
            location,
            description,
            tags,
            recurrences=None,
            uuids=[event_url],
            url_human=url_human,
            start_time=start_time,
            end_day=end_day,
            end_time=end_time,
            published=published,
            image=image,
        )
