var config = require('../config').getConfig(),
    fetch = require('../fetch'),
    chalk = require('chalk'),
    Promise = require('es6-promise').Promise;

//TODO pass in config as arg to exec from commmand?
module.exports = {
    exec: (program) => {
        var requestsQueue = createRequestQueue(program, config);
        console.log(requestsQueue);
        executeRequests(program, requestsQueue).then(() => console.log('done'));
    }
}

function executeRequests(program, queue) {
    if  (program.async) {
        return executeRequestsAsync(queue);
    } else {
        return executeRequestsSync(queue);
    }
}

function executeRequestsSync(queue) { 
    return queue.reduce((sequence, request) => {
        return sequence.then(() => {
            return fetch(request); 
        }).then((res) =>{
            processResponse(res);
        }).catch((e) =>{
            console.log(e); 
        });
    }, Promise.resolve());
}

function createRequestQueue(program, config) {
    var env = config.envs[program.env] || config.envs.default;
    return program.args.reduce((queue, curr) => {
        if (!config.requests[curr]) {
            console.warn(chalk.red(`request : ${curr} not found in request.yml. Skipping.`));
            return queue;   
        };
        queue.push(createRequest(program, env, config.requests[curr]));
        return queue;
    }, []);
}

function createRequest(program, env, requestSchema) {
    var request = {};
    request.protocol = requestSchema.protocol || 'http:';
    request.host = env.split(':')[0];
    request.port = env.split(':')[1] || '80';
    request.method = requestSchema.method;
    request.path = getPath(requestSchema, program.query); 
    request.headers = getHeaders(env, requestSchema);
    return request;
}

function getPath(r, q) {
    var path = `/${r.endpoint}`;
    path = `${path}${r.query ? '?' + r.query : ''}`  
    path = `${path}${q ? (path.indexOf('?') > -1) ? '&' + q : '?' + q : ''}` 
    return path;
}

function getHeaders(env, r) {
   return Object.assign({}, r.headers, getCookies(env, r));
}

function getCookies(env, r) {
    return { Cookie: 'foo=bar;baz=qux'};
}

function executeRequestsAsync(requests) {}

function processResponse(r) {
    if (r.headers['content-type'] === 'application/json') { 
        r.body = parseJsonResponse(r.body);
    }
    console.log(r);
}

function parseJsonResponse(j) {
    var parsed = {};
    try {
        parsed = JSON.parse(j);
    } catch(e) {
        console.warn(e);    
    }
    return parsed;
}


@@ -99,24 +99,48 @@
 e('chalk'),%0A
+    qs = require('qs'),%0A
     Promise 
@@ -1781,20 +1781,508 @@
   re
-turn request
+quest.body = getRequestBody(requestSchema);%0A    return request;%0A%7D%0A%0Afunction getRequestBody(r) %7B%0A    // TODO file uploads, sanity checks%0A    var body,%0A        contentType = r.headers%5B'content-type'%5D %7C%7C 'application/json';%0A%0A    switch(contentType) %7B%0A        case 'application/json':%0A            body = JSON.stringify(r.body);%0A            break;%0A        case 'multipart/form-data':%0A            body = qs.stringify(r.body);%0A            break;%0A        default:%0A            body = '';%0A    %7D%0A    return body
 ;%0A%7D%0A

add getBody method for requests