import urllib.request
import json

from django import template
from django.core.exceptions import SuspiciousOperation
from django.contrib.auth.models import User
from musr.models import Profile, Post

register = template.Library()


# Used to highlight currently active page
@register.simple_tag(takes_context=True)
def current(context, url=None):
    if not "exception" in context and context.request.resolver_match.url_name == url:
        return "active"
    return ""


# Feed view "component"
@register.inclusion_tag("musr/songs.html")
def songs(posts, user, show_count=None):
    return {"posts": posts, "user": user, "show_count": show_count}


# Individual post "component"
@register.inclusion_tag("musr/song.html")
def song(post, user, show_count):
    if not post:
        raise SuspiciousOperation(
            "Invalid request; song can't be displayed without song id"
        )
        return

    # Grab post information
    poster = Profile.objects.get(user=post.poster)
    re_poster = None
    if post.original_poster:
        re_poster = poster
        poster = Profile.objects.get(user=post.original_poster)

    # Grab data from deezer
    try:
        url = "https://api.deezer.com/track/" + str(post.song_id)
        req = urllib.request.Request(url)
        r = urllib.request.urlopen(req).read()
        data = json.loads(r.decode("utf-8"))
    except:
        return {
            "error": "Could not retrieve information from Deezer API. Try refreshing the page."
        }

    if "error" in data:
        raise SuspiciousOperation("Invalid request; deezer song id is not a valid song")

    post.title = data["title"]
    post.artist = data["artist"]["name"]
    post.album = data["album"]["title"]
    post.album_art = data["album"]["cover_big"]
    post.preview = data["preview"]

    return {
        "song": post,
        "poster": poster,
        "re_poster": re_poster,
        "user": user,
        "show_count": show_count,
    }


# User List
@register.inclusion_tag("musr/user_list.html")
def user_list(users):
    return {"users": users}


# An item in the above user list
@register.inclusion_tag("musr/user_list_item.html")
def user_list_item(user):
    profile = Profile.objects.get(user=user)
    return {"profile": profile}
