module.exports = function combineGetDataRequest(recordId, subflag, currentUserId, dataFactory, WorkflowService, NotionService, $http, $q) {
  var urls = getUrls(recordId, subflag, currentUserId, dataFactory, WorkflowService, NotionService);
  var recordPromise = $q.defer();
  var workflowPromise = $q.defer();
  var notionListPromise = $q.defer();
  var tempNotionsPromise = $q.defer();

  var promise = $http.get('/mobileoa/combinereq', {
    params: {
      urls: urls
    }
  }).then(function(response) {
    var record = response.data[0].body;
    var workflow = response.data[1].body;
    var notionList = response.data[2].body;
    var tempNotions = {ideas: []};
    var tempNotionIdeas = dataFactory.config.tempNotions;
    if (!_.isEmpty(tempNotionIdeas)) {
      _.forEach(tempNotionIdeas, function(idea, index) {
        var idea = response.data[3 + index].body.content;
        tempNotions.ideas.push(idea);
        tempNotions[idea] = idea;
      });
      if (tempNotionIdeas.length === 1) {
        tempNotions.tempNotion = tempNotions.ideas[0];
      }
    }
    recordPromise.resolve(record);
    workflowPromise.resolve(workflow);
    notionListPromise.resolve(notionList);
    tempNotionsPromise.resolve(tempNotions);
  }, function(error) {
    recordPromise.reject(error);
    workflowPromise.reject(error);
    notionListPromise.reject(error);
    tempNotionsPromise.reject(error);
  });

  promise.recordPromise = recordPromise.promise;
  promise.workflowPromise = workflowPromise.promise;
  promise.notionListPromise = notionListPromise.promise;
  promise.tempNotionsPromise = tempNotionsPromise.promise;

  return promise;
}

function getUrls(recordId, subflag, currentUserId, dataFactory, WorkflowService, NotionService) {
  var flowType = dataFactory.config.flowType;
  var tempNotions = dataFactory.config.tempNotions;
  var urls = [
    dataFactory.remoteDataService.getQueryDataUrl(recordId),
    WorkflowService.getQueryWorkFlowInfoUrl(currentUserId, flowType, recordId, subflag || '0'),
    NotionService.getQueryNotionUrl(flowType, recordId)
  ];

  if (!_.isEmpty(tempNotions)) {
    urls = urls.concat(_.map(tempNotions, function(ideaName) {
      return NotionService.getQueryTempNotionUrl(ideaName, recordId, currentUserId, flowType);
    }));
  }

  return urls;
}
