from ..twosteps_extractor import *
from bs4 import BeautifulSoup


# A class dedicated to get events from MEC Wordpress plugin
# URL: https://webnus.net/modern-events-calendar/
class CExtractor(TwoStepsExtractor):
    
    def local2agendaCategory(self, category):
        mapping = {
            "Musique": "Fêtes & Concerts",
            "CONCERT": "Fêtes & Concerts",
            "VISITE": "Visites & Expositions",
            "Spectacle": "Spectacles",
            "Rencontre": "Rencontres & Débats",
            "Atelier": "Animations & Ateliers",
            "Projection": "Cinéma",
        }
        mapping_tag = {
            "Musique": "concert",
            "CONCERT": "concert",
            "VISITE": None,
            "Spectacle": "rhéâtre",
            "Rencontre": None,
            "Atelier": "atelier",
            "Projection": None,
        }
        
        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("div.mec-tile-event-content")
        for e in events:
            link = e.select("h4.mec-event-title a")
            if len(link) == 1:
                url = link[0]["href"]
                title = link[0].get_text()
                
                if self.add_event_url(url):
                    print(url, title)
                    self.add_event_title(url, title)

                categories = e.select(".mec-label-normal")
                if len(categories) == 0:
                    categories = e.select(".mec-category")
                if len(categories) > 0:
                    category, tag = self.local2agendaCategory(categories[0].get_text())
                    if category:
                        self.add_event_category(url, category)
                    if tag:
                        self.add_event_category(url, tag)


    def add_event_from_content(
        self,
        event_content,
        event_url,
        url_human=None,
        default_values=None,
        published=False,
    ):
        soup = BeautifulSoup(event_content, "xml")
        
        start_day = soup.select(".mec-start-date-label")
        if start_day and len(start_day) > 0:
            start_day = Extractor.parse_french_date(start_day[0].get_text())
        else:
            start_day = None
        t = soup.select(".mec-single-event-time .mec-events-abbr")
        if t:
            t = t[0].get_text().split("-")
            start_time = Extractor.parse_french_time(t[0])
            if len(t) > 1:
                end_time = Extractor.parse_french_time(t[1])
            else:
                end_time = None
        else:
            start_time = None
            end_time = None
            
        image = soup.select(".mec-events-event-image img")
        if image:
            image = image[0]["src"]
        else:
            image = None
        description = soup.select(".mec-event-content .mec-single-event-description")[0].get_text(separator=" ")

        url_human = event_url

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