from Blueprints.BaseRequestParser import base_parser, handle_request
from Rest.MediaREST import MediaREST
from flask_restx import Namespace, Resource
from Rest.Answer import message_response_model
from Blueprints.APIModels import a_media, post_submit_a_media, post_a_media

api = Namespace('Media', path='/media', description='Media related Operations')

get_media_parser = base_parser.copy()
get_media_parser.add_argument('media_id', location='args', required=True)

a_media_response = message_response_model(a_media, single=True)


@api.route('/')
class Media(Resource):
    @api.doc(parser=get_media_parser)
    def get(self):
        return handle_request(parser=get_media_parser,
                              arg_dict={'media_id': None},
                              rest_function=MediaREST.get_media,
                              model=a_media_response)

    @api.doc(parser=base_parser, body=post_a_media)
    def post(self):
        return handle_request(json_data=True,
                              rest_function=MediaREST.post_media,
                              model=a_media_response)

    @api.doc(parser=get_media_parser)
    def delete(self):
        return handle_request(parser=get_media_parser,
                              arg_dict={'media_id': None},
                              rest_function=MediaREST.delete_media)


@api.route('/like/<media_id>')
@api.doc(params={'media_id': 'Id of a media'})
class LikeMedia(Resource):
    @api.doc(parser=base_parser)
    def post(self, media_id):
        return handle_request(path_args=[media_id],
                              rest_function=MediaREST.post_like_media,
                              model=a_media_response)

    @api.doc(parser=base_parser)
    def delete(self, media_id):
        return handle_request(path_args=[media_id],
                              rest_function=MediaREST.delete_like_media,
                              model=a_media_response)


@api.route('/submit_media/<project_id>')
@api.doc(params={'project_id': 'Id of a project'})
class SubmitMedia(Resource):
    @api.doc(parser=base_parser, body=post_submit_a_media)
    def post(self, project_id):
        return handle_request(path_args=[project_id],
                              json_data=True,
                              rest_function=MediaREST.post_submit_media)
