import json
import re
from urllib import quote_plus

from django.conf import settings
from django.utils.functional import SimpleLazyObject
from django.utils.html import mark_safe
from django.utils.timezone import now

from .models import Category, Vote
from .utils import get_voter, get_number_of_votes, vote_needs_captcha


def voter(request):
    def _get_voter():
        return get_voter(request)

    def _get_votes():
        voter = get_voter(request)
        if not voter:
            return '{}'
        votes = Vote.objects.filter(
            voter__email=voter['email'],
            voter__site_mode=request.site_mode,
        )
        votes = [i.submission_id for i in votes]
        return mark_safe(json.dumps({"submissions": votes}))

    return {
        'VOTER': SimpleLazyObject(_get_voter),
        'VOTES_CAST': SimpleLazyObject(_get_votes),
    }


def global_vars(request):
    mode = request.site_mode

    def _get_categories():
        return Category.objects.filter(site_mode=mode)

    site_domain_with_protocol = "%s://%s" % (request.scheme, mode.site.domain)
    url_tmpl = u"https://twitter.com/intent/tweet?url=%(SITE_DOMAIN)s&text=%(tweet_text)s"
    TWITTER_URL = url_tmpl % {
        "SITE_DOMAIN": quote_plus(site_domain_with_protocol + "?source=tw-site"),
        "tweet_text": quote_plus(mode.twitter_site_text),
    }
    FACEBOOK_URL = u"https://www.facebook.com/sharer/sharer.php?&u=%(url)s" % {
        "url": quote_plus(site_domain_with_protocol + "?source=fb-site"),
    }

    return {
        'CAPTCHA_SITE_KEY': settings.NORECAPTCHA_SITE_KEY,
        'DEBUG': settings.DEBUG,
        'VOTE_NEEDS_CAPTCHA': vote_needs_captcha(request),
        'NUMBER_OF_VOTES': get_number_of_votes(request) if mode.show_total_votes else 0,
        'STATIC_URL': settings.STATIC_URL,

        'FACEBOOK_URL': FACEBOOK_URL,
        'TWITTER_URL': TWITTER_URL,

        'SUBMISSIONS_PER_PAGE': settings.SUBMISSIONS_PER_PAGE,
        'SITE_DOMAIN': mode.site.domain,
        'SITE_LINK': site_domain_with_protocol,
        'MIXPANEL_KEY': settings.MIXPANEL_KEY,
        'OPTIMIZELY_KEY': settings.OPTIMIZELY_KEY,
        'SHOW_QUESTION_VOTES': mode.show_question_votes,
        'SHOW_TOTAL_VOTES': mode.show_total_votes,
        'ALLOW_SORTING_BY_VOTES': mode.allow_sorting_by_votes,
        'ALLOW_VOTING_AND_SUBMITTING_QUESTIONS': mode.allow_voting_and_submitting_questions,
        'DEBATE_TIME': mode.debate_time,
        'PREVIOUS_DEBATE_TIME': (
            mode.previous_debate_time if mode.previous_debate_time and
            mode.previous_debate_time < now() else None
        ),
        'LOCAL_VOTES_STATE': mode.debate_state,
        'ANNOUNCEMENT': {
            'headline': mode.announcement_headline,
            'body': mode.announcement_body,
            'link': mode.announcement_link,
        } if (mode.announcement_headline and
              (not mode.announcement_page_regex or
               re.match(mode.announcement_page_regex, request.path))
              ) else None,

        'BANNER_HEADER_TITLE': mode.banner_header_title,
        'BANNER_HEADER_COPY': mode.banner_header_copy,
        # Note: POPUP_AFTER_SUBMISSION_TEXT is not actually json; this is used
        # to provide a string surrounded by double quotes with all the necessary
        # characters escaped. See PR #188.
        'POPUP_AFTER_SUBMISSION_TEXT': json.dumps(mode.popup_after_submission_text),

        'SUBMISSION_CATEGORIES': SimpleLazyObject(_get_categories),
        'SITE_THEME_NAME': settings.SITE_THEME_NAME,
        'SITE_MODE': mode,
    }

@@ -921,16 +921,432 @@
 e_mode%0A%0A
+    context = %7B%0A        'CAPTCHA_SITE_KEY': settings.NORECAPTCHA_SITE_KEY,%0A        'DEBUG': settings.DEBUG,%0A        'STATIC_URL': settings.STATIC_URL,%0A        'SUBMISSIONS_PER_PAGE': settings.SUBMISSIONS_PER_PAGE,%0A        'MIXPANEL_KEY': settings.MIXPANEL_KEY,%0A        'OPTIMIZELY_KEY': settings.OPTIMIZELY_KEY,%0A        'SITE_THEME_NAME': settings.SITE_THEME_NAME,%0A    %7D%0A    if mode is None:%0A        return context%0A%0A
     def 
@@ -1360,24 +1360,24 @@
 tegories():%0A
-
         retu
@@ -1936,108 +1936,24 @@
     
-return %7B%0A        'CAPTCHA_SITE_KEY': settings.NORECAPTCHA_SITE_KEY,%0A        'DEBUG': settings.DEBUG,
+context.update(%7B
 %0A   
@@ -2100,51 +2100,8 @@
 e 0,
-%0A        'STATIC_URL': settings.STATIC_URL,
 %0A%0A  
@@ -2177,71 +2177,8 @@
 L,%0A%0A
-        'SUBMISSIONS_PER_PAGE': settings.SUBMISSIONS_PER_PAGE,%0A
     
@@ -2266,106 +2266,8 @@
 ol,%0A
-        'MIXPANEL_KEY': settings.MIXPANEL_KEY,%0A        'OPTIMIZELY_KEY': settings.OPTIMIZELY_KEY,%0A
     
@@ -3619,61 +3619,8 @@
 s),%0A
-        'SITE_THEME_NAME': settings.SITE_THEME_NAME,%0A
     
@@ -3642,14 +3642,34 @@
 : mode,%0A
-
     %7D
+)%0A    return context
 %0A

Make the global_vars context processor return a sane default