import logging

from flask import Blueprint, render_template, request, redirect, url_for, flash, g, jsonify
from flask.ext.login import login_required

# Project 5 - Steve - adjusting imports to minimal set after model changes.
from talent_match import db
from ..models.talentInfo import Category, Skill
from ..forms import EditSkillForm

from functools import wraps
from talent_match import db

logger = logging.getLogger(__name__)
app = Blueprint(
    'skills', __name__, template_folder="templates", url_prefix="/skills")


def admin_required(f):
    @wraps(f)
    @login_required
    def decorated(*args, **kwargs):
        if not g.user.is_admin:
            return redirect(url_for('auth.login'))
        return f(*args, **kwargs)
    return decorated


@app.route('/', methods=['GET', 'POST'])
@admin_required
def listSkills():
    # original test stuff
    # user = dict(isAdmin = True, name='Steve', email='test-only-a-test')
    # talents = [ dict(name='Harp', category='Music'), dict(name='Flute', category='Music')]
    # form = PickCategoriesForm()

    categoryID = request.values.get('categoryID')
    skillList = []
    category = None
    categoryName = None
    categoryFirst = True  # display category first or skill first in the table
    if categoryID:
        print("Looking up skills by categoryID=" + categoryID)
        category = Category.query.get(categoryID)
    if category:
        # show skills for a specific category
        # since the category is fixes, list the skill column first
        categoryFirst = False
        myCategoryID = categoryID
        for cat, skill in db.session.query(Category, Skill).\
                filter(Category.id == Skill.categoryID).\
                filter(Skill.categoryID == myCategoryID).all():
            newSkill = dict(name=skill.name, categoryName=cat.name, description=skill.description,
                            count='Not available yet', id=skill.id, deleted=skill.deleted)
            skillList.append(newSkill)