from Blueprints.BaseRequestParser import base_parser, handle_request
from Rest.MatchesREST import MatchesREST
from flask_restx import Namespace, Resource, inputs
from datetime import datetime
from Rest.Answer import message_response_model
from Blueprints.APIModels import a_gmdm, a_id_list, a_card, a_user

api = Namespace('Matches', path='/matches', description='Match and Like related Operations')

get_matches_parser = base_parser.copy()
get_matches_parser.add_argument('amount', location='args', type=int, required=False)
get_matches_parser.add_argument("type", location='args', type=str, required=True)
get_matches_parser.add_argument("project_id", location='args', type=str)
get_matches_parser.add_argument('ProjectPreference', location='args', type=int)
get_matches_parser.add_argument('AgeGroupPreference', location='args', type=int)
get_matches_parser.add_argument('InterestsPreference', location='args', type=int)

post_like_parser = base_parser.copy()
post_like_parser.add_argument("likes_id", location='args', required=True)

post_dislike_parser = base_parser.copy()
post_dislike_parser.add_argument("dislikes_id", location='args', required=True)

group_matching_parser = base_parser.copy()
group_matching_parser.add_argument("project_id", location='args', required=False)

post_group_matching_parser = group_matching_parser.copy()
post_group_matching_parser.add_argument("status", location='args', type=inputs.boolean, required=True)

check_group_matching_parser = base_parser.copy()
check_group_matching_parser.add_argument("IDs", location='args', action='split', type=str)


def matriculation_date_to_semester(mat_date):
    date_diff = datetime.utcnow() - mat_date
    semester = int(date_diff.days / (364.25 / 2))
    return semester


a_gmdm_response = message_response_model(a_gmdm, single=True)

a_matches_response = message_response_model(a_card)

a_users_response = message_response_model(a_user)

a_id_list_response = message_response_model(a_id_list, single=True)


@api.route('/')
class Matches(Resource):
    @api.doc(parser=get_matches_parser)
    def get(self):
        return handle_request(parser=get_matches_parser,
                              arg_dict={'type': None,
                                        'ProjectPreference': None,
                                        'AgeGroupPreference': None,
                                        'InterestsPreference': None,
                                        'project_id': None,
                                        'amount': 10},
                              rest_function=MatchesREST.get_matches,
                              model=a_matches_response)


@api.route('/confirmed')
class ConfirmedMatches(Resource):
    @api.doc(parser=base_parser)
    def get(self):
        return handle_request(rest_function=MatchesREST.get_confirmed_matches,
                              model=a_users_response)


@api.route('/like')
class Like(Resource):
    @api.doc(parser=post_like_parser)
    def post(self):
        return handle_request(parser=post_like_parser,
                              arg_dict={'likes_id': None},
                              rest_function=MatchesREST.post_like_user)


@api.route('/dislike')
class Dislike(Resource):
    @api.doc(parser=post_dislike_parser)
    def post(self):
        return handle_request(parser=post_dislike_parser,
                              arg_dict={'dislikes_id': None},
                              rest_function=MatchesREST.post_dislike)


@api.route('/group_matching')
class GroupMatching(Resource):
    @api.doc(parser=post_group_matching_parser)
    def post(self):
        return handle_request(parser=post_group_matching_parser,
                              arg_dict={'project_id': None, 'status': True},
                              rest_function=MatchesREST.post_group_matching)

    @api.doc(parser=group_matching_parser)
    def get(self):
        return handle_request(parser=group_matching_parser,
                              arg_dict={'project_id': None},
                              rest_function=MatchesREST.get_group_matching,
                              model=a_gmdm_response)


@api.route('/check_group_matching')
class CheckGroupMatching(Resource):
    @api.doc(parser=check_group_matching_parser)
    def get(self):
        return handle_request(parser=check_group_matching_parser,
                              arg_dict={'IDs': []},
                              rest_function=MatchesREST.get_check_group_matching,
                              model=a_id_list_response)
