from typing import Callable

from flask import request

from Blueprints import api
from Models import UserModel
from Rest.Answer import generic_message_model

base_parser = api.parser()
base_parser.add_argument('user_id', location='args', required=True)
base_parser.add_argument('Accept-Language', location='headers')


def handle_request(parser=base_parser, path_args: list = None, arg_dict: dict = None, json_data: bool = False,
                   rest_function: Callable = None, model=generic_message_model, mask=None, skip_none=True):
    args = parser.parse_args()
    user = UserModel.query.get(args['user_id'])
    language = args.get('Accept-Language', 'en')
    if language and 'de' in language:
        language = 'de'
    else:
        language = 'en'
    arguments = []
    if path_args:
        arguments.extend(path_args)
    if arg_dict:
        for arg in arg_dict.items():
            arguments.append(args.get(*arg))
    if json_data:
        arguments.append(request.get_json(force=True))
    answer = rest_function(user, *arguments)
    return answer.make_marshaled_response(user=user, lang=language, model=model, mask=mask, skip_none=skip_none)


# usage:
"""
return handle_request(
                      parser=,
                      path_args=,
                      arg_dict=,
                      json_data=,
                      rest_function=,
                      model=,
                      mask=,
                      skip_none=
)
"""
