'use strict';

angular.module('impactApp')
  .factory('RequestService', function RequestService(isAdult, $timeout, $sessionStorage, $http, $state, $window, Auth, User, RequestResource) {
    var currentRequest = null;
    if ($sessionStorage.currentRequest) {
      currentRequest = new RequestResource($sessionStorage.currentRequest);
    }

    return {
      saveCurrent: function(scope, next) {
        if (!currentRequest) {
          console.err('No current request');
          return;
        }

        currentRequest.steps = [{ name: 'questionnaire', state: 'complet' }];

        currentRequest.steps.push({
          name: 'obligatoire',
          state: 'en_cours',
          files: [
            { name: 'certificatMedical', state: 'demande' },
            { name: 'carteIdentite', state: 'demande' }
          ]
        });

        var success = function() {
          if (next) {
            next();
          }

          $timeout(function() {
            scope.$broadcast('requestSaved');
          }, 100);
        };

        var error = function(err) {
          $window.alert(err.data.message);
        };

        if (currentRequest._id) {
          currentRequest.$update(success, error);
        } else {
          currentRequest.$save(success, error);
        }
      },

      saveStepState: function(request, step, state, next) {
        this.saveStepStateAndFiles(request, step, state, [], next);
      },

      saveStepStateAndFiles: function(request, step, state, files, next) {
        RequestResource.updateStep({shortId: request.shortId}, {step: step, state: state, files: files}, function() {
          _.find(request.steps, {name: step}).state = state;
          if (next) { next(); }
        });
      },

      saveNewStep: function(request, step, state, status, next) {
        return this.saveNewStepAndFiles(request, step, state, [], status, next);
      },

      saveNewStepAndFiles: function(request, step, state, files, status) {
        this.saveStatus(request, status);
        return $http.post('/api/requests/' + request.shortId + '/step', {step: step, state: state, files: files})
        .success(function(data) {
          request.steps.push(data);
        });
      },

      saveStatus: function(request, status) {
      	$http.post('/api/requests/' + request.shortId + '/status', {requestStatus: status})
        .success(function() {
        })
        .error(function(err) {
        	$window.alert(err);
        });
      },

      /**
      * Utilitaires
      */
      getUserRequests: function() {
        return User.queryRequests({}, function(requests) {
          if (requests && angular.isArray(requests)) {
            $sessionStorage.currentRequest = _.find(requests, 'opened');
          }
        });
      },

      setCurrent: function(request) {
        currentRequest = $sessionStorage.currentRequest = request;
      },

      getCurrent: function(next) {
        return next ? next(currentRequest) : currentRequest;
      },

      resetCurrent: function(cb) {
        this.setCurrent(null);
        if (cb) { cb(); }
      },

      setCurrentMdph: function(mdph) {
        currentRequest.mdph = mdph;
      },

      getCurrentStep: function(request) {
        if (!request || !request.steps) {
          return undefined;
        }
        return request.steps[request.steps.length - 1];
      },

      updatedAt: function(request) {
        return moment(request.updatedAt).fromNow();
      }
    };
  });

@@ -1296,1194 +1296,8 @@
 %7D,%0A%0A
-      saveStepState: function(request, step, state, next) %7B%0A        this.saveStepStateAndFiles(request, step, state, %5B%5D, next);%0A      %7D,%0A%0A      saveStepStateAndFiles: function(request, step, state, files, next) %7B%0A        RequestResource.updateStep(%7BshortId: request.shortId%7D, %7Bstep: step, state: state, files: files%7D, function() %7B%0A          _.find(request.steps, %7Bname: step%7D).state = state;%0A          if (next) %7B next(); %7D%0A        %7D);%0A      %7D,%0A%0A      saveNewStep: function(request, step, state, status, next) %7B%0A        return this.saveNewStepAndFiles(request, step, state, %5B%5D, status, next);%0A      %7D,%0A%0A      saveNewStepAndFiles: function(request, step, state, files, status) %7B%0A        this.saveStatus(request, status);%0A        return $http.post('/api/requests/' + request.shortId + '/step', %7Bstep: step, state: state, files: files%7D)%0A        .success(function(data) %7B%0A          request.steps.push(data);%0A        %7D);%0A      %7D,%0A%0A      saveStatus: function(request, status) %7B%0A      %09$http.post('/api/requests/' + request.shortId + '/status', %7BrequestStatus: status%7D)%0A        .success(function() %7B%0A        %7D)%0A        .error(function(err) %7B%0A        %09$window.alert(err);%0A        %7D);%0A      %7D,%0A%0A
     

Remove unused functions