# Path: src/moviesapp/exceptions.py
# class MovieNotInDb(Exception):
#     """
#     Movie is not in DB.
# 
#     It means the movie does not have an IMDB id.
#     """
# 
# Path: src/moviesapp/models.py
# def get_poster_url(size, poster):
#     if size == "small":
#         poster_size = settings.POSTER_SIZE_SMALL
#         no_image_url = settings.NO_POSTER_SMALL_IMAGE_URL
#     elif size == "normal":
#         poster_size = settings.POSTER_SIZE_NORMAL
#         no_image_url = settings.NO_POSTER_NORMAL_IMAGE_URL
#     elif size == "big":
#         poster_size = settings.POSTER_SIZE_BIG
#         no_image_url = None  # is not used anywhere
#     if poster is not None:
#         return settings.POSTER_BASE_URL + poster_size + "/" + poster
#     return no_image_url


from datetime import datetime
from operator import itemgetter
from babel.dates import format_date
from django.conf import settings
from .exceptions import MovieNotInDb
from .models import get_poster_url
import tmdbsimple




def get_tmdb(lang=None):
    tmdbsimple.API_KEY = settings.TMDB_KEY
    tmdbsimple.LANGUAGE = lang
    return tmdbsimple


def get_poster_from_tmdb(poster):
    if poster:
        return poster[1:]
    return None


def get_movies_from_tmdb(query, search_type, options, user, lang):
    def set_proper_date(movies):
        def get_date(date):
            if date:
                date = datetime.strptime(date, "%Y-%m-%d")
                if date:
                    return format_date(date, locale=lang)
            return None

        for movie in movies:

# Path: src/moviesapp/exceptions.py
# class MovieNotInDb(Exception):
#     """
#     Movie is not in DB.
# 
#     It means the movie does not have an IMDB id.
#     """
# 
# Path: src/moviesapp/models.py
# def get_poster_url(size, poster):
#     if size == "small":
#         poster_size = settings.POSTER_SIZE_SMALL
#         no_image_url = settings.NO_POSTER_SMALL_IMAGE_URL
#     elif size == "normal":
#         poster_size = settings.POSTER_SIZE_NORMAL
#         no_image_url = settings.NO_POSTER_NORMAL_IMAGE_URL
#     elif size == "big":
#         poster_size = settings.POSTER_SIZE_BIG
#         no_image_url = None  # is not used anywhere
#     if poster is not None:
#         return settings.POSTER_BASE_URL + poster_size + "/" + poster
#     return no_image_url


# Path: src/moviesapp/tmdb.py
from datetime import datetime
from operator import itemgetter
from babel.dates import format_date
from django.conf import settings
from .exceptions import MovieNotInDb
from .models import get_poster_url
import tmdbsimple





def get_tmdb(lang=None):
    tmdbsimple.API_KEY = settings.TMDB_KEY
    tmdbsimple.LANGUAGE = lang
    return tmdbsimple


def get_poster_from_tmdb(poster):
    if poster:
        return poster[1:]
    return None


def get_movies_from_tmdb(query, search_type, options, user, lang):
    def set_proper_date(movies):
        def get_date(date):
            if date:
                date = datetime.strptime(date, "%Y-%m-%d")
                if date:
                    return format_date(date, locale=lang)
            return None

        for movie in movies:

            movie["releaseDate"] = get_date(movie["releaseDate"])