from flask import (
    request,
    jsonify,
    current_app,
    render_template,
    Blueprint
)

from .npc_party import (
    create_party,
    create_npc
)
from .models import (
    Spell,
    CharacterClass
)


npc_views = Blueprint(
    'npc_views',
    __name__,
    template_folder='templates',
    url_prefix='/npc'
)

@npc_views.route('/party')
@npc_views.route('/party/<int:base_level>')
def generate_npc_party(base_level=None):
    party = None
    if base_level:
        party = create_party(base_level)

    # If asked for JSON, return the party, otherwise render HTML template
    if request.args.get('format', 'html') == 'json':
        return jsonify([npc.roll20_format for npc in party])
    return render_template('generate_npc_party.html', party=party, base_level=base_level)

@npc_views.route('/single')
@npc_views.route('/single/<int:base_level>/<int:guild_id>')
def generate_single_npc(base_level=None, guild_id=0):
    guilds = CharacterClass.query.filter(CharacterClass.bucket.notin_(['Demi-Human'])).all()

    npc = None
    if base_level:
        npc = create_npc(base_level, guild_id)

    # If asked for JSON, return the npc, otherwise render HTML template
    if request.args.get('format', 'html') == 'json':
        return jsonify(npc)
    return render_template('generate_single_npc.html', npc=npc, base_level=base_level, guilds=guilds, guild_id=guild_id)

@npc_views.route('/spells')
def spell_list():
    spells = Spell.query.all()
    return render_template('spell_list.html', spells=spells)
