# -*- coding: utf-8 -*-

from .models import *

from datetime import datetime, date
from django.http import Http404, HttpResponse
from django.template import loader
from .funcs import *
from GeneralApp.funcs import get_inter_http_respose

# from django.contrib.auth.decorators import login_required
# from BaseModels.search_optimization.ld_json.ld_article_news import get_ld_article_news
# from django.contrib.sites.shortcuts import get_current_site



def get_flat_pages_links_Dict(site):

    flat_pages_links = UserPageModel.objects.filter(
        url__in=('about-truenergy', 'for-partners', 'contacts'),
        sites=site
    ).values_list('url', flat=True)

    return {'flat_pages_links': flat_pages_links}



def get_article_breadcrumbs(request, art):
    # print('get_article_breadcrumbs')
    articles_add_count = 1
    # half_count = articlesCountInBlock / 2

    arts_top = ArticleModel.objects.filter(enable=True, createDT__gt=art.createDT).order_by(
        'createDT')[:articles_add_count]
    arts_down = ArticleModel.objects.filter(enable=True, createDT__lt=art.createDT).order_by(
        '-createDT')[:articles_add_count]
    # if len(artListDown)<half_count:
    #     art_List = ArticleModel.objects.filter(enable=True, article_DT__gte=art.article_DT).order_by(
    #     'article_DT')[:art.articlesCountInBlock-len(artListDown)]

    Dict = {
        'arts_top': arts_top,
        'arts_down': arts_down
    }
    return Dict


def get_user_pages_breadcrumbs(request, art):
    from ArticlesApp.models import UserPageModel
    # print('get_user_pages_breadcrumbs')
    half_count = art.articlesCountInBlock / 2

    art_List = UserPageModel.objects.filter(enable=True, article_DT__gte=art.article_DT).order_by(
        'article_DT')[:half_count]
    artListDown = UserPageModel.objects.filter(enable=True, article_DT__lt=art.article_DT).order_by(
        '-article_DT')[:art.articlesCountInBlock-len(art_List)]
    if len(artListDown)<half_count:
        art_List = UserPageModel.objects.filter(enable=True, article_DT__gte=art.article_DT).order_by(
        'article_DT')[:art.articlesCountInBlock-len(artListDown)]

    art_List = list(art_List)
    art_List.reverse()
    artlist = art_List + list(artListDown)
    # print('artlist',artlist)
    return artlist


# @login_required(login_url='/admin/')
def ArticlesPageView(request, year=None):

    kwargs = {}

    if year:
        try:
            year = int(year)
            kwargs.update({'createDT__year': year})
        except:
            raise Http404


    Dict = get_articles(art_kwargs=kwargs)

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



# @login_required(login_url='/admin/')
def UserPageView(request, page_url):


    kwargs = {
        'url': page_url
    }

    try:
        art = UserPageModel.objects.get(**kwargs)
    except UserPageModel.DoesNotExist:
        raise Http404

    # service = ServiceModel.objects.filter(enable=True, artDevs=art)[0]

    # article_breadcrumbs = get_user_pages_breadcrumbs(request,art)
    # print('article_breadcrumbs',article_breadcrumbs)

    Dict = {
        'art' : art,
        'user_page' : True,
        # 'service' : service,
        # 'article_breadcrumbs' : article_breadcrumbs
    }



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



# @login_required(login_url='/admin/')
def ArticlesOnePageView(request, art_url):

    kwargs = {
        'url': art_url
    }

    try:
        art = ArticleModel.objects.get(**kwargs)
    except ArticleModel.DoesNotExist:
        raise Http404



    # print('article_breadcrumbs',article_breadcrumbs)

    Dict = {
        'art' : art,
    }

    Dict.update(get_article_breadcrumbs(request, art))



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