# Create your views here.
from django.shortcuts import render
from django.contrib.syndication.views import Feed
from django.template.defaultfilters import truncatewords
from django.urls import reverse
from django.shortcuts import redirect

from thewind.cache.CacheManager import CacheManager
from thewind.conf.config import config_info
from thewind.models import Article
from thewind.models import ArticleDetail


def add_except_page(func):
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            print(e)
            return render(args[0], '404.html', {'config_info': config_info})
    return wrapper


@add_except_page
def index(request, page_id):
    if not page_id:
        page_id = 0
    cache_manager = CacheManager()
    render_info = cache_manager.get_article_list(page_id=int(page_id))
    motto_info = cache_manager.get_motto()
    account_info = cache_manager.get_account_list()
    render_info.update(account_info)
    render_info.update(
        {
            'config_info': config_info,
            'motto_info': motto_info,
        }
    )
    return render(request, 'index.html', render_info)


@add_except_page
def article_like(request, article_id):
    # add like count
    cache_manager = CacheManager()
    cache_manager.add_article_like(article_id=article_id)
    page_id = cache_manager.get_page_id(article_id=int(article_id))
    return index(request=request, page_id=page_id)


@add_except_page
def nextpage(request):
    indexID = request.path.split('/')[-1][8:]
    cacheManager = CacheManager()
    callBackInfo = cacheManager.get_article_list(indexID=indexID)
    article_info = callBackInfo['articleInfo']
    page_info = callBackInfo['pageInfo']
    motto_info = cacheManager.get_motto()
    render_info = {
        'config_info': config_info,
        'article_info': article_info,
        'motto_info': motto_info,
        'page_info': page_info
    }
    return render(request, 'index.html', render_info)


@add_except_page
def book(request):
    cache_manager = CacheManager()
    render_info = cache_manager.get_book_list()
    render_info.update(
        {
            'config_info': config_info,
        }
    )
    return render(request, 'book.html', render_info)


@add_except_page
def word(request):
    cache_manager = CacheManager()
    word_info = cache_manager.get_word_list()
    render_info = {'config_info': config_info, 'word_info': word_info}
    return render(request, 'word.html', render_info)


@add_except_page
def category(request, category_id):
    cache_manager = CacheManager()
    render_info = cache_manager.get_category_list(category_id=category_id)
    render_info.update(
        {
            'config_info': config_info,
        }
    )
    return render(request, 'category.html', render_info)


@add_except_page
def tag(request, tag_id):
    cache_manager = CacheManager()
    render_info = cache_manager.get_tag_list(tag_id=tag_id)
    render_info.update(
        {
            'config_info': config_info,
        }
    )
    return render(request, 'tag.html', render_info)


@add_except_page
def link(request):
    cache_manager = CacheManager()
    render_info = cache_manager.get_link_list()
    render_info.update(
        {
            'config_info': config_info,
        }
    )
    return render(request, 'link.html', render_info)

@add_except_page
def article_detail(request, article_id):
    cache_manager = CacheManager()
    render_info = cache_manager.get_article_detail(article_id=article_id)
    render_info.update(
        {
            'config_info': config_info,
        }
    )
    return render(request, 'detail.html', render_info)


@add_except_page
def motto_detail(request, motto_id):
    cache_manager = CacheManager()
    render_info = cache_manager.get_motto_detail(motto_id=motto_id)
    render_info.update(
        {
            'config_info': config_info,
        }
    )
    return render(request, 'motto_detail.html', render_info)


@add_except_page
def about(request):
    return render(request, '404.html', {'config_info': config_info})


@add_except_page
def mottos(request):
    cache_manager = CacheManager()
    render_info = cache_manager.get_motto_list()
    render_info.update(
        {
            'config_info': config_info,
        }
    )
    return render(request, 'motto.html', render_info)


# @add_except_page
class BlogFeed(Feed):
    title = "the wind's feed"
    link = "/feeds"
    description = 'update thewind feeds'

    def items(self):
        return Article.objects.all()[:5]

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        detail_content = ''
        item_detail = item.articledetail_set.first()
        if item_detail:
            detail_content = item_detail.content
        return detail_content

    def item_link(self, item):
        return reverse('article_detail', args=[item.pk])

# def django_send_email():
#     # subject, message, sender, recipients
#     send_mail(
#         subject='the django send mail api test',
#         message='the django send mail detail',
#         from_email=settings.EMAIL_FROM,
#         recipient_list=['15951606335@163.com'],
#     )
#     return HttpResponse('send email success!')

# Create your views here.
