import uuid
import django.utils.deprecation

from bs4 import BeautifulSoup

from django.conf import settings
from django.http import HttpResponseForbidden
from django_cas_ng.middleware import CASMiddleware
from django.views.defaults import permission_denied
from django.contrib.auth.views import login, logout
from django.core.urlresolvers import resolve, reverse
from django.shortcuts import redirect, render_to_response
from django_cas_ng.views import login as cas_login, logout as cas_logout
# test
from django.utils.translation import activate
from django.contrib.messages import get_messages

from google_analytics.tasks import send_ga_tracking
from google_analytics.utils import build_ga_params, set_cookie

from wagtail.wagtailcore.models import Site, Page
from molo.core.models import SiteSettings
from molo.core.models import Languages


class MoloCASMiddleware(CASMiddleware):

    def process_view(self, request, view_func, view_args, view_kwargs):
        if view_func == login or view_func == logout:
            return None

        if view_func == cas_login:
            return cas_login(request, *view_args, **view_kwargs)
        elif view_func == cas_logout:
            return cas_logout(request, *view_args, **view_kwargs)

        if settings.CAS_ADMIN_PREFIX:
            if not request.path.startswith(settings.CAS_ADMIN_PREFIX):
                return None
        elif not view_func.__module__.startswith('django.contrib.admin.'):
            return None

        if request.user.is_authenticated():
            if request.user.has_perm('wagtailadmin.access_admin'):
                """
                Implemented using wagtails permissions model
                https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailadmin/views/account.py#L112 # noqa
                """
                return None
            else:
                return permission_denied(request, 'error')
        return super(MoloCASMiddleware, self).process_view(
            request, view_func, view_args, view_kwargs)


class Custom403Middleware(django.utils.deprecation.MiddlewareMixin):
    """Catches 403 responses and raises 403 which allows for custom 403.html"""
    def process_response(self, request, response):
        storage = get_messages(request)
        for message in storage:
            pass
        if isinstance(response, HttpResponseForbidden):
            return permission_denied(request, 'error')
        return response


class ForceDefaultLanguageMiddleware(django.utils.deprecation.MiddlewareMixin):
    """
    Ignore Accept-Language HTTP headers

    This will force the I18N machinery to always choose settings.LANGUAGE_CODE
    as the default initial language, unless another one is set via
    sessions or cookies

    Should be installed *before* any middleware that checks
    request.META['HTTP_ACCEPT_LANGUAGE'],
    namely django.middleware.locale.LocaleMiddleware
    """
    def process_request(self, request):
        if 'HTTP_ACCEPT_LANGUAGE' in request.META:
            del request.META['HTTP_ACCEPT_LANGUAGE']


class AdminLocaleMiddleware(django.utils.deprecation.MiddlewareMixin):
    """Ensures that the admin locale doesn't change with user selection"""
    def process_request(self, request):
        if request.path.startswith('/admin/') or \
           request.path.startswith('/django-admin/'):
            activate(settings.ADMIN_LANGUAGE_CODE)


class NoScriptGASessionMiddleware(django.utils.deprecation.MiddlewareMixin):
    """Store a unique session key for use with GTM"""
    def process_request(self, request):
        if 'MOLO_GA_SESSION_FOR_NOSCRIPT' not in request.session:
            request.session[
                'MOLO_GA_SESSION_FOR_NOSCRIPT'] = uuid.uuid4().hex


class MoloGoogleAnalyticsMiddleware(django.utils.deprecation.MiddlewareMixin):
    """Uses GA IDs stored in Wagtail to track pageviews using celery"""
    def submit_tracking(self, account, request, response, custom_params={}):
        try:
            title = BeautifulSoup(
                response.content, "html.parser"
            ).html.head.title.text.encode('utf-8')
        except:
            title = None

        path = request.get_full_path()
        referer = request.META.get('HTTP_REFERER', '')
        params = build_ga_params(
            request, account, path=path, referer=referer, title=title)
        response = set_cookie(params, response)

        # send user unique id after cookie's been set
        if hasattr(request, 'user') and hasattr(request.user, 'profile')\
                and request.user.profile.uuid:
            uuid = request.user.profile.uuid
            params = build_ga_params(
                request, account, path=path,
                referer=referer, title=title,
                user_id=uuid, custom_params=custom_params)

        send_ga_tracking.delay(params)
        return response

    def process_response(self, request, response):
        if hasattr(settings, 'GOOGLE_ANALYTICS_IGNORE_PATH'):
            exclude = [p for p in settings.GOOGLE_ANALYTICS_IGNORE_PATH
                       if request.path.startswith(p)]
            if any(exclude):
                return response

        # Only track 200 and 302 responses for request.site
        if not (response.status_code == 200 or response.status_code == 302):
            return response

        site_settings = SiteSettings.for_site(request.site)
        local_ga_account = site_settings.local_ga_tracking_code or \
            settings.GOOGLE_ANALYTICS.get('google_analytics_id')

        if local_ga_account:
            response = self.submit_tracking(
                local_ga_account, request, response)

        if site_settings.global_ga_tracking_code:
            response = self.submit_tracking(
                site_settings.global_ga_tracking_code, request, response)

        return response


class MultiSiteRedirectToHomepage(django.utils.deprecation.MiddlewareMixin):

    def process_request(self, request):
        if request.path.startswith('/admin/pages/') and \
                not request.path.startswith('/admin/pages/moderation/'):
            current_site = Site.find_for_request(request)
            func, args, kwargs = resolve(request.path)
            if args:
                p_site = Page.objects.get(pk=args[-1]).get_site()
                if p_site and not current_site == p_site:
                    return redirect('%s%s' % (p_site.root_url, request.path))
            if not Languages.for_site(request.site).languages.all().exists():
                return redirect('%s/admin/' % request.site.root_url)


class MaintenanceModeMiddleware(django.utils.deprecation.MiddlewareMixin):

    def process_request(self, request):
        maintenance = getattr(settings, 'MAINTENANCE_MODE', None)
        template_name = getattr(
            settings,
            'MAINTENANCE_MODE_TEMPLATE', 'core/maintenance_mode.html'
        )
        if maintenance and reverse('health') not in request.path:
            page_ctx = getattr(
                settings,
                'MAINTENANCE_MODE_PAGE_CONTEXT',
                {'title': 'Maintenance Mode'}
            )

            class Page(object):
                depth = 2

                def __init__(self, ctx):
                    for k, v in ctx.items():
                        setattr(self, k, v)

            return render_to_response(
                template_name, context={
                    'self': Page(page_ctx),
                    'request': request,
                    'ENV': getattr(settings, 'ENV', 'dev'),
                    'STATIC_URL': getattr(settings, 'STATIC_URL', 'dev'),
                }
            )
