from decimal import Decimal
import random
from django.contrib.sites.shortcuts import get_current_site
from django.contrib.sites.models import Site
from waffle.utils import get_setting, keyfmt


VERSION = (0, 10, 1, 1)
__version__ = '.'.join(map(str, VERSION))


class DoesNotExist(object):

    """The record does not exist."""
    @property
    def active(self):
        return get_setting('SWITCH_DEFAULT')


def set_flag(request, flag_name, active=True, session_only=False):
    """Set a flag value on a request object."""
    if not hasattr(request, 'waffles'):
        request.waffles = {}
    request.waffles[flag_name] = [active, session_only]


def flag_is_active(request, flag_name):
    from .models import cache_flag, Flag
    from .compat import cache

    flag = cache.get(keyfmt(get_setting('FLAG_CACHE_KEY'), flag_name))
    if flag is None:
        try:
            flag = Flag.objects.get(name=flag_name)
            cache_flag(instance=flag)
        except Flag.DoesNotExist:
            return get_setting('FLAG_DEFAULT')

    if get_setting('OVERRIDE'):
        if flag_name in request.GET:
            return request.GET[flag_name] == '1'

    if flag.everyone:
        return True
    elif flag.everyone is False:
        return False

    if flag.testing:  # Testing mode is on.
        tc = get_setting('TEST_COOKIE') % flag_name
        if tc in request.GET:
            on = request.GET[tc] == '1'
            if not hasattr(request, 'waffle_tests'):
                request.waffle_tests = {}
            request.waffle_tests[flag_name] = on
            return on
        if tc in request.COOKIES:
            return request.COOKIES[tc] == 'True'

    user = request.user

    if flag.authenticated and user.is_authenticated():
        return True

    if flag.staff and user.is_staff:
        return True

    if flag.superusers and user.is_superuser:
        return True

    if flag.languages:
        languages = flag.languages.split(',')
        if (hasattr(request, 'LANGUAGE_CODE') and
                request.LANGUAGE_CODE in languages):
            return True

    flag_users = cache.get(keyfmt(get_setting('FLAG_USERS_CACHE_KEY'),
                                  flag.name))
    if flag_users is None:
        flag_users = flag.users.all()
        cache_flag(instance=flag)
    if user in flag_users:
        return True

    flag_groups = cache.get(keyfmt(get_setting('FLAG_GROUPS_CACHE_KEY'),
                                   flag.name))
    if flag_groups is None:
        flag_groups = flag.groups.all()
        cache_flag(instance=flag)
    user_groups = user.groups.all()
    for group in flag_groups:
        if group in user_groups:
            return True

    # site management
    flag_sites = cache.get(keyfmt(get_setting('FLAG_SITES_CACHE_KEY'),
                                  flag.name))
    if flag_sites is None:
        flag_sites = flag.sites.all()
        cache_flag(instance=flag)
    site = get_current_site(request)
    if site in flag_sites:
        return True

    if flag.percent and flag.percent > 0:
        if not hasattr(request, 'waffles'):
            request.waffles = {}
        elif flag_name in request.waffles:
            return request.waffles[flag_name][0]

        cookie = get_setting('COOKIE') % flag_name
        if cookie in request.COOKIES:
            flag_active = (request.COOKIES[cookie] == 'True')
            set_flag(request, flag_name, flag_active, flag.rollout)
            return flag_active

        if Decimal(str(random.uniform(0, 100))) <= flag.percent:
            set_flag(request, flag_name, True, flag.rollout)
            return True
        set_flag(request, flag_name, False, flag.rollout)

    return False


def switch_is_active(switch_name):
    from .models import cache_switch, Switch
    from .compat import cache

    switch = cache.get(keyfmt(get_setting('SWITCH_CACHE_KEY'), switch_name))
    switch_sites = cache.get(keyfmt(get_setting('SWITCHES_SITES_CACHE_KEY'),
                                    switch_name))
    if switch is None:
        try:
            switch = Switch.objects.get(name=switch_name)
            switch_sites = switch.sites.all()
            cache_switch(instance=switch)
        except Switch.DoesNotExist:
            switch = DoesNotExist()
            switch.name = switch_name
            switch.sites = Site.objects.none()
            cache_switch(instance=switch)

    if switch_sites:
        site = Site.objects.get_current()
        return switch.active and site in switch_sites
    return switch.active


def sample_is_active(sample_name):
    from .models import cache_sample, Sample
    from .compat import cache

    sample = cache.get(keyfmt(get_setting('SAMPLE_CACHE_KEY'), sample_name))
    sample_sites = cache.get(keyfmt(get_setting('SAMPLE_SITES_CACHE_KEY'),
                                    sample_name))
    if sample is None:
        try:
            sample = Sample.objects.get(name=sample_name)
            sample_sites = sample.sites.all()
            cache_sample(instance=sample)
        except Sample.DoesNotExist:
            return get_setting('SAMPLE_DEFAULT')

    if sample_sites:
        site = Site.objects.get_current()
        if site in sample_sites:
            return Decimal(str(random.uniform(0, 100))) <= sample.percent
        else:
            return False
    else:
        return Decimal(str(random.uniform(0, 100))) <= sample.percent

@@ -99,53 +99,8 @@
 ite%0A
-from django.contrib.sites.models import Site%0A
 from
@@ -4194,32 +4194,89 @@
 h.DoesNotExist:%0A
+            from django.contrib.sites.models import Site%0A
             swit
@@ -4436,32 +4436,85 @@
 f switch_sites:%0A
+        from django.contrib.sites.models import Site%0A
         site = S
@@ -5214,32 +5214,85 @@
 f sample_sites:%0A
+        from django.contrib.sites.models import Site%0A
         site = S

remove import of Site in init to add django 1.9 compatibility