# from app import app
import urllib.request,json 
# will help us create a connection to our API URL and send a request and json modules that will format the JSON response to a Python dictionary
from .model import Movie

# Movie = movie.Movie


#getting the api key
api_key=None
 #getting the base url
base_url=None 

def configure_request(app):
    global api_key,base_url

    api_key = app.config['MOVIE_API_KEY']
    
    base_url= app.config['MOVIE_API_BASE_URL']
    # print(base_url)

def process_results(movie_list):
    '''
    Function  that processes the movie result and transform them to a list of Objects

    Args:
        movie_list: A list of dictionaries that contain movie details

    Returns :
        movie_results: A list of movie objects
    '''
    movie_results = []
    for movie_item in movie_list:
        id = movie_item.get('id')
        title = movie_item.get('original_title')
        overview = movie_item.get('overview')
        poster = movie_item.get('poster_path')
        vote_average = movie_item.get('vote_average')
        vote_count = movie_item.get('vote_count')

        if poster:
            movie_object=Movie(id,title,overview,poster,vote_average,vote_count)
            movie_results.append(movie_object)

    return movie_results

def get_movies(category):


    get_movies_url = base_url.format(category,api_key)
    
    # get_url = 'https://api.themoviedb.org/3/movie/{}?api_key=6d7b3c11acae661cd9252d66dddb2883'
    # get_movies_url = get_url .format(category)

    with urllib.request.urlopen(get_movies_url) as url:
        get_movies_data = url.read() #using the read function to get the response and store it in the variable
        get_movies_response = json.loads(get_movies_data) #converting JSON response to a python dictionary using the json.loads function

        movie_results = None

        if get_movies_response['results']:
            movie_result_list=get_movies_response['results']
            movie_results=process_results(movie_result_list) #process_results is a function that takes in the list of dictionary objects and returns a list of movie objects

    return movie_results




def get_movie(id):
    get_movie_details_url = base_url.format(id,api_key)
    with urllib.request.urlopen(get_movie_details_url) as url:
        movie_details_data = url.read()
        movie_details_response = json.loads(movie_details_data)

        movie_object = None

        if movie_details_response:
            id = movie_details_response.get('id')
            title = movie_details_response.get('original_title')
            overview = movie_details_response.get('overview')
            poster = movie_details_response.get('poster_path')
            vote_average = movie_details_response.get('vote_average')
            vote_count = movie_details_response.get('vote_count')

            movie_object = Movie(id,title,overview,poster,vote_average,vote_count)

    return movie_object

def search_movie(movie_name):
    search_movie_url = 'https://api.themoviedb.org/3/search/movie?api_key={}&query={}'.format(api_key,movie_name)
    with urllib.request.urlopen(search_movie_url) as url:
        search_movie_data = url.read()
        search_movie_response =json.loads(search_movie_data)

        search_movie_results = None

        if search_movie_response['results']:
            search_movie_list = search_movie_response['results']
            search_movie_results = process_results(search_movie_list)

    return search_movie_results









    