// Copyright 2014-2015, Renasar Technologies Inc.
/* jshint node: true */
'use strict';

var di = require('di'),
    express = require('express'),
    parser = require('body-parser');

module.exports = skusRouterFactory;

di.annotate(skusRouterFactory, new di.Provide('Http.Api.Skus'));
di.annotate(skusRouterFactory, new di.Inject(
        'Services.Waterline',
        'common-api-presenter',
        '_',
        'Q',
        'Protocol.TaskGraphRunner'
    )
);

function skusRouterFactory (waterline, presenter, _, Q, taskGraphProtocol) {
    var router = express.Router();

    /**
     * @api {get} /api/1.1/skus/ GET /
     * @apiVersion 1.1.0
     * @apiDescription get list of skus
     * @apiName skus-get
     * @apiGroup skus
     * @apiSuccess {json} skus a list of all skus, or an empty object if there are none.
     */

    router.get('/skus', presenter.middleware(function (req) {
        return waterline.skus.find(req.query);
    }));

    /**
     * @api {get} /api/1.1/skus/:identifier GET /:id
     * @apiVersion 1.1.0
     * @apiDescription get specific sku details
     * @apiName sku-get
     * @apiGroup skus
     * @apiParam {String} identifier of sku, must cast to ObjectId
     * @apiError NotFound There is no sku with the <code>identifier</code>
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "Not Found"
     *     }
     */

    router.get('/skus/:identifier', presenter.middleware(function (req) {
        return waterline.skus.findByIdentifier(req.params.identifier);
    }));

    /**
     * @api {get} /api/1.1/skus/:identifier/nodes GET /:id/nodes
     * @apiVersion 1.1.0
     * @apiDescription get nodes for specific sku
     * @apiName sku-get-nodes
     * @apiGroup skus
     * @apiParam {String} identifier of sku, must cast to ObjectId
     * @apiError NotFound There is no sku with the <code>identifier</code>
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "Not Found"
     *     }
     */

    router.get('/skus/:identifier/nodes', presenter.middleware(function (req) {
        return waterline.skus.findByIdentifier(req.params.identifier).then(function (sku) {
            return waterline.nodes.find({ sku: sku.id });
        });
    }));

    /**
     * @api {post} /api/1.1/skus POST /
     * @apiVersion 1.1.0
     * @apiDescription create a sku
     * @apiName sku-post
     * @apiGroup skus
     * @apiError E_VALIDATION attributes are invalid.
     */

    router.post('/skus', parser.json(), presenter.middleware(function (req) {
       return waterline.skus.create(req.body).then(function (sku) {
            return regenerateSkus().then(function () {
                return sku;
            });
        });
    }));

    /**
     * @api {patch} /api/1.1/skus/:identifier PATCH /:id
     * @apiVersion 1.1.0
     * @apiDescription patch specific sku
     * @apiName sku-patch
     * @apiGroup skus
     * @apiParam {String} identifier of sku, must cast to ObjectId
     * @apiError NotFound There is no sku with the <code>identifier</code>
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "Not Found"
     *     }
     */

    router.patch('/skus/:identifier', parser.json(), presenter.middleware(function (req) {
       return waterline.skus.updateByIdentifier(
            req.params.identifier,
            req.body
        ).then(function (sku) {
            return regenerateSkus().then(function () {
                return sku;
            });
        });
    }));

    /**
     * @api {delete} /api/1.1/skus/:identifier DELETE /:id
     * @apiVersion 1.1.0
     * @apiDescription Delete specific sku.
     * @apiName sku-delete
     * @apiGroup skus
     * @apiParam {String} identifier of sku, must cast to ObjectId
     * @apiError NotFound There is no sku with the <code>identifier</code>
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *       "error": "Not Found"
     *     }
     */

    router.delete('/skus/:identifier', presenter.middleware(function (req) {
        return waterline.skus.destroyByIdentifier(
            req.params.identifier
        ).then(function (sku) {
            return regenerateSkus().then(function () {
                return sku;
            });
        });
    }, { success: 204}));

    function regenerateSkus() {
        return waterline.nodes.find({}).then(function (nodes) {
            return Q.all(nodes.map(function (node) {
                return taskGraphProtocol.runTaskGraph(
                    'Graph.GenerateSku',
                    { defaults: { nodeId: node.id }}
                );
            }));
        });
    }

    return router;
}

@@ -369,28 +369,29 @@
    '
-common-api-presenter
+Http.Services.RestApi
 ',%0A 
@@ -499,25 +499,20 @@
 erline, 
-p
 res
-enter
+t
 , _, Q, 
@@ -849,36 +849,20 @@
 /skus', 
-p
 res
-enter.middleware
+t
 (functio
@@ -1429,36 +1429,20 @@
 ifier', 
-p
 res
-enter.middleware
+t
 (functio
@@ -1473,35 +1473,35 @@
  waterline.skus.
-fin
+nee
 dByIdentifier(re
@@ -2059,36 +2059,20 @@
 nodes', 
-p
 res
-enter.middleware
+t
 (functio
@@ -2111,19 +2111,19 @@
 ne.skus.
-fin
+nee
 dByIdent
@@ -2512,36 +2512,20 @@
 json(), 
-p
 res
-enter.middleware
+t
 (functio
@@ -3241,36 +3241,20 @@
 json(), 
-p
 res
-enter.middleware
+t
 (functio
@@ -4034,28 +4034,12 @@
 r', 
-p
 res
-enter.middleware
+t
 (fun

add rest to skus