from wagtail.models import Site
from wagtail.rich_text import expand_db_html

from ocyan.plugin.wagtail.models import OcyanSettings


class DeliveryTime(object):
    def __init__(self, delivery_time_text, delivery_time_days):
        self.delivery_time_text = delivery_time_text
        self.delivery_time_days = delivery_time_days

    def __str__(self):
        if self.delivery_time_text is not None:
            return expand_db_html(self.delivery_time_text)
        return ""

    def __bool__(self):
        return bool(self.delivery_time_days) and self.delivery_time_text is not None

    @classmethod
    def delivery_time(cls, basket, request=None):
        if request is not None:
            site = Site.find_for_request(request)
        else:
            site = Site.objects.get(is_default_site=True)

        settings = OcyanSettings.for_site(site)

        return cls(settings.delivery_time_text, settings.delivery_time_days)
