from django.test import RequestFactory
from django.contrib.sites.models import Site
from django.contrib.staticfiles.finders import AppDirectoriesFinder

try:
    from wagtail.models.sites import get_site_for_hostname
except ImportError:  # Wagtail <5
    from wagtail.sites import get_site_for_hostname
try:
    from wagtail.contrib.settings.context_processors import SettingProxy as SettingsProxy
except ImportError:  # Wagtail <7
    from wagtail.contrib.settings.context_processors import SettingsProxy


def get_wagtailsite(domain):
    hostname = domain.rsplit(":", maxsplit=1)[0]
    return get_site_for_hostname(hostname, None)


def get_wagtailsettings(domain):
    site = get_wagtailsite(domain)
    return SettingsProxy(site)


def get_offline_request(domain):
    request = RequestFactory(SERVER_NAME=domain).get("/")
    request.site = get_wagtailsite(domain)
    return request


def get_default_site():
    wagtailsite = get_site_for_hostname(None, 0)
    return Site.objects.get(domain__iexact=wagtailsite.hostname)


class StaticSrcFinder(AppDirectoriesFinder):
    """
    This finder adds the capability to load the oscar scss from the static_src
    folder.
    """

    source_dir = "static_src"

    def check(self, **kwargs):
        return []
