import datetime
import logging

from django.contrib import auth
from django.http import HttpResponseRedirect, HttpResponseNotFound
from django.utils.translation import check_for_language
from django.shortcuts import render

from blueapps.account.components.bk_token.forms import AuthenticationForm
from gcloud.core.signals import user_enter
from gcloud.conf import settings

logger = logging.getLogger("root")


def page_not_found(request, exception):
    if request.path.startswith(settings.STATIC_URL):
        return HttpResponseNotFound()

    user = _user_authenticate(request)

    # 未登录重定向到首页，跳到登录页面
    if not user:
        return HttpResponseRedirect(
            settings.SITE_URL + "?{}={}".format(settings.PAGE_NOT_FOUND_URL_KEY, request.build_absolute_uri())
        )
    request.user = user
    # not home url enter
    user_enter.send(username=user.username, sender=user.username)
    return render(request, "core/base_vue.html", {})


def _user_authenticate(request):
    # 先做数据清洗再执行逻辑
    form = AuthenticationForm(request.COOKIES)
    if not form.is_valid():
        return None

    bk_token = form.cleaned_data["bk_token"]
    # 确认 cookie 中的 bk_token 和 session 中的是否一致
    # 如果登出删除 cookie 后 session 存在 is_match 为False
    is_match = bk_token == request.session.get("bk_token")
    if is_match and request.user.is_authenticated:
        return request.user

    user = auth.authenticate(request=request, bk_token=bk_token)
    if user:
        # 登录成功，记录 user 信息
        auth.login(request, user)
        request.session["bk_token"] = bk_token
    return user


def home(request):
    try:
        username = request.user.username
        # home url enter
        user_enter.send(username=username, sender=username)
    except Exception:
        logger.exception("user_enter signal send failed.")
    return render(request, "core/base_vue.html")


def set_language(request):
    request_params = getattr(request, request.method)
    next_url = request_params.get("next", None) or request.META.get("HTTP_REFERER", "/")
    response = HttpResponseRedirect(next_url)

    if request.method == "GET":
        lang_code = request.GET.get("language", None)
        if lang_code and check_for_language(lang_code):
            if hasattr(request, "session"):
                request.session["blueking_language"] = lang_code
            max_age = 60 * 60 * 24 * 365
            expires = datetime.datetime.strftime(
                datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT",
            )
            response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code, max_age, expires)
    return response
