from __future__ import unicode_literals, division, absolute_import
from builtins import *  # pylint: disable=unused-import, redefined-builtin

import copy
import logging
from math import ceil

from flask import jsonify
from flask import request
from sqlalchemy.orm.exc import NoResultFound

from flexget.api import api, APIResource
from flexget.plugins.list import movie_list as ml
from flexget.utils.tools import split_title_year

log = logging.getLogger('movie_list')

movie_list_api = api.namespace('movie_list', description='Movie List operations')

default_error_schema = {
    'type': 'object',
    'properties': {
        'status': {'type': 'string'},
        'message': {'type': 'string'}
    }
}
empty_response = api.schema('empty', {'type': 'object'})

default_error_schema = api.schema('default_error_schema', default_error_schema)
empty_response = api.schema('empty_response', empty_response)

allowed_ids = ml.SUPPORTED_IDS

input_movie_list_id_object = {
    'type': 'array',
    'items': {
        'type': 'object',
        'minProperties': 1,
        'additionalProperties': True
    }
}

input_movie_entry = {
    'type': 'object',
    'properties': {
        'title': {'type': 'string'},
        'original_url': {'type': 'string'},
        'movie_name': {'type': 'string'},
        'movie_year': {'type': 'integer'},
        'movie_identifiers': input_movie_list_id_object
    },
    'additionalProperties': True,
    'required': ['original_url'],
    'anyOf': [
        {'required': ['title']},
        {'required': ['movie_name', 'movie_year']}
    ]
}

return_movie_list_id_object = copy.deepcopy(input_movie_list_id_object)
return_movie_list_id_object.update(
    {'properties': {
        'id': {'type': 'integer'},
        'added_on': {'type': 'string'},
        'movie_id': {'type': 'integer'}
    }})

movie_list_object = {
    'type': 'object',
    'properties': {
        'title': {'type': 'string'},
        'added_on': {'type': 'string'},