import inspect

import requests
from flask import current_app

API_KEY = "***REMOVED***"
TMDB_FIND_URL = "https://api.themoviedb.org/3/find/"
TMDB_GET_TV_URL = "https://api.themoviedb.org/3/tv/"

TMDB_IMG_URL = "https://image.tmdb.org/t/p/original"


def get_movie_data(imdb_id):
    try:
        data = {"api_key": API_KEY, "language": "en-US", "external_source": "imdb_id"}
        r = requests.get(TMDB_FIND_URL + imdb_id, params=data)
        info = dict(r.json())
        if "status_code" in info.keys():
            current_app.logger.info("error getting tmdb movie data, status code: " + str(info["status_code"]) + " " + str(info["status_message"]))
            return None
        if info["movie_results"] == []:
            current_app.logger.info("no tmdb results for: " + str(imdb_id))
            return None
        current_app.logger.info("tmdb movie title: " + str(info["movie_results"][0]["title"]))
        movie_id = info["movie_results"][0]["id"]
        overview = info["movie_results"][0]["overview"]
        poster_path = info["movie_results"][0]["poster_path"]
        backdrop_path = info["movie_results"][0]["backdrop_path"]

        return movie_id, overview, poster_path, backdrop_path
    except Exception as e:
        current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))


def get_tv_show_data(imdb_id):
    try:
        data = {"api_key": API_KEY, "language": "en-US", "external_source": "imdb_id"}
        r = requests.get(TMDB_FIND_URL + imdb_id, params=data)
        info = dict(r.json())
        if "status_code" in info.keys():
            current_app.logger.info("error getting tmdb tv show data, status code: " + str(info["status_code"]) + " " + str(info["status_message"]))
            return None
        if info["tv_results"] == []:
            current_app.logger.info("no tmdb results for: " + str(imdb_id))
            return None
        current_app.logger.info("tmdb tv show title: " + str(info["tv_results"][0]["name"]))
        tv_show_id = info["tv_results"][0]["id"]
        overview = info["tv_results"][0]["overview"]
        poster_path = info["tv_results"][0]["poster_path"]

        return tv_show_id, overview, poster_path
    except Exception as e:
        current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))


def get_tv_episode_data(imdb_id):
    try:
        data = {"api_key": API_KEY, "language": "en-US", "external_source": "imdb_id"}
        r = requests.get(TMDB_FIND_URL + imdb_id, params=data)
        episode_info = dict(r.json())
        if "status_code" in episode_info.keys():
            current_app.logger.info(
                "error getting tmdb tv episode data, status code: " + str(episode_info["status_code"]) + " " + str(episode_info["status_message"])
            )
            return None
        if episode_info["tv_episode_results"] == []:
            current_app.logger.info("no tmdb results for: " + str(imdb_id))
            return None
        data = {"api_key": API_KEY, "language": "en-US"}
        r = requests.get(TMDB_GET_TV_URL + str(episode_info["tv_episode_results"][0]["show_id"]), params=data)
        show_name = dict(r.json())["name"]
        current_app.logger.info("tmdb tv_episode title: " + show_name + ": " + str(episode_info["tv_episode_results"][0]["name"]))
        tv_episode_id = episode_info["tv_episode_results"][0]["id"]
        name = episode_info["tv_episode_results"][0]["name"]
        overview = episode_info["tv_episode_results"][0]["overview"]
        still_path = episode_info["tv_episode_results"][0]["still_path"]

        return tv_episode_id, overview, still_path
    except Exception as e:
        current_app.logger.error(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
