from urllib.parse import urlparse, urlunparse

from bs4 import BeautifulSoup

from wagtail import blocks


class CoyoteEmbedBlockValue(blocks.StructValue):
    def auto_play(self):
        video_soup = BeautifulSoup(str(self.get("video")), "html.parser")

        parsed_url = urlparse(video_soup.iframe["src"])
        embed_id = parsed_url.path.split("/")[-1]
        parsed_url = parsed_url._replace(
            query="rel=0&autoplay=1&loop=1&showinfo=0&mute=1&playlist=" + embed_id
        )

        video_soup.iframe["src"] = urlunparse(parsed_url)
        video_soup.iframe["autoplay"] = "1"
        video_soup.iframe["frameborder"] = "0"

        return video_soup
