from django import template
from django.urls import reverse

from takahe.utils import Takahe

register = template.Library()


@register.simple_tag(takes_context=True)
def wish_item_action(context, item):
    user = context["request"].user
    action = {}
    if user and user.is_authenticated and item:
        action = {
            "taken": user.shelf_manager.locate_item(item) is not None,
            "url": reverse("journal:wish", args=[item.uuid]),
        }
    return action


@register.simple_tag(takes_context=True)
def liked_piece(context, piece):
    user = context["request"].user
    return user and user.is_authenticated and piece.is_liked_by(user.identity)


@register.simple_tag(takes_context=True)
def liked_post(context, post):
    if post.liked_by_current_user is not None:
        return post.liked_by_current_user
    user = context["request"].user
    return (
        user
        and user.is_authenticated
        and post.interactions.filter(
            identity_id=user.identity.pk, type="like", state__in=["new", "fanned_out"]
        ).exists()
    )


@register.simple_tag(takes_context=True)
def boosted_post(context, post):
    if post.boosted_by_current_user is not None:
        return post.boosted_by_current_user
    user = context["request"].user
    return (
        user
        and user.is_authenticated
        and post.interactions.filter(
            identity_id=user.identity.pk, type="boost", state__in=["new", "fanned_out"]
        ).exists()
    )
