from ..twosteps_extractor import *
from bs4 import BeautifulSoup
from datetime import datetime
from urllib.parse import urlparse

# A class dedicated to get events from Raymond Bar
# URL: https://www.raymondbar.net/
class CExtractor(TwoStepsExtractorNoPause):

    def __init__(self):
        super().__init__()

    def guess_category(self, category):
        if "Cinéma" in category:
            return "Cinéma"
        if "Conférence" in category or "Rencontres" in category:
            return "Rencontres & débats"
        if "Lecture" in category or "Conte" in category:
            return "Spectacles"
        if "Atelier" in category or "Jeux" in category or "":
            return "Animations & Ateliers"
        if "Numérique" in category:
            return "Rendez-vous locaux"

        return "Sans catégorie"


    def guess_tags_from_category(self, category):
        tags = []
        if "Lecture" in category:
            tags.append("📖 lecture")
        if "Jeux" in category:
            tags.append("🎲 jeux")

        return tags

    def build_event_url_list(self, content, infuture_days=180):
        
        soup = BeautifulSoup(content, "html.parser")

        root_address_human = self.url_human.split('?')[0]
        root_address = self.url.split('Service')[0]

        items = soup.select("li.listItem")
        if items:
            for item in items:
                elems = item["onclick"].split('"')
                v = elems[3].split('^')[1]
                contentItem = elems[1]
                multidate = item.select_one('.until.maindate').text != ''
                if not multidate:
                    url_human = root_address_human + '?p=*&v=' + v + "#contentitem=" + contentItem
                    url = 	root_address + 'Service.PubItem.cls?action=get&instance=*&uuid=' + contentItem
                    self.add_event_url(url)
                    self.add_event_url_human(url, url_human)


    def add_event_from_content(
        self,
        event_content,
        event_url,
        url_human=None,
        default_values=None,
        published=False,
    ):

        root_address_human = "https://" + urlparse(self.url_human).netloc + "/"

        soup = BeautifulSoup(event_content, "xml")

        
        title = soup.select_one("Title").text
        content = soup.select_one("Content").text

        soup = BeautifulSoup(content, "html.parser")

        image = root_address_human + soup.select_one(".image img")["src"]
        description = soup.select_one(".rightcolumn .content").text
        location = soup.select_one(".infos .location").text
        public = soup.select_one(".infos .public").text
        start_day = Extractor.parse_french_date(soup.select_one(".infos .date .from").text)
        start_time = Extractor.parse_french_time(soup.select_one(".infos .date .time").text)
        acces = soup.select_one(".infos .acces").text
        category = soup.select_one(".rightcolumn .category").text
        infos = soup.select_one('.infos').text

        description = description + "\n" + infos

        tags = self.guess_tags_from_category(category)
        category = self.guess_category(category)
        if "Tout-petits" in public or "Jeunesse" in public:
            tags.append("🎈 jeune public")
        if "Accès libre" in acces:
            tags.append("💶 gratuit")

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