'use strict';

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

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

        this.coroutines.findResource.main = Promise.coroutine(function* (req, res, next, config) {
            let query, q, maxdist = parseFloat(req.query.radius || 2000) / 6371,
                limit = Math.min(Math.max(parseInt(req.query.limit || 10000), 0), 10000),
                sort = req.query.sort || {'updated': -1},
                skip = Math.max(parseInt(req.query.skip || 0), 0),
                uuid = req.params.region_uuid || req.params.uuid,
                region = yield mongoose.model('Region').findOne({$or: [{uuid: uuid}, {slug: uuid}]}),
                isAdmin = req.api_key && (
                    req.api_key.scopes.indexOf('admin') ||
                    req.api_key.scopes.indexOf('region-' + region.uuid)
                );
                
                isAdmin = isAdmin == -1 || !isAdmin ? false: true;

            if (!region && !config.query.user_mapping) {
                return rHandler.handleErrorResponse(new restify.NotFoundError(), res, next);
            }

            query = require('../lib/util/query-mapping')({}, req, config);
            if(!isAdmin) {
                query = conditionalAdd(query, 'hidden', false,!isAdmin);
            }
            
            query = conditionalAdd(query, 'region_uuid', region ? region.uuid : undefined);
            query = conditionalAdd(query, 'lonlat', {
                $near: [parseFloat(req.query.longitude || 10.0014), parseFloat(req.query.latitude || 53.5653)],
                $maxDistance: maxdist
            }, (req.query.longitude !== undefined && req.query.latitude !== undefined));

            q = mongoose.model(config.resource).find(query);
            q = q.sort(sort);
            q = config.select ? q.select(config.select) : q;
            q = q.skip(skip).limit(limit);

            var data = { page: Math.floor(skip / limit), pagesize: limit };
            data.results = yield q.exec();
            data.total = yield mongoose.model(config.resource).count(q._conditions);

            data.results = yield Promise.map(data.results, Promise.coroutine(function* (result) {
                result = result.toObject();
                result.user = yield mongoose.model('User').findOne({uuid: result.user_uuid})
                    .select('uuid nickname').exec();
                result.user = result.user ? result.user.toObject() : undefined;
                result.region = yield mongoose.model('Region').findOne({uuid: result.region_uuid})
                    .select('uuid title slug').exec();
                result.region = result.region ? result.region.toObject() : undefined;
                let photo = yield mongoose.model('Photo').findOne({location_uuid: result.uuid}).exec();
                result = conditionalAdd(result, 'photo', photo ? photo.toObject() : undefined);
                return result;
            }));

            rHandler.handleDataResponse(data, 200, res, next);
        });

        this.coroutines.searchResource = {
            pre: Promise.resolve,
            main: Promise.coroutine(function* (req, res, next, config) {
                var parts = req.params.q.split(' '),
                    query = {
                        $and: parts.map(function (part) {
                            var reg = new RegExp(part, 'i');
                            return {
                                $and: [
                                    { hidden: false },
                                    {
                                        $or: [
                                            {title: {$regex: reg}},
                                            {description: {$regex: reg}},
                                            {owner: {$regex: reg}},
                                            {street: {$regex: reg}},
                                            {city: {$regex: reg}},
                                            {postcode: {$regex: reg}},
                                            {street: {$regex: reg}}
                                        ]
                                    }
                                ]
                            };
                        })
                    };
                if (req.params.uuid) {
                    query.region_uuid = req.params.uuid;
                }
                var q = mongoose.model('Location').find(query);
                q = config.select ? q.select(config.select) : q;
                let result = yield q.exec(),
                    regions = {};
                if (Array.isArray(result)) {
                    for (let i = 0; i < result.length; i+=1) {
                        let item = result[i].toObject();
                        if (!regions.hasOwnProperty(item.region_uuid)) {
                            let region = yield mongoose.model('Region').findOne({uuid: item.region_uuid});
                            if (region) {
                                regions[item.region_uuid] = region.slug;
                            }
                        }
                        item.region_slug = regions[item.region_uuid];
                        result[i] = item;
                    }
                }
                rHandler.handleDataResponse(result, 200, res, next);
            })
        };
    }
}

module.exports = LocationsController;
@@ -848,119 +848,33 @@
     
-region = yield mongoose.model('Region').findOne(%7B$or: %5B%7Buuid: uuid%7D, %7Bslug: uuid%7D%5D%7D),%0A                isAdmin =
+isAdmin = req.api_key && 
  req
@@ -877,24 +877,31 @@
  req.api_key
+.scopes
  && (%0A      
@@ -949,16 +949,21 @@
 'admin')
+ %3E -1
  %7C%7C%0A    
@@ -1021,20 +1021,18 @@
 ' + 
-region.
 uuid)
+ %3E -1
 %0A   
@@ -1080,63 +1080,317 @@
     
-    isAdmin = isAdmin == -1 %7C%7C !isAdmin ? false: true;%0A
+let region_query = %7B$or: %5B%7Buuid: uuid%7D, %7Bslug: uuid.toLowerCase()%7D%5D%7D;%0A            if(!isAdmin) %7B%0A                region_query = %7B$and: %5B%7Bhide: false%7D, %7B$or: %5B%7Buuid: uuid%7D, %7Bslug: uuid.toLowerCase()%7D%5D%7D%5D%7D;%0A            %7D%0A            let region = yield mongoose.model('Region').findOne(region_query);%0A            
 %0A   
@@ -1412,16 +1412,34 @@
 egion &&
+ (!config.query %7C%7C
  !config
@@ -1458,16 +1458,17 @@
 mapping)
+)
  %7B%0A     

fix location listing for editors