'use strict';

var angular = require('angular'),
  _ = require('jsUtil');
require('../modules');

var module = angular.module('info.services');

module.factory('InfoRemoteService', function($http, $q) {
  var service = {};

  /**
   * 获取某个模块的所有信息list
   * @param  {String} modlId 模块id
   * @param  {String} pageNo 分页页码
   * @param  {String} listUrl 获取数据的不同的URL
   * @return {Promsie}        查询到的数据
   */
  service.queryInfos = function(mode, modlId, pageNo, listUrl) {
    var params = {};
    params['page.pn'] = !_.isUndefined(pageNo) ? pageNo: 0;

    var url = (_.isEmpty(listUrl) || listUrl == '-1') && (mode != 'archivein' && mode != 'archiveout') ? '/info/v3/allofmodel/' + modlId : listUrl;
    if(_.isEmpty(url) || url == '-1') {
      return $q.when(false);
    } else {
      return $http.get(url, {
        params: params,
        timeout: 15000
      }).then(function(response) {
        return response.data;
      });
    }

  };
  /**
   * 获取一个模块下的列表数据，有MD5值的
   */
  service.queryInfoList = function(mode, modlId, pageNo, listUrl, md5) {
    var params = {};
    params['page.pn'] = !_.isUndefined(pageNo) ? pageNo: 0;
    params['page.size'] = modlId === 'cggs'? 10 : 15;
    params['md5'] = md5;

    var url = (_.isEmpty(listUrl) || listUrl == '-1') && (mode != 'archivein' && mode != 'archiveout') ? '/info/v3/list/' + modlId : listUrl;

    if(_.isEmpty(url) || url == '-1') {
      return $q.when(false);
    } else {
      return $http.get(url, {
        params: params,
        timeout: 15000
      }).then(function(response) {
        return response.data;
      });
    }
  };
  /**
   * 获取某条信息的所有数据
   * @param  {String} modlId 模块id
   * @param  {String} infoId 信息id
   * @return {Promsie}        该信息的所有数据
   */
  service.queryInfo = function(modlId, infoId) {
    var params = {};

    return $http.get('/info/v3/specific/'+infoId+'?modlId='+modlId, {
      params: params,
      timeout: 15000
    }).then(function(response) {
      return response.data;
    });
  };

  /**
   * 获取该正文内容
   */
  service.queryContentByContentUrl = function(contentUrl, infoId) {
    var params = {};

    return $http.get(contentUrl + '?id=' + infoId, {
      params: params,
      timeout: 15000
    }).then(function(response) {
      return response.data;
    });
  };

  /**
   * 获取列表页信息，整个列表页是下滑
   */
  service.queryListData = function(number) {
    var params = {};
    params['page.pn'] = !_.isUndefined(number) ? number: 0;
    return $http.get('/info/ccdt/list', {
        params: params,
        timeout: 15000
      }).then(function(response) {
      return response.data;
    });
  };

  service.updateNotion = function(modlId, infoId) {
    return $http.post('/info/v3/notion/' + modlId + '/' + infoId, {
            timeout: 15000
        }).then(function(response) {
            return response.statusText;
        });
      };

  return service;
});
