import json, logging
from flask import Flask, jsonify, request, Response
from flask_cors import CORS
from helpers import LANGUAGES
from helpers.ChallengeHelper import LoadChallenge, LoadChallenges
from helpers.LeaderboardHelper import LoadLeaderboard
from helpers.SubmissionHelper import CreateSubmission, LoadSubmission
from helpers.SubmissionResultHelper import LoadSubmissionResult
from helpers.UserHelper import CreateOrGetUser, LoadUser

logging.getLogger().setLevel(logging.DEBUG)

# -------------------------------------------------------------------

app = Flask(__name__)
CORS(app)

# -------------------------------------------------------------------

def jsonify_err(code, error):
	res = jsonify(error=error)
	res.status_code = code
	return res

class CustomEncoder(json.JSONEncoder):
	def default(self, obj):
		if isinstance(obj, set):
			return list(obj)
		return json.JSONEncoder.default(self, obj)

app.json_encoder = CustomEncoder

# -------------------------------------------------------------------
# -------------------------------------------------------------------
# -------------------------------------------------------------------

@app.route('/challenge/<string:challenge_id>')
def challenge_get(challenge_id):
	challenge = LoadChallenge(challenge_id)
	return jsonify(challenge) if challenge else jsonify_err(400, 'Cannot load challenge, it may not exist')

# -------------------------------------------------------------------

@app.route('/challenges')
def challenge_list():
	return jsonify(LoadChallenges()) 

# -------------------------------------------------------------------
# -------------------------------------------------------------------
# -------------------------------------------------------------------

@app.route('/languages')
def languages_list():
	return jsonify(LANGUAGES) 

# -------------------------------------------------------------------
# -------------------------------------------------------------------
# -------------------------------------------------------------------

@app.route('/user', methods=[ 'POST' ])
def user_create():
	user = CreateOrGetUser(**request.get_json())
	return jsonify(user) if user else jsonify_err(400, 'Cannot create user, check inputs')

# -------------------------------------------------------------------

@app.route('/user/<string:user_id>')
def user_get(user_id):
	user = LoadUser(user_id)
	return jsonify(user) if user else jsonify_err(400, 'Cannot load user, it may not exist')

# -------------------------------------------------------------------
# -------------------------------------------------------------------
# -------------------------------------------------------------------

@app.route('/submission', methods=[ 'POST' ])
def submission_create():
	submission = CreateSubmission(**request.get_json())
	return jsonify(submission) if submission else jsonify_err(400, 'Cannot create submission, check inputs')

# -------------------------------------------------------------------

@app.route('/submission/<string:submission_id>')
def submission_get(submission_id):
	submission = LoadSubmission(submission_id)
	return jsonify(submission) if submission else jsonify_err(400, 'Cannot load submission, it may not exist')

# -------------------------------------------------------------------

@app.route('/submission/<string:submission_id>/result')
def submission_result_get(submission_id):
	result = LoadSubmissionResult(submission_id)
	return jsonify(result) if result else jsonify_err(400, 'Cannot load submission result, it may not exist')

# -------------------------------------------------------------------
# -------------------------------------------------------------------
# -------------------------------------------------------------------

@app.route('/leaderboard')
def leaderboard_list():
	return jsonify(LoadLeaderboard()) 

# -------------------------------------------------------------------
