from eve.render import send_response
from flask import request, send_from_directory

from taarifa_api import api as app, main

app.name = 'TaarifaWaterpoints'


@app.route('/' + app.config['URL_PREFIX'] + '/waterpoints/values/<field>')
def waterpoint_values(field):
    "Return the unique values for a given field in the waterpoints collection."
    # FIXME: Direct call to the PyMongo driver, should be abstracted
    resources = app.data.driver.db['resources']
    if request.args:
        resources = resources.find(dict(request.args.items()))
    return send_response('resources', (resources.distinct(field),))


@app.route('/' + app.config['URL_PREFIX'] + '/waterpoints/stats')
def waterpoint_stats():
    "Return number of waterpoints grouped by district and status."
    # FIXME: Direct call to the PyMongo driver, should be abstracted
    resources = app.data.driver.db['resources']
    return send_response('resources', (resources.group(
        ['district', 'status'], dict(request.args.items()),
        initial={'count': 0},
        reduce="function(curr, result) {result.count++;}"),))


@app.route('/' + app.config['URL_PREFIX'] + '/waterpoints/status')
def waterpoint_status():
    "Return number of waterpoints grouped by status."
    # FIXME: Direct call to the PyMongo driver, should be abstracted
    resources = app.data.driver.db['resources']
    return send_response('resources', (resources.group(
        ['status'], dict(request.args.items()), initial={'count': 0},
        reduce="function(curr, result) {result.count++;}"),))


@app.route('/' + app.config['URL_PREFIX'] + '/waterpoints/count_by/<field>')
def waterpoint_count_by(field):
    "Return number of waterpoints grouped a given field."
    # FIXME: Direct call to the PyMongo driver, should be abstracted
    resources = app.data.driver.db['resources']
    return send_response('resources', (resources.group(
        field.split(','), dict(request.args.items()), initial={'count': 0},
        reduce="function(curr, result) {result.count++;}"),))


@app.route('/' + app.config['URL_PREFIX'] + '/waterpoints/stats_by/<selector>')
def waterpoint_stats_by(selector):
    """Return number of waterpoints of a given status grouped by a certain
    attribute."""
    # FIXME: Direct call to the PyMongo driver, should be abstracted
    resources = app.data.driver.db['resources']
    return send_response('resources', (resources.aggregate([
        {"$match": dict(request.args.items())},
        {"$group": {"_id": {selector: "$" + selector,
                            "status": "$status"},
                    "statusCount": {"$sum": 1},
                    "populationCount": {"$sum": "$population"}}},
        {"$group": {"_id": "$_id." + selector,
                    "waterpoints": {
                        "$push": {
                            "status": "$_id.status",
                            "count": "$statusCount",
                            "population": "$populationCount",
                        },
                    },
                    "count": {"$sum": "$statusCount"}}},
        {"$project": {"_id": 0,
                      selector: "$_id",
                      "waterpoints": 1,
                      "population": 1,
                      "count": 1}}])['result'],))


@app.route('/scripts/<path:filename>')
def scripts(filename):
    return send_from_directory(app.root_path + '/dist/scripts/', filename)


@app.route('/styles/<path:filename>')
def styles(filename):
    return send_from_directory(app.root_path + '/dist/styles/', filename)


@app.route('/images/<path:filename>')
def images(filename):
    return send_from_directory(app.root_path + '/dist/images/', filename)


@app.route('/views/<path:filename>')
def views(filename):
    return send_from_directory(app.root_path + '/dist/views/', filename)


@app.route("/")
def index():
    return send_from_directory(app.root_path + '/dist/', 'index.html')


@app.route("/dashboard")
def dashboard():
    return send_from_directory(app.root_path + '/dash/', 'index.html')


@app.route("/favicon.ico")
def favicon():
    return send_from_directory(app.root_path + '/dist/', 'favicon.ico')

if __name__ == '__main__':
    main()

@@ -2095,24 +2095,21 @@
 ats_by/%3C
-selector
+field
 %3E')%0Adef 
@@ -2128,24 +2128,21 @@
 tats_by(
-selector
+field
 ):%0A    %22
@@ -2229,16 +2229,247 @@
 ute.%22%22%22%0A
+    # Some fields require an addtional projection%0A    project = %7B'construction_year': %7B%22$cond%22: %5B%22$_id%22,%0A                                               %7B%22$year%22: %22$_id%22%7D,%0A                                               %22unknown%22%5D%7D%7D%0A
     # FI
@@ -2718,32 +2718,26 @@
 %22: %7B
-selector: %22$%22 + selector
+field: %22$%22 + field
 ,%0A  
@@ -2935,24 +2935,21 @@
 _id.%22 + 
-selector
+field
 ,%0A      
@@ -3347,24 +3347,41 @@
     
-selector:
+field: project.get(field,
  %22$_id%22
+)
 ,%0A  

Add projection to only return construction year for stats_by endpoint