# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from bson.json_util import dumps
from flask import Blueprint, jsonify, Response, request
import markdown as md

from ..extensions import omim
from ..models import Institute, Case
from ..helpers import get_document_or_404

TERMS_MAPPER = {
  'Autosomal recessive': 'AR',
  'Autosomal dominant': 'AD',
  'X-linked dominant': 'XD',
  'X-linked recessive': 'XR',
  'Autosomal dominant; Isolated cases': 'AD'
}

mkd = md.Markdown()

api = Blueprint('api', __name__, url_prefix='/api/v1')


@api.route('/omim/gene/<hgnc_symbol>')
def omim_gene(hgnc_symbol):
  return jsonify(**omim.gene(hgnc_symbol))


@api.route('/omim/inheritance/<hgnc_symbol>')
def omim_inheritance(hgnc_symbol):
  entry = omim.gene(hgnc_symbol)
  models = set(phenotype['inheritance'] for phenotype in entry['phenotypes'])
  terms = [TERMS_MAPPER.get(model_human, model_human) for model_human in models]

  return jsonify(models=terms)


@api.route('/<institute_id>/cases')
def cases(institute_id):
  institute = Institute.objects.get(display_name=institute_id)
  cases_json = dumps([case.to_mongo() for case in institute.cases])

  return Response(cases_json, mimetype='application/json; charset=utf-8')


@api.route('/<institute_id>/<case_id>/status', methods=['PUT'])
def case_status(institute_id, case_id):
  case = get_document_or_404(Case, case_id)
  case.status = request.json.get('status', case.status)
  case.save()

  # TODO: create a new event here!

  return jsonify(status=case.status, ok=True)


@api.route('/markdown', methods=['POST'])
def markdown():
  """Convert a Markdown string to HTML"""
  mkd_string = request.json.get('markdown')
  html_string = mkd.convert(mkd_string)

  return jsonify(html=html_string)

@@ -305,19 +305,16 @@
 ERMS_MAP
-PER
  = %7B%0A  '
@@ -478,16 +478,124 @@
 'AD'%0A%7D%0A%0A
+# markdown to HTML converter object%0A# can't use Flask-Markdown object since it doesn't support %60%60init_app%60%60%0A
 mkd = md
@@ -737,131 +737,553 @@
 :%0A  
-return jsonify(**omim.gene(hgnc_symbol))%0A%0A%0A@api.route('/omim/inheritance/%3Chgnc_symbol%3E')%0Adef omim_inheritance(hgnc_symbol):
+%22%22%22Query OMIM for extended information on a specific gene.%0A%0A  Args:%0A    hgnc_symbol (str): HGNC symbol for a gene%0A%0A  Returns:%0A    Response: jsonified %60%60dict%60%60 of gene specific information%0A  %22%22%22%0A  # query using the OMIM extension%0A  return jsonify(**omim.gene(hgnc_symbol))%0A%0A%0A@api.route('/omim/inheritance/%3Chgnc_symbol%3E')%0Adef omim_inheritance(hgnc_symbol):%0A  %22%22%22Query OMIM for inheritance model of a specific gene.%0A%0A  Args:%0A    hgnc_symbol (str): HGNC symbol for a gene%0A%0A  Returns:%0A    Response: jsonified %60%60dict%60%60 of inheritance model terms%0A  %22%22%22
 %0A  e
@@ -1414,11 +1414,8 @@
 _MAP
-PER
 .get
@@ -1563,16 +1563,203 @@
 te_id):%0A
+  %22%22%22Fetch all cases belonging to a specific institute.%0A%0A  Args:%0A    institute_id (str): unique institute display name%0A%0A  Returns:%0A    Response: jsonified MongoDB objects as a list%0A  %22%22%22%0A
   instit
@@ -2062,16 +2062,64 @@
 se_id):%0A
+  %22%22%22Update (PUT) status of a specific case.%22%22%22%0A
   case =
@@ -2405,16 +2405,240 @@
  to HTML
+.%0A%0A  Retrives the content (str) in the request object for the key%0A  %60%60markdown%60%60 and converts it into HTML using a standard Markdown%0A  converter.%0A%0A  Returns:%0A    Response: jsonified %60%60dict%60%60 with the converted HTML string%0A  
 %22%22%22%0A  mk

add inline docs to api routes