__all__ = ["CSPSetting"]


class CSPSetting(object):
    """
    Used to concatenate the CSP settings for the security plugin

    >>> first = CSPSetting()
    >>> klaas = CSPSetting(default_src=["'self'"])
    >>> harrie = first + klaas
    >>> harrie.default_src
    ["'self'"]
    >>> test = harrie + CSPSetting(default_src=["klatsieharrie.nl"], img_src=["testing.nl", "example.com"])
    >>> sorted(test.default_src)
    ["'self'", 'klatsieharrie.nl']
    >>> sorted(test.img_src)
    ['example.com', 'testing.nl']

    >>> test += CSPSetting(style_src=["knaken.nl"])
    >>> sorted(test.style_src)
    ['knaken.nl']
    """

    # pylint: disable=dangerous-default-value
    def __init__(
        self,
        default_src=[],
        script_src=[],
        style_src=[],
        connect_src=[],
        img_src=[],
        font_src=[],
        object_src=[],
        child_src=[],
        form_action=[],
        frame_ancestors=[],
        excluded_url_prefixes=[],
        base_uri=[],
    ):
        self._default_src = set(default_src)
        self._script_src = set(script_src)
        self._style_src = set(style_src)
        self._connect_src = set(connect_src)
        self._img_src = set(img_src)
        self._font_src = set(font_src)
        self._object_src = set(object_src)
        self._child_src = set(child_src)
        self._form_action = set(form_action)
        self._frame_ancestors = set(frame_ancestors)
        self._excluded_url_prefixes = set(excluded_url_prefixes)
        self._base_uri = set(base_uri)

    def __add__(self, other):
        return CSPSetting(
            default_src=self._default_src | other._default_src,
            script_src=self._script_src | other._script_src,
            style_src=self._style_src | other._style_src,
            connect_src=self._connect_src | other._connect_src,
            img_src=self._img_src | other._img_src,
            excluded_url_prefixes=self._excluded_url_prefixes
            | other._excluded_url_prefixes,
            font_src=self._font_src | other._font_src,
            object_src=self._object_src | other._object_src,
            child_src=self._child_src | other._child_src,
            form_action=self._form_action | other._form_action,
            frame_ancestors=self._frame_ancestors | other._frame_ancestors,
            base_uri=self._base_uri | other._base_uri,
        )

    @property
    def default_src(self):
        return list(self._default_src)

    @property
    def script_src(self):
        return list(self._script_src)

    @property
    def style_src(self):
        return list(self._style_src)

    @property
    def connect_src(self):
        return list(self._connect_src)

    @property
    def img_src(self):
        return list(self._img_src)

    @property
    def font_src(self):
        return list(self._font_src)

    @property
    def object_src(self):
        return list(self._object_src)

    @property
    def child_src(self):
        return list(self._child_src)

    @property
    def form_action(self):
        return list(self._form_action)

    @property
    def frame_ancestors(self):
        return list(self._frame_ancestors)

    @property
    def excluded_url_prefixes(self):
        return tuple(self._excluded_url_prefixes)

    @property
    def base_uri(self):
        return list(self._base_uri)
