var angular = require('angular');
require('../modules');
require('./InfoRemoteService');
require('./InfosPageLocalStorageService');
require('./InfosService');
require('./ModelService');
require('jsUtil');
'use strict';

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

module.factory('InfoReleaseService', function(ModelService, InfoRemoteService,
  InfosPageLocalStorageService, $q, InfosService) {

  var storage = InfosPageLocalStorageService;

  var dataListCache = {};
  var pageSize = 36.0;

  var service = {
    /**
     * 获取每一小页的信息
     *
     *
     * @param  {[type]} modelId     [description]
     * @param  {[type]} smallPageNo [description]
     * @param  {[type]} enterSign   [description]
     * @return {[type]}             [description]
     */
    getInfoListNew: function(modelId, smallPageNo, needSyncDataFromRemote) {
      var self = this;
      return ModelService.getModelOfId(modelId).then(function(model) {
        return self._getInfoListNew(model, smallPageNo, needSyncDataFromRemote);
      });
    },

    _getInfoListNew: function(model, smallPageNo, needSyncDataFromRemote) {
      var modelId = model.id,
        mode = model.mode,
        dataList = storage.getDataList(modelId),
        needReplaceDataList = smallPageNo == 1;

      if ((smallPageNo == 1 && !needSyncDataFromRemote) ||
        (smallPageNo > 1 && (!dataList.needGetData(smallPageNo)))) {
        return {
          page: dataList.getPage(smallPageNo),
          success: true,
          dataList: dataList
        };
      } else {
        return InfosService.getInfos(mode, modelId,
            needReplaceDataList?0 : dataList.getTotalPages(), model.listUrl)
          .then(function(data) {
            if (needReplaceDataList) {
              dataList.clear();
            }
            dataList.add(data.content, data.totalElements, pageSize);
            storage.saveDataList(modelId, dataList);
            var page = dataList.getPage(smallPageNo);
            return {
              page: page,
              success: true,
              dataList: dataList
            };
          }, function() {
            return {
              success: false,
              page: dataList.getPage(smallPageNo),
              dataList: dataList
            };
          });
      }
    },

    /**
     * 详情页获取数据(不包括正文内容)
     * 直接从缓存中获取
     * @param  {[type]} modelId [模块ID]
     * @param  {[type]} infoId
     */
    getInfoByInfoId: function(modelId, infoId) {
      return $q.when(storage.getDataList(modelId).getInfoByInfoId(infoId));
    },

    fetchContent: function(pid, modelId, infoId) {
      return ModelService.getModelOfId(pid).then(function(model) {
        if (model.contentUrl) {
          return InfoRemoteService.queryContentByContentUrl(model.contentUrl, infoId);
        } else {
          return InfoRemoteService.queryInfo(modelId, infoId);
        }
      });
    },

    /**
     * 详情页获取正文内容
     * @param  {[type]} modelId [模块ID]
     * @param  {[type]} infoId
     * @return {[type]}         整个信息发布内容
     */
    getInfo: function(pid, modelId, infoId) {
      var dataList = storage.getDataList(pid),
        info = dataList.getInfoByInfoId(infoId);
      return this.fetchContent(pid, modelId, infoId)
        .then(function(data) {
          data.content = handleTable(data.content);
          data.content = handleKg(data.content);
          if(!_.isEmpty(data)) {
            _.apply(info, data);//服务器-->缓存
            storage.saveDataList(pid, dataList);
            return data;
          }
        });
    },

    updateNotion: function(pid, modelId, infoId) {
      var dataList = storage.getDataList(pid),
        info = dataList.getInfoByInfoId(infoId);
        ++info.notion;
        storage.saveDataList(pid, dataList);
      InfoRemoteService.updateNotion(modelId, infoId).then(function(data) {
        if(data != 'OK') {
          storage.saveDataList(pid, dataList);
        }
      });
    }
  };

  function handleTable(content) {
    //'<ion-scroll direction="xy" class="available-scroller">'
    var addStr1 = '<div class="table-container" sino-info-table-view>'
      + '<div class="table-unlimited set-table-width">',
      addStr2 = '</div></div>';
    return content.replace(/<table/g, addStr1 + '<table').replace(/<\/table>/g, '</table>' + addStr2);
  }

  function handleKg(content) {
    // 处理空格
    var th = '<span style="width:0.5em;display:inline-block;"></span>';

    return content.replace(/(&nbsp;)/g,th);
  }

  return service;
});
