'use strict';

var mongoose = require('mongoose'),
    Promise = require('bluebird'),
    rHandler = require('../lib/util/response-handlers'),
    CommonController = require('./common');

class RegionsController extends CommonController {
    constructor() {
        super();

        this.coroutines.findResource.main = Promise.coroutine(function* (req, res, next, config) {
            if (req.params.lat && req.params.lon) {
                var limit = req.params.limit || 1,
                    q = mongoose.model('Region').where('lonlat');
                q.near({
                    center: {coordinates: [parseFloat(req.params.lon), parseFloat(req.params.lat)], type: 'Point'}
                });
                q.limit(parseInt(limit));
                q.exec().then(function (results) {
                    rHandler.handleDataResponse(results, 200, res, next);
                });
            } else {
                return Promise.resolve()
                    .then(function () {
                        return mongoose.model('Location').mapReduce({
                            map: function () {
                                if (!this.hide) {
                                    emit(this.region_uuid, 1);
                                }
                            },
                            reduce: function (k, v) {
                                return Array.sum(v);
                            }
                        });
                    })
                    .then(function (results) {
                        var output = [];
                        return Promise.map(results, function (item) {
                                return mongoose.model('Region').findOne({uuid: item._id, hide:false})
                                    .then(function (region) {
                                        if (region) {
                                            var out = region.toObject();
                                            out.locations = item.value;
                                            output.push(out);
                                        }
                                    });
                            }, {concurrency: 1})
                            .then(function () {
                                return output;
                            });
                    })
                    .then(function (results) {
                        rHandler.handleDataResponse(results, 200, res, next);
                    });
            }
        });

    }
}

module.exports = RegionsController;
@@ -719,32 +719,396 @@
    q
-.limit(parseInt(limit));
+ = config.select ? q.select(config.select) : q;%0A                if (req.params.skip) %7B%0A                    q = q.skip(parseInt(req.params.skip));%0A                %7D%0A                if (req.params.limit) %7B%0A                    q = q.limit(parseInt(req.params.limit));%0A                %7D%0A                if (req.params.sort) %7B%0A                    q = q.sort(req.params.sort);%0A                %7D
 %0A   
@@ -1922,16 +1922,598 @@
 t = %5B%5D;%0A
+%0A                        function sortOn(property) %7B%0A                            var sortOrder = 1;%0A                            if (property%5B0%5D === %22-%22) %7B%0A                                sortOrder = -1;%0A                                property = property.substr(1);%0A                            %7D%0A                            return function (a, b) %7B%0A                                var result = (a%5Bproperty%5D %3C b%5Bproperty%5D) ? -1 : (a%5Bproperty%5D %3E b%5Bproperty%5D) ? 1 : 0;%0A                                return result * sortOrder;%0A                            %7D%0A                        %7D%0A%0A
         
@@ -3130,16 +3130,16 @@
 cy: 1%7D)%0A
-
         
@@ -3170,32 +3170,195 @@
 n(function () %7B%0A
+                                if (req.params.sort) %7B%0A                                    output.sort(sortOn(req.params.sort));%0A                                %7D%0A
                 

fix region sorting