import {Microservices} from '../configs/microservices';
import rp from 'request-promise';

export default {
    name: 'decktree',
    // At least one of the CRUD methods is Required
    read: (req, resource, params, config, callback) => {
        let args = params.params? params.params : params;
        let selector= {'id': String(args.id), 'spath': args.spath, 'sid': String(args.sid), 'stype': args.stype};
        if(resource === 'decktree.nodes'){
            /*********connect to microservices*************/
            rp.get({uri: Microservices.deck.uri + '/decktree/' + selector.id}).then((res) => {
                callback(null, {deckTree: JSON.parse(res), selector: selector, 'page': params.page, 'mode': args.mode});
            }).catch((err) => {
                //console.log(err);
                //we should report the error to the action creator
                callback({msg: 'Error in retrieving data from ' + Microservices.deck.uri + ' service! Please try again later...', details: err}, {});
            });
        }
    },
    create: (req, resource, params, body, config, callback) => {
        let args = params.params? params.params : params;
        let selector= {'id': String(args.id), 'spath': args.spath, 'sid': String(args.sid), 'stype': args.stype};
        let nodeSpec = {'id': String(args.nodeSpec.id), 'type': args.nodeSpec.type};
        if(resource === 'decktree.node'){
            /*********connect to microservices*************/
            rp.post({
                uri: Microservices.deck.uri + '/decktree/node/create',
                body:JSON.stringify({
                    selector: selector,
                    nodeSpec: nodeSpec,
                    //todo: send the right user id
                    user: '1'
                })
            }).then((res) => {
                callback(null, {node: JSON.parse(res), selector: args.selector});
            }).catch((err) => {
                console.log(err);
                callback(null, {node: {}, selector: args.selector});
            });
        }
    },
    update: (req, resource, params, body, config, callback) => {
        let args = params.params? params.params : params;
        let selector= {'id': String(args.id), 'spath': args.spath, 'sid': String(args.sid), 'stype': args.stype};
        if(resource === 'decktree.nodeTitle'){
            // only update if the value has changed
            if(params.oldValue === params.newValue){
                callback(null, params);
            }
            /*********connect to microservices*************/
            rp.put({
                uri: Microservices.deck.uri + '/decktree/node/rename',
                body:JSON.stringify({
                    //todo: send the right user id
                    user: '1',
                    selector: selector,
                    name: params.newValue
                })
            }).then((res) => {
                callback(null, params);
            }).catch((err) => {
                console.log(err);
                callback(null, params);
            });
        }
    },
    delete: (req, resource, params, config, callback) => {
        let args = params.params? params.params : params;
        let selector= {'id': String(args.id), 'spath': args.spath, 'sid': String(args.sid), 'stype': args.stype};
        if(resource === 'decktree.node'){
            /*********connect to microservices*************/
            let options = {
                method: 'DELETE',
                uri: Microservices.deck.uri + '/decktree/node/delete',
                body:JSON.stringify({
                    //todo: send the right user id
                    user: '1',
                    selector: selector
                })
            };
            rp(options).then((res) => {
                callback(null, params);
            }).catch((err) => {
                console.log(err);
                callback(null, params);
            });
        }
    }
};

@@ -1198,32 +1198,41 @@
 d': String(args.
+selector.
 id), 'spath': ar
@@ -1226,32 +1226,41 @@
 , 'spath': args.
+selector.
 spath, 'sid': St
@@ -1261,32 +1261,41 @@
 d': String(args.
+selector.
 sid), 'stype': a
@@ -1290,32 +1290,41 @@
 , 'stype': args.
+selector.
 stype%7D;%0A        
@@ -2251,32 +2251,41 @@
 d': String(args.
+selector.
 id), 'spath': ar
@@ -2279,32 +2279,41 @@
 , 'spath': args.
+selector.
 spath, 'sid': St
@@ -2314,32 +2314,41 @@
 d': String(args.
+selector.
 sid), 'stype': a
@@ -2343,32 +2343,41 @@
 , 'stype': args.
+selector.
 stype%7D;%0A        

fix a minor issue: decktree selector parameters fixed