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

'use strict';

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

module.factory('taskDrafts', function(TaskCacheArray, TaskRemoteService, TaskRepository, $q) {
  
  var draftsCacheArray = new TaskCacheArray('task.drafts', 20);

  draftsCacheArray.queryList = function(pageNo) {
    var self = this;
    return TaskRemoteService.queryDraftTaskList(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 draftsCacheArray;
});