from flask import request
from flask_restx import Resource, Namespace, reqparse

from app.functions.get_creature import get_creature
from app.functions.roll_encounter import roll_encounter

namespace = Namespace('gameplay', description='Gamma World Game Play')

parser = reqparse.RequestParser()
parser.add_argument('terrain', type=str, default='clear',
                    help='The terrain being traversed. ("clear", "mountains", "forest", "desert",'
                         '"watery", "ruins", "deathlands")')


@namespace.route('/encounter')  # resolves to: /gameplay/encounter
class RollEncounter(Resource):
    @namespace.expect(parser)
    def get(self):
        terrain = request.args.get('terrain', default="clear", type=str)
        valid_terrains = ["clear", "mountains", "forest", "desert", "watery", "ruins", "deathlands"]
        if terrain:
            if terrain.lower() in valid_terrains:
                details = dict()
                encounter = roll_encounter(terrain.lower())
                details["name"] = encounter["encounter"]
                creature_stats = get_creature(encounter["encounter"])
                if creature_stats is not None:
                    details.update(creature_stats)
                return details, 200
            else:
                return {
                    'error': 'Invalid terrain type provided.',
                    'valid_terrains': valid_terrains
                }, 400
        else:
            return {'error': 'No terrain type provided', 'valid_terrains': valid_terrains}, 400
