/*global config*/
import Resource from '../services/ResourceProvider';
import { fromJS } from 'immutable';
import humanizeDuration from 'humanize-duration';
import Cookies from 'js-cookie';

function _parse(params, resp) {
  const builds = resp.map((build) => {
    build.blazarPath = `/builds/branch/${params.branchId}/build/${build.buildNumber}`;
    if (build.endTimestamp && build.startTimestamp) {
      build.duration = humanizeDuration(build.endTimestamp - build.startTimestamp, {round: true});
    }
    return build;
  });
  
  return fromJS(builds);
}

function _parseModules(resp) {
  return resp.filter((module) => {
    return module.active;
  });
}

function fetchBranchBuildHistory(params, cb) {
  const branchBuildHistoryPromise = new Resource({
    url: `${config.apiRoot}/builds/history/branch/${params.branchId}`,
    type: 'GET'
  }).send();

  return branchBuildHistoryPromise.then((resp) => {
    cb(_parse(params, resp));
  });
}

function fetchBranchInfo(params, cb) {
  const branchInfoPromise = new Resource({
    url: `${config.apiRoot}/branches/${params.branchId}`,
    type: 'GET'
  }).send();

  return branchInfoPromise.then((resp) => {
    cb(resp);
  });
}

function fetchBranchModules(params, cb) {
  const branchModulesPromise = new Resource({
    url: `${config.apiRoot}/branches/${params.branchId}/modules`,
    type: 'GET'
  }).send();

  branchModulesPromise.then((resp) => {
    cb(_parseModules(resp));
  });
}

function fetchMalformedFiles(params, cb) {
  const malformedFilesPromise = new Resource({
    url: `${config.apiRoot}/branches/${params.branchId}/malformedFiles`,
    type: 'GET'
  }).send();

  malformedFilesPromise.then((resp) => {
    cb(resp);
  });
}

function _generateBuildModuleJsonBody(moduleIds, downstreamModules, resetCache) {
  return JSON.stringify({
    moduleIds: moduleIds, 
    buildDownstreams: downstreamModules,
    resetCaches: resetCache
  });
}

function triggerBuild(params, moduleIds, downstreamModules, resetCache, cb) {
  if (moduleIds === null) {
    moduleIds = [];
  }
  const username = Cookies.get(config['usernameCookie']) || null;
  const buildPromise = new Resource({
    url: `${config.apiRoot}/branches/builds/branch/${params.branchId}?username=${username}`,
    type: 'POST',
    contentType: 'application/json',
    data: _generateBuildModuleJsonBody(moduleIds, downstreamModules, resetCache)
  }).send();

  buildPromise.then((resp) => {
    cb(false, resp);
  }, (error) => {
    console.warn(error);
    cb('Error triggering build. Check your console for more detail.');
  });
}

export default {
  fetchBranchBuildHistory: fetchBranchBuildHistory,
  fetchBranchInfo: fetchBranchInfo,
  fetchBranchModules: fetchBranchModules,
  fetchMalformedFiles: fetchMalformedFiles,
  triggerBuild: triggerBuild
};
@@ -2064,16 +2064,41 @@
 ername =
+ config.usernameCookie &&
  Cookies
@@ -2112,10 +2112,9 @@
 nfig
-%5B'
+.
 user
@@ -2127,19 +2127,65 @@
 okie
-'%5D) %7C%7C null
+) ? %60username=$%7BCookies.get(config.usernameCookie)%7D%60 : ''
 ;%0A  
@@ -2212,34 +2212,32 @@
  new Resource(%7B%0A
-  
   url: %60$%7Bconfig
@@ -2288,25 +2288,16 @@
 anchId%7D?
-username=
 $%7Buserna
@@ -2300,17 +2300,16 @@
 ername%7D%60
-,
 %0A    typ

fix so we do not pass "" or "null" to backend as username