import re
from functools import wraps

from c3nav.mapdata.utils.user import get_user_data_lazy


class NoLanguageMiddleware:
    """
    Middleware that allows unsetting the Language HTTP header usind the @no_language decorator.
    """
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)

        if not getattr(response, 'no_language', False):
            return response

        if not getattr(response, 'keep_content_language', False):
            del response['Content-Language']

        if not response.has_header('Vary'):
            return response

        vary = tuple(s for s in re.split(r'\s*,\s*', response['Vary']) if s.lower() != 'accept-language')

        if vary:
            response['Vary'] = ', '.join(vary)
        else:
            del response['Vary']

        return response


def no_language(keep_content_language=False):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            response = func(*args, **kwargs)
            response.no_language = True
            response.keep_content_language = keep_content_language
            return response
        return wrapper
    return decorator


class UserDataMiddleware:
    """
    Enables getting user_data using request.user_data.
    """
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        request.user_data = get_user_data_lazy(request)
        return self.get_response(request)
