# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.http import HttpResponseBadRequest, JsonResponse
from django.views.decorators.cache import cache_page

import pybomojo

from bomojo.backends import get_movie_backend
from bomojo.movies.models import Movie
from bomojo.movies.renderers import render_movie
from bomojo.utils import get_setting


@cache_page(60 * 60 * 24)
def search(request):
    search_term = request.GET.get('title', '').strip()

    if not search_term:
        return HttpResponseBadRequest('"title" is required')

    max_results = get_setting('MOVIE_MAX_SEARCH_RESULTS')
    movies = list(Movie.objects.filter(
        title__search=search_term)[:max_results])

    if len(movies) < max_results:
        api_results = pybomojo.search_movies(search_term)

        for api_result in api_results:
            movie, created = Movie.objects.get_or_create(
                title=api_result['title'], external_id=api_result['movie_id'])
            if created:
                movies.append(movie)

    movies = movies[:max_results]

    backend = get_movie_backend()
    results = [_render_movie(m, backend, search_term) for m in movies]

    # Move exact result to the top. This is very inefficient, but it'll get the
    # job done for now. (The more efficient code would be slightly uglier.)
    results = ([result for result in results if result['exact']] +
               [result for result in results if not result['exact']])

    return JsonResponse({'results': results})


@cache_page(60 * 15)
def box_office(request, movie_id):
    try:
        return JsonResponse(pybomojo.get_box_office(
            get_movie_backend().parse_movie_id(movie_id)))
    except pybomojo.MovieNotFound as e:
        return JsonResponse({'errors': [str(e)]}, status=404)


def _render_movie(movie, backend, search_term):
    result = render_movie(movie, backend)
    result['exact'] = movie.title.lower() == search_term
    return result
