/*!
 Copyright (C) 2017 Google Inc., authors, and contributors
 Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
 */

(function (can, GGRC) {
  'use strict';
  var tpl = can.view(GGRC.mustache_path +
    '/components/assessment/info-pane/info-pane.mustache');

  /**
   * Assessment Specific Info Pane View Component
   */
  GGRC.Components('assessmentInfoPane', {
    tag: 'assessment-info-pane',
    template: tpl,
    viewModel: {
      define: {
        mappedSnapshots: {
          Value: can.List
        },
        controls: {
          get: function () {
            return this.attr('mappedSnapshots')
              .filter(function (item) {
                return item.child_type === 'Control';
              });
          }
        },
        relatedInformation: {
          get: function () {
            return this.attr('mappedSnapshots')
              .filter(function (item) {
                return item.child_type !== 'Control';
              });
          }
        },
        comments: {
          Value: can.List
        },
        instance: {}
      },
      getSnapshotQuery: function () {
        var relevantFilters = [{
          type: this.attr('instance.type'),
          id: this.attr('instance.id'),
          operation: 'relevant'
        }];
        return GGRC.Utils.QueryAPI
          .buildParam('Snapshot', {}, relevantFilters, [], []);
      },
      getCommentsQuery: function () {
        var relevantFilters = [{
          type: this.attr('instance.type'),
          id: this.attr('instance.id'),
          operation: 'relevant'
        }];
        return GGRC.Utils.QueryAPI
          .buildParam('Comment', {}, relevantFilters, [], []);
      },
      requestQuery: function (query) {
        var dfd = can.Deferred();
        this.attr('isLoading', true);
        GGRC.Utils.QueryAPI
          .batchRequests(query)
          .done(function (response) {
            var type = Object.keys(response)[0];
            var values = response[type].values;
            dfd.resolve(values);
          })
          .fail(function () {
            dfd.resolve([]);
          })
          .always(function () {
            this.attr('isLoading', false);
          }.bind(this));
        return dfd;
      },
      loadSnapshots: function () {
        var query = this.getSnapshotQuery();
        return this.requestQuery(query);
      },
      loadComments: function () {
        var query = this.getCommentsQuery();
        return this.requestQuery(query);
      },
      prepareFormFields: function () {
        this.attr('formFields',
          GGRC.Utils.CustomAttributes.convertValuesToFormFields(
            this.attr('instance.custom_attribute_values')
          )
        );
      },
      saveForm: function (formFields) {
        var caValues = can.makeArray(
          this.attr('instance.custom_attribute_values')
        );
        Object.keys(formFields).forEach(function (fieldId) {
          var caValue =
            caValues
              .find(function (item) {
                return item.def.id === Number(fieldId);
              });
          caValue.attr('attribute_value',
            GGRC.Utils.CustomAttributes.convertToCaValue(
              caValue.attr('attributeType'),
              formFields[fieldId]
            )
          );
        });

        return this.attr('instance').save();
      }
    },
    init: function () {
      this.viewModel.attr('mappedSnapshots')
        .replace(this.viewModel.loadSnapshots());
      this.viewModel.attr('comments')
        .replace(this.viewModel.loadComments());
      this.viewModel.prepareFormFields();
    },
    events: {
      '{viewModel.instance} related_destinations': function () {
        console.info('Was related_destinations called!!!!', arguments);
      }
    }
  });
})(window.can, window.GGRC);

@@ -467,24 +467,187 @@
   define: %7B%0A
+        isSaving: %7B%0A          type: 'boolean',%0A          value: false%0A        %7D,%0A        isLoading: %7B%0A          type: 'boolean',%0A          value: false%0A        %7D,%0A
         mapp
@@ -1221,32 +1221,90 @@
 List%0A        %7D,%0A
+        documents: %7B%0A          Value: can.List%0A        %7D,%0A
         instance
@@ -1326,24 +1326,16 @@
      get
-Snapshot
 Query: f
@@ -1335,32 +1335,36 @@
 uery: function (
+type
 ) %7B%0A        var 
@@ -1573,18 +1573,12 @@
 ram(
-'Snapshot'
+type
 , %7B%7D
@@ -1626,25 +1626,24 @@
   getComment
-s
 Query: funct
@@ -1663,256 +1663,216 @@
     
-var relevantFilters = %5B%7B%0A          type: this.attr('instance.type'),%0A          id: this.attr('instance.id'),
+return this.getQuery('Comment');%0A      %7D,%0A      getSnapshotQuery: function () %7B%0A        return this.getQuery('Snapshot');
 %0A      
+%7D,%0A
     
-operation: 'relevant'%0A        %7D%5D;%0A        return GGRC.Utils.QueryAPI%0A          .buildParam('Comment', %7B%7D, relevantFilters, %5B%5D, %5B%5D
+  getDocumentQuery: function () %7B%0A        return this.getQuery('Document'
 );%0A 
@@ -2631,17 +2631,16 @@
 tComment
-s
 Query();
@@ -2686,24 +2686,426 @@
 );%0A      %7D,%0A
+      loadDocuments: function () %7B%0A        var query = this.getDocumentQuery();%0A        return this.requestQuery(query);%0A      %7D,%0A      updateRelatedItems: function () %7B%0A        this.attr('mappedSnapshots')%0A          .replace(this.loadSnapshots());%0A        this.attr('comments')%0A          .replace(this.loadComments());%0A        this.attr('documents')%0A          .replace(this.loadDocuments());%0A      %7D,%0A
       prepar
@@ -4009,206 +4009,25 @@
 del.
-attr('mappedSnapshots')%0A        .replace(this.viewModel.loadSnapshots());%0A      this.viewModel.attr('comments')%0A        .replace(this.viewModel.loadComments());%0A      this.viewModel.prepareFormField
+updateRelatedItem
 s();
@@ -4125,69 +4125,42 @@
     
-console.info('Was related_destinations called!!!!', arguments
+this.viewModel.updateRelatedItems(
 );%0A 

Update related items loading