import json

from django.http import HttpResponse, Http404, FileResponse
from django.template import loader, RequestContext
from django.contrib.auth.decorators import login_required
from .models import *
from django.conf import settings
from .funcs import get_inter_http_respose
from django.utils.translation import gettext_lazy as _

def test_code(request):


    return HttpResponse('finished')


def MainPage(request):

    # print(f'LOCALE_PATHS = {str(settings.LOCALE_PATHS)}')

    from .init_options import init_options
    init_options()

    try:
        page = StaticPage.objects.get(url='main')
    except StaticPage.DoesNotExist:
        page = StaticPage.objects.create(
            url='main',
            name_ru = _('Главная')
        )

    # try:
    #     page = StaticPage.objects.get(url='contacts')
    # except StaticPage.DoesNotExist:
    #     page = StaticPage.objects.create(
    #         url='contacts',
    #         name_ru = _('Контакты')
    #     )

    # from ArticlesApp.models import ArticleModel
    # arts = ArticleModel.objects.filter(enable=True).order_by('-createDT')[:4]

    Dict = {
        'page': page,
        'main': True
        # 'articles': arts,
    }

    # breadcrumbs_Dict = {
    # }
    # Dict.update({'breadcrumbs': breadcrumbs_Dict})

    t = loader.get_template('pages/p_main.html')
    return get_inter_http_respose(t, Dict, request)
    # return HttpResponse(t.render(Dict, request))



def StaticPageView(request, url):
    # from RoutesApp.forms import RouteForm
    # from SubscribesApp.funcs import get_subsribes_w_options

    Dict = {}

    if url == '':
        return MainPage(request)

    elif url in ['main']:
        raise Http404


    try:
        page = StaticPage.objects.get(url=url)
    except StaticPage.DoesNotExist:
        raise Http404


    Dict.update({
        'page': page,
    })

    t = loader.get_template('pages/p_static_page.html')
    return get_inter_http_respose(t, Dict, request)
    # return HttpResponse(t.render(Dict, request))


def contacts_view(request):
    # from RoutesApp.forms import RouteForm
    # from SubscribesApp.funcs import get_subsribes_w_options
    try:
        page = StaticPage.objects.get(url='contacts')
    except Exception as e:
        page = StaticPage.objects.create(
            url='contacts',
            name_ru=_('Контакты')
        )

    Dict = {
        'page': page,
        'offices': Office.objects.filter(enable=True)
    }

    t = loader.get_template('pages/p_contacts.html')
    return get_inter_http_respose(t, Dict, request)
    # return HttpResponse(t.render(Dict, request))
