/*
 * Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
 * Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
 * Created By: brad@reciprocitylabs.com
 * Maintained By: brad@reciprocitylabs.com
 */

(function (can, $) {
  can.Control('GGRC.Controllers.PbcWorkflows', {}, {
    '{CMS.Models.Assessment} created': function (model, ev, instance) {
      var auditDfd;
      var objectDfd;

      if (!(instance instanceof CMS.Models.Assessment)) {
        return;
      }

      this._after_pending_joins(instance, function() {
        auditDfd = this._create_relationship(instance,
            instance.audit, instance.audit.context);
        objectDfd = this._create_relationship(instance,
            instance.object, instance.audit.context);
        instance.delay_resolving_save_until($.when(auditDfd, objectDfd));
      }.bind(this));
    },
    '{CMS.Models.Issue} created': function (model, ev, instance) {
      var auditDfd;
      var controlDfd;
      var programDfd;
      var assessmentDfd;

      if (!(instance instanceof CMS.Models.Issue)) {
        return;
      }

      this._after_pending_joins(instance, function() {
        auditDfd = this._create_relationship(instance, instance.audit);
        controlDfd = this._create_relationship(instance, instance.control);
        programDfd = this._create_relationship(instance, instance.program);
        assessmentDfd = this._create_relationship(instance, instance.assessment);
        instance.delay_resolving_save_until($.when(auditDfd, controlDfd,
            programDfd, assessmentDfd));
      }.bind(this));
    },
    '{CMS.Models.Section} created': function (model, ev, instance) {
      var directiveDfd;

      if (!(instance instanceof CMS.Models.Section)) {
        return;
      }

      this._after_pending_joins(instance, function() {
        directiveDfd = this._create_relationship(instance, instance.directive);
        instance.delay_resolving_save_until($.when(directiveDfd));
      }.bind(this));
    },
    '{CMS.Models.UserRole} created': function (model, ev, instance) {
      var dfd;
      if (!(instance instanceof CMS.Models.UserRole)) {
        return;
      }
      if (instance.role_name !== 'Auditor') {
        return;
      }
      // Find the program context
      dfd = instance.refresh_all('context', 'related_object', 'program',
          'context');
      dfd.then(function (programContext) {
        // Find existing auditor roles for program context
        return $.when(
            programContext,
            CMS.Models.UserRole.findAll({context_id: programContext.id,
                person_id: instance.person.id}),
            CMS.Models.Role.findAll({name: 'ProgramReader'}));
      }).then(function (programContext, auditorProgramRoles, readerRoles) {
        // Check if there are any existing roles for the user and program context
        if (auditorProgramRoles.length) {
          return null;
        }
        // If no program role exists for the user, we create a reader role
        return new CMS.Models.UserRole({
          person: instance.person,
          role: readerRoles[0].stub(),
          context: programContext
        }).save();
      });
      instance.delay_resolving_save_until(dfd);
    },
    _after_pending_joins: function(instance, callback) {
      var dfd = instance.attr('_pending_joins_dfd');
      if (!dfd) {
        dfd = new $.Deferred().resolve();
      }
      dfd.then(callback)
    },
    _create_relationship: function (source, destination, context) {
      if (!destination || !destination.id) {
        return $.Deferred().resolve();
      }
      if (!context) {
        context = source.context;
      }
      return new CMS.Models.Relationship({
        source: source.stub(),
        destination: destination,
        context: context
      }).save();
    }
  });

  $(function () {
    $(document.body).ggrc_controllers_pbc_workflows();
  });
})(this.can, this.can.$);

@@ -557,37 +557,38 @@
 stance, function
+ 
 () %7B%0A
-
         auditDfd
@@ -1168,32 +1168,33 @@
 stance, function
+ 
 () %7B%0A        aud
@@ -1448,32 +1448,43 @@
 te_relationship(
+%0A          
 instance, instan
@@ -1865,16 +1865,17 @@
 function
+ 
 () %7B%0A   
@@ -3322,16 +3322,17 @@
 function
+ 
 (instanc
@@ -3463,24 +3463,24 @@
 ();%0A      %7D%0A
-
       dfd.th
@@ -3491,16 +3491,17 @@
 allback)
+;
 %0A    %7D,%0A

Fix ESLint warning for pbc/workflow_controller.js