from youtube_autonomous.elements.builder.element_builder import ElementBuilder
from youtube_autonomous.segments.enums import SegmentField
from yta_multimedia.greenscreen.custom.greenscreen import Greenscreen


class GreenscreenElementBuilder(ElementBuilder):
    """
    The builder that generate greenscreen content.
    """

    @classmethod
    def build_from_enhancement(
        cls,
        enhancement: dict
    ):
        """
        Returns an ImageGreenscreen or VideoGreenscreen instance
        initialized with the provided filename or url (filename
        has priority) or raises an Exception if something goes
        wrong.
        """
        filename = enhancement.get(SegmentField.FILENAME.value, None)
        url = enhancement.get(SegmentField.URL.value, None)

        if not url and not filename:
            # TODO: I think this cannot happen
            raise Exception('No "url" nor "filename" provided and at least one is needed.')

        return (
            Greenscreen.init(url)
            if url and not filename else
            Greenscreen.init(filename)
        )