import json
from django.shortcuts import render, get_object_or_404, HttpResponse, redirect
import logging
from projects.settings import RECAPTCHA_PUBLIC_KEY
from userapp.models import CostumUser

logger = logging.getLogger(__name__)


def gen_data(request):

    try:
        languages = ["fr", "en"]
        auth_pages = ["/auth/to_login", "/auth/to_signup"]

        path_incude_s = ["/static/", "/assets/", "/accounts/"]
        data = {}
        cur_url = request.path
        data["cur_url"] = cur_url
        print("PATH :::::::::::::::::::", request.path, "::::::::::::::::::::::::::")
        logger.info(cur_url)

        if (
            request.COOKIES.get("rvlang") is not None
            and request.COOKIES.get("rvlang") in languages
        ):

            data["rvlang"] = request.COOKIES.get("rvlang")
        else:
            data["rvlang"] = "en"

        if RECAPTCHA_PUBLIC_KEY:
            data["RECAPTCHA_PUBLIC_KEY"] = RECAPTCHA_PUBLIC_KEY

        path_is_valid = (
            not str(request.path).startswith("/static/")
            and not str(request.path).startswith("/assets/")
            and not str(request.path).startswith("/accounts/")
            and not str(request.path).startswith("/favico")
            and not str(request.path).startswith("/404")
            and not str(request.path).startswith("/to_forgetpass")
            and not str(request.path).startswith("/resetpass")
            and not str(request.path).startswith("/to_updatepass")
            and not str(request.path).startswith("/updatepass")
            and not str(request.path).startswith("/auth")
        )

        if request.path not in auth_pages and path_is_valid:
            request.session["mynext"] = request.path
            request.session.save()
        data["mynext"] = request.session.get("mynext", "/")

        if request.user.is_authenticated:
            data["cuser"] = get_object_or_404(CostumUser, user=request.user)

    except Exception as e:
        print(e)

    return data
