import Ember from 'ember';
import EditFormController from 'ember-flexberry/controllers/edit-form';
import { Query } from 'ember-flexberry-data';
import BusinessDataObjectEvents from 'ember-flexberry-designer/enums/i-c-s-soft-s-t-o-r-m-n-e-t-business-data-service-object-events';

const { SimplePredicate } = Query;

export default EditFormController.extend({
  parentRoute: 'fd-class-list-form',

  getCellComponent(attr, bindingPath, model) {
    let cellComponent = this._super(...arguments);
    if (attr.kind === 'belongsTo') {
      switch (`${model.modelName}+${bindingPath}`) {
        case 'fd-class-storage-type+storageType':
          cellComponent.componentProperties = {
            choose: 'showLookupDialog',
            remove: 'removeLookupValue',
            displayAttributeName: 'shortName',
            required: true,
            relationName: 'storageType',
            projection: 'ListFormView',
            dropdown: true,
            dropdownIsSearch: false,
            sorting: 'desc',
            direction: 'downward'
          };
          break;

      }
    }

    return cellComponent;
  },

  /**
    Current predicate to limit accessible values for lookup with '«businessserver»' stereotype.
    @property lookupStereotypeLimitPredicate
    @type SimplePredicate
   */
  lookupStereotypeLimitPredicate: Ember.computed(function() {
    return new SimplePredicate('stereotype', 'eq', '«businessserver»');
  }),

  setBusinessServerEvents() {
    if (!this.model.get('businessServerClass') &&
          this.model.get('businessServerEvents') !== BusinessDataObjectEvents.OnAllEvents) {
      this.model.set('businessServerEvents', BusinessDataObjectEvents.OnAllEvents);
    }
  },

  actions: {
    /**
      Overridden action for button 'Save'.
      @method actions.save
    */
    save() {
      this.setBusinessServerEvents();
      this._super();
    },

    /**
      Overridden action for button 'Save and close'.
      @method actions.saveAndClose
      @param {Boolean} skipTransition If `true`, then transition during close form process will be skipped after save.
    */
    saveAndClose(skipTransition) {
      this.setBusinessServerEvents();
      this._super(skipTransition);
    },
  }
});

@@ -1443,277 +1443,8 @@
 ),%0A%0A
-  setBusinessServerEvents() %7B%0A    if (!this.model.get('businessServerClass') &&%0A          this.model.get('businessServerEvents') !== BusinessDataObjectEvents.OnAllEvents) %7B%0A      this.model.set('businessServerEvents', BusinessDataObjectEvents.OnAllEvents);%0A    %7D%0A  %7D,%0A%0A
   ac
@@ -1553,35 +1553,43 @@
 () %7B%0A      this.
+_
 set
+Default
 BusinessServerEv
@@ -1893,19 +1893,27 @@
    this.
+_
 set
+Default
 Business
@@ -1968,17 +1968,445 @@
 );%0A    %7D
-,
+%0A  %7D,%0A%0A  /**%0A    Sets 'businessServerEvents' model property value to default if business server is not present.%0A    @method _setDefaultBusinessServerEvents%0A  */%0A  _setDefaultBusinessServerEvents() %7B%0A    if (!this.model.get('businessServerClass') &&%0A        this.model.get('businessServerEvents') !== BusinessDataObjectEvents.OnAllEvents) %7B%0A      this.model.set('businessServerEvents', BusinessDataObjectEvents.OnAllEvents);%0A    %7D
 %0A  %7D%0A%7D);

Fix code style and add comments