from bs4 import BeautifulSoup
from datetime import date

from ..twosteps_extractor import TwoStepsExtractorNoPause
from ..extractor import Extractor


# A class dedicated to get events from Le Caméléon
# URL: https://lesrdvducameleon.wixsite.com/lesrdv/les-spectacles
class CExtractor(TwoStepsExtractorNoPause):
    def __init__(self):
        super().__init__()
        self.today = date.today()

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

        links = soup.select("a[data-anchor='event-details']")
        if links:
            for lk in links:
                self.add_event_url(lk["href"])

    def add_event_from_content(
        self,
        event_content,
        event_url,
        url_human=None,
        default_values=None,
        published=False,
    ):
        soup = BeautifulSoup(event_content, "html.parser")

        title = soup.select_one("[data-hook='event-title']").text
        d = soup.select_one("[data-hook='event-full-date']").text
        location = soup.select_one("[data-hook='event-full-location']").text
        description = (
            soup.select_one("[data-hook='about-section-text']").get_text(separator="\n")
            + "\n"
            + soup.select_one("[data-hook='event-description']").get_text(
                separator="\n"
            )
        )
        img = soup.select_one("[data-hook='event-image'] img")

        els = d.split(",")
        if len(els) > 1:
            start_day = Extractor.parse_french_date(
                els[0], default_year_by_proximity=self.today
            )
            end_day = start_day
            es = els[1].split("–")
            start_time = Extractor.parse_french_time(es[0])
            end_time = Extractor.parse_french_time(es[1])
        else:
            start_day = None
            start_time = None
            start_time = None
            end_time = None

        image = img["src"].split(".jpg")[0] + ".jpg"
        image_alt = img["alt"]

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