""" template tags """
from django import template
from ..models import Item
from cart.cart import get_cart
from random import randint

register = template.Library()


@register.inclusion_tag("catalog/tags/featured_item.html")
def featured_item():
    """ get a featured item and display it """
    item = None
    featured_items = Item.objects.featured_items()
    if featured_items:
        index = randint(0, (len(featured_items) - 1))
        item = featured_items[index]
    return {"item": item}


@register.inclusion_tag("catalog/tags/cart_box.html", takes_context=True)
def cart_box(context, request):
    """ show the cart box """
    cart = get_cart(request)
    if cart:
        cart_item_count = cart.item_count
        cart_subtotal = cart.cart_subtotal
    else:
        cart_item_count = 0
        cart_subtotal = 0.00
    return {
        "cart_item_count": cart_item_count,
        "cart_subtotal": cart_subtotal,
        "MEDIA_URL": context["MEDIA_URL"],
        "user": request.user,
    }
