var angular = require('angular');
require('../modules');

var module = angular.module('flow.services');
module.factory('NotionService', function($q, $http) {
  var notionService = {};

  /**
   * 获取意见
   * @param  {String} flowType 流程类型
   * @param  {String} recordId 业务主键ID
   * @return {[type]}          [description]
   */
  notionService.queryNotion = function(flowType, recordId) {
    var deferred = $q.defer();

    $http({
      method: 'GET',
      url: '/workflow/notion',
      params: {
        flowType: flowType,
        recordId: recordId
      },
      timeout: 15000
    }).then(function(response) {
      var data = response.data;
      deferred.resolve(data);
    }, deferred.reject);

    return deferred.promise;
  };

  notionService.getQueryNotionUrl = function(flowType, recordId) {
    return '/workflow/notion?flowType=' + flowType + '&recordId=' + recordId;
  };

  /**
   * 获取意见
   * @param  {String} flowType 流程类型
   * @param  {String} recordId 业务主键ID
   * @return {[type]}          [description]
   */
  notionService.queryNotionByNotionField = function(flowType, recordId, notionField) {
    if(!notionField) { return; }
    var deferred = $q.defer();

    $http({
      method: 'GET',
      url: '/workflow/notion/notionField/' + notionField,
      params: {
        flowType: flowType,
        recordId: recordId
      },
      timeout: 15000
    }).then(function(response) {
      var data = response.data;
      deferred.resolve(data.content);
    }, deferred.reject);

    return deferred.promise;
  };

  /**
   * 保存意见
   * @param  {Object} opinion 意见对象
   * @return {[type]}         [description]
   */
  notionService.saveTempNotion = function(opinion) {
    var _url = '/workflow/tempnotion' + '?recordId=' + opinion.recordId + '&flowType=' + opinion.flowTypeId;
    return $http({
      method: 'POST',
      url: _url,
      data: opinion,
      timeout: 15000
    }).then(function(response) {
      return response.data;
    });
  };

  /**
   * 获取临时意见
   * @param {String} ideaName 意见名称
   * @param {String} recordId 业务ID
   * @param {String} userId 当前用户ID
   * @param {String} flowType 流程类型
   * @return {[type]} [description]
   */
  notionService.getTempNotion = function(ideaName, recordId, userId, flowType) {
    var url = '/workflow/tempnotion/' + ideaName;
    return $http.get(url, {
      params: {
        recordId: recordId,
        userId: userId,
        flowType: flowType
      },
      timeout: 15000
    }).then(function(response) {
      return response.data.content;
    });
  };

  /**
   * 获取多个临时意见
   * @param {Array} ideaNames 多个意见类型
   * @param {String} recordId 业务ID
   * @param {String} userId 当前用户ID
   * @param {String} flowType 流程类型
   * @return {Map.<String, String>}
  */
  notionService.getTempNotions = function(ideaNames, recordId, userId, flowType) {
    return $q.all(ideaNames.map(function(ideaName) {
      return notionService.getTempNotion(ideaName, recordId, userId, flowType);
    })).then(function(notions) {
      var notionMap = {};
      ideaNames.forEach(function(ideaName, index) {
        notionMap[ideaName] = notions[index];
      });
      return notionMap;
    });
  };

  notionService.getQueryTempNotionUrl = function(ideaName, recordId, userId, flowType) {
    return '/workflow/tempnotion/' + ideaName + '?recordId=' + recordId + '&userId=' + userId + '&flowType=' + flowType;
  };

  notionService.setNotionConfig = function(notionConfig) {
    this.notionConfig = notionConfig;
  };

  notionService.getNotionConfig = function() {
    return this.notionConfig;
  };
  /**
   * 获取阅毕意见
   * @param  {[String]} flowType   流程类型
   * @param  {[String]} relationId ID
   * @return {[list]}
   */
  notionService.getDyNotion = function(flowType, relationId) {
    return $http({
      method: 'GET',
      url: '/toberead/notionlist?flowType=' + flowType + '&relationId=' + relationId,
      timeout: 15000
    }).then(function(response) {
      return response.data;
    });
  };

  return notionService;
});
