'use strict';

/**
 * Controller which handles requesting data for a full paper, and then returning it
 */
angular.module('uql.authors').controller('UqlAuthorsPaperCtrl', ['$q', '$stateParams', '$scope', 'AlertSvc', 'UqlAuthorsJsonService',
  function ($q, $stateParams, $scope, AlertSvc, UqlAuthorsJsonService) {
    var id = $stateParams.id;

    function getPaper(pid) {
      if (!pid) {
        throw 'No pid passed into full page app view';
      }

      var promise = $q.all([
        UqlAuthorsJsonService.getPaperDetails(pid, true),
        UqlAuthorsJsonService.getEspacePaperDetails(pid, true)
      ]);

      promise.then(function (paper) {
        if (paper && (paper.length === 2)) {
          $scope.paper = [
            paper[0],
            paper[1].response.docs[0]
          ];
        } else {
          AlertSvc.add({
            type: 'danger',
            timeout: false,
            heading: 'No paper found',
            message: 'Request error'
          });
        }
      }, function (reason) {
        AlertSvc.add({
          type: 'danger',
          timeout: false,
          heading: 'No paper found',
          message: reason ? reason : 'Please check Document PID'
        });
      });
    }

    getPaper(id);
  }]);
