from bs4 import BeautifulSoup


def text_card_factory(title: str, content: str, image_link: str = ""):
    """
    Factory for text cards (the normal cards seen).

    Args:
        title (str): Title for the text card.
        content (str): The text content of the card, the meat of it. Automatically formats lines into a list.
        image_link (str, optional): The image on the slide. Defaults to "".

    Returns:
        _type_: the Beautifulsoup text card tag.
    """
    soup = BeautifulSoup()
    card_tag = soup.new_tag("div")
    title_tag = soup.new_tag("h1")
    title_tag.string = title

    fixed_content = "<ul>"
    for line in content.splitlines():
        fixed_content += "<li>" + line + "</li>"
    fixed_content += "</ul>"
    text_tag = BeautifulSoup(fixed_content, "html.parser")

    card_tag = soup.new_tag("div")

    card_tag.append(title_tag)
    content_table_tag = soup.new_tag("table")
    table_row = soup.new_tag("tr")

    text_table_data = soup.new_tag("td")
    text_table_data.append(text_tag)

    table_row.append(text_table_data)
    if image_link:
        # so that it correctly shares space with the image
        text_table_data["width"] = "50%"
        image_tag = soup.new_tag(
            "img", **{"class": "center", "src": image_link, "width": "50%"}
        )
        img_td = soup.new_tag("td")
        img_td.append(image_tag)
        table_row.append(img_td)

    content_table_tag.append(table_row)

    card_tag.append(content_table_tag)
    return card_tag