'use strict';

var Router = require('koa-router');
var middleware = require('../middleware');

var validateEntry = middleware.validateParameters([
    { name: 'entryId', type: 'objectid' }
]);
var validateAttachment = middleware.validateParameters([
    { name: 'entryId', type: 'objectid' },
    { name: 'attachmentId', type: 'objectid' }
]);

module.exports = function (hds) {

    var router = new Router();

    router.post('/:kind', checkKind, createEntry);

    router.get('/:kind/:entryId', validateEntry, checkKind, checkEntry, getEntry);

    router.put('/:kind/:entryId', validateEntry, checkKind, checkEntry, changeEntry);

    router.delete('/:kind/:entryId', validateEntry, checkKind, checkEntry, deleteEntry);

    router.get('/:kind/:entryId/:attachmentId', validateAttachment, checkKind, checkEntry, getAttachment);

    return router.middleware();

    function* checkKind(next) {
        try {
            this.state.hds_kind = yield hds.Kind.get(this.params.kind);
        } catch (e) {
            return this.hds_jsonError(404, 'kind ' + this.params.kind + ' not found');
        }
        yield next;
    }

    function* checkEntry(next) {
        var entry = yield this.state.hds_kind.findOne({_id: this.params.entryId}).exec();
        if (entry) {
            this.state.hds_entry = entry;
            yield next;
        } else {
            this.hds_jsonError(404, 'entry ' + this.params.entryId + ' not found');
        }
    }

    function* getEntry() {
        var entry = this.state.hds_entry.toObject();
        if (entry._at) {
            var i = 0, l = entry._at.length, at;
            for (; i < l; i++) {
                at = entry._at[i];
                at.url = this.state.hds_host + 'entry/' + this.params.kind + '/' + entry._id + '/' + at._id;
            }
        }
        this.body = entry;
    }

    function* getAttachment() {
        var entry = this.state.hds_entry;
        try {
            var att = yield entry.getAttachment(this.params.attachmentId, true);
            this.set('Content-Type', att.mimetype);
            this.set('Content-Disposition', 'attachment;filename="' + att.filename + '"');
            this.body = att.stream;
        } catch (e) {
            this.hds_jsonError(404, 'attachment ' + this.params.attachmentId + ' not found');
        }
    }

    function* createEntry() {
        var data = this.query;
        try {
            var value = yield this.state.hds_kind.create(this.params.kind, data).save();
            this.body = {
                status: 'created',
                entryID: value._id
            };
        } catch(err) {
            this.hds_jsonError(500, err);
        }
    }

    function* deleteEntry() {
        try {
            yield this.state.hds_entry.remove();
            this.body = {status: 'deleted'};
        } catch (err) {
            this.hds_jsonError(500, err);
        }
    }

    function* changeEntry() {
        try {
            yield this.state.hds_entry.update({ $set: this.params.data});
            this.body = {status: 'modified'};
        } catch (err) {
            this.hds_jsonError(500, err);
        }
    }
};
@@ -822,24 +822,112 @@
 tachment);%0A%0A
+    router.get('/:kind/_find', validateEntry, checkKind, multipleEntries, queryEntry);%0A%0A
     return r
@@ -1531,32 +1531,367 @@
        %7D%0A    %7D%0A%0A
+    function* multipleEntries(next) %7B%0A        var entry = yield this.state.hds_kind.find(this.request.find.query).exec();%0A        if (entry) %7B%0A            this.state.hds_entries = entry;%0A            yield next;%0A        %7D else %7B%0A            this.hds_jsonError(404, 'entries ' + this.request.find.query + ' not found');%0A        %7D%0A    %7D%0A%0A
     function* ge
@@ -3567,11 +3567,563 @@
  %7D%0A    %7D
+%0A%0A    function* queryEntry() %7B%0A        try %7B%0A            var from = this.request.find.from %7C%7C 0;%0A            var limit = this.request.find.limit %7C%7C 20;%0A            var entries = yield this.state.hds_entries.aggregate(%5B%0A                %7B$skip: from%7D,%0A                %7B$limit: limit%7D%0A            %5D).exec();%0A            this.body = %7B%0A                from: from,%0A                to: limit,%0A                total: (limit - from),%0A                entry: entries%0A            %7D;%0A        %7D catch (err) %7B%0A            this.hds_jsonError(500, err);%0A        %7D%0A    %7D
 %0A%7D;

make complex queries on entries