var angular = require('angular');
require('../modules');
require('./TaskRepository');
'use strict';

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

module.factory('CurrentTaskService', function($q, $rootScope, TaskRepository, localforage) {
  var service = {
    /**
     * 切换当前任务。
     */
    changeCurrentTask: function(task) {
      var promise;
      promise = localforage.setItem('currentTask', task.id).then(function() {
        $rootScope.currentTask = task;
      });
      return $q.when(promise);
    },
    /**
     * 清空当前任务。
     */
    clearCurrentTask: function() {
      delete $rootScope.currentTask;
      return $q.when(localforage.removeItem('currentTask'));
    },

    getCurrentTask: function() {
      var promise;
      promise = localforage.getItem('currentTask').then(function(taskId) {
        return TaskRepository.getTaskById(taskId).then(function(task) {
          $rootScope.currentTask = task;
          return task;
        });
      });
      return $q.when(promise);
    }
  };

  return service;
});