from Blueprints.BaseRequestParser import base_parser, handle_request
from Rest.UserREST import UserREST
from flask_restx import Namespace, Resource
from Rest.Answer import message_response_model

from Blueprints.APIModels import a_user, a_own_user, a_chat_with_other_user, a_chat, post_a_password, post_a_qrcode, \
    a_card, post_a_card

api = Namespace('User', path='/user', description='User related Operations')

report_parser = base_parser.copy()
report_parser.add_argument('message', location='args', required=True)

a_own_user_response = message_response_model(a_own_user, True)
a_user_response = message_response_model(a_user, single=True)

a_card_response = message_response_model(a_card, True)

chat_parser = base_parser.copy()
chat_parser.add_argument('other_user_id', location='args', required=True)

a_chat_with_other_user_response = message_response_model(a_chat_with_other_user)
a_chat_response = message_response_model(a_chat)
a_chat_single_response = message_response_model(a_chat, single=True)

default_chat_with_other_user_mask = """
message,
result{
    id,
    channel_id,
    other_user,
    last_post{
        id,
        content,
        date_created,
        linked_user,
        author_id,
        anonymous,
        image_id
    },
    last_post_read_id
}
"""


@api.route('/own_chats')
class OwnChats(Resource):
    @api.doc(parser=base_parser)
    def get(self):
        return handle_request(rest_function=UserREST.get_own_chats,
                              model=a_chat_with_other_user_response,
                              mask=default_chat_with_other_user_mask)


@api.route('/own_chat')
class OwnChat(Resource):
    @api.doc(parser=chat_parser)
    def get(self):
        return handle_request(parser=chat_parser,
                              rest_function=UserREST.get_own_chat,
                              arg_dict={'other_user_id': None},
                              model=a_chat_single_response)


@api.route('/self')
class Self(Resource):
    @api.doc(parser=base_parser)
    def get(self):
        return handle_request(rest_function=UserREST.get_self,
                              model=a_own_user_response)

    @api.doc(parser=base_parser, body=a_own_user)
    def post(self):
        return handle_request(json_data=True,
                              rest_function=UserREST.post_self,
                              model=a_own_user_response)

    @api.doc(parser=base_parser, body=post_a_password)
    def delete(self):
        return handle_request(json_data=True,
                              rest_function=UserREST.delete_self)


@api.route('/card')
class Card(Resource):
    @api.doc(parser=base_parser)
    def get(self):
        return handle_request(rest_function=UserREST.get_card,
                              model=a_card_response)

    @api.doc(parser=base_parser, body=post_a_card)
    def post(self):
        return handle_request(json_data=True,
                              rest_function=UserREST.post_card,
                              model=a_card_response)


@api.route('/<user_to_get_id>')
@api.doc(params={'user_to_get_id': 'Id of User, that will be returned'})
class User(Resource):
    @api.doc(parser=base_parser)
    def get(self, user_to_get_id):
        return handle_request(path_args=[user_to_get_id],
                              rest_function=UserREST.get_user,
                              model=a_user_response)


@api.route('/mute/<user_to_mute_id>')
@api.doc(params={'user_to_mute_id': 'Id of User, that will be muted'})
class MuteUser(Resource):
    @api.doc(parser=base_parser)
    def post(self, user_to_mute_id):
        return handle_request(path_args=[user_to_mute_id],
                              rest_function=UserREST.post_mute_user, )


@api.route('/unmute/<user_to_unmute_id>')
@api.doc(params={'user_to_unmute_id': 'Id of User, that will be unmuted'})
class UnmuteUser(Resource):
    @api.doc(parser=base_parser)
    def post(self, user_to_unmute_id):
        return handle_request(path_args=[user_to_unmute_id],
                              rest_function=UserREST.post_unmute_user, )


@api.route('/ban/<user_to_ban_id>')
@api.doc(params={'user_to_ban_id': 'Id of User that will be banned'})
class Ban(Resource):
    @api.doc(parser=base_parser)
    def post(self, user_to_ban_id):
        return handle_request(path_args=[user_to_ban_id],
                              rest_function=UserREST.post_ban_user)


@api.route('/unban/<user_to_unban_id>')
@api.doc(params={'user_to_unban_id': 'Id of User that will be unbanned'})
class UnBan(Resource):
    @api.doc(parser=base_parser)
    def post(self, user_to_unban_id):
        return handle_request(path_args=[user_to_unban_id],
                              rest_function=UserREST.post_unban_user)


@api.route('/block/<user_to_block_id>')
@api.doc(params={'user_to_block_id': 'Id of User that will be blocked'})
class Block(Resource):
    @api.doc(parser=base_parser)
    def post(self, user_to_block_id):
        return handle_request(path_args=[user_to_block_id],
                              rest_function=UserREST.post_block_user)


@api.route('/unblock/<user_to_unblock_id>')
@api.doc(params={'user_to_unblock_id': 'Id of User that will be unblocked'})
class UnBlock(Resource):
    @api.doc(parser=base_parser)
    def post(self, user_to_unblock_id):
        return handle_request(path_args=[user_to_unblock_id],
                              rest_function=UserREST.post_unblock_user)


@api.route('/qrcode')
class QRCode(Resource):
    @api.doc(parser=base_parser)
    def get(self):
        return handle_request(rest_function=UserREST.get_qr_code)

    @api.doc(parser=base_parser, body=post_a_qrcode)
    def post(self):
        return handle_request(json_data=True,
                              rest_function=UserREST.post_qr_code())
