var angular = require('angular');
require('../modules');
require('./TaskCacheArray');
require('./TaskRemoteService');
require('./TaskRepository');

'use strict';

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

module.factory('taskList', function($rootScope, TaskCacheArray, TaskRemoteService, TaskRepository, $q) {
  
  var listCacheArray = new TaskCacheArray('task.list', 20);

  listCacheArray.queryList = function(pageNo) {
    var self = this;
    return TaskRemoteService.queryTaskList(pageNo).then(function(data) {
      self.number = data.number;
      self.totalPages = data.totalPages;
      return TaskRepository.addOrUpdateTasks(data.content);
    }, function() {
      self.totalPages = -1;
      return $q.when();
    });
  };

  return listCacheArray;
});