/**
  @module ember-flexberry-designer
*/

import Ember from 'ember';

import FdUmlElement from './fd-uml-element';
import joint from 'npm:jointjs';

/**
  An object that describes a UsecaseActor on the UML diagram.

  @class FdUmlUsecaseActor
  @extends FdUmlElement
*/
export default FdUmlElement.extend({

  /**
    The name of the class.

    @property name
    @type String
  */
  name: Ember.computed.alias('primitive.Name.Text'),

  /**See {{#crossLink "FdUmlPrimitive/JointJS:method"}}here{{/crossLink}}.

    @method JointJS
  */
  JointJS() {
    let properties = this.getProperties('id', 'name', 'size', 'position');
    return new UseCase(properties);
  },
});

/**
  Defines the JointJS object, which represents a 'UsecaseActor' object in the UML diagram.

  @for FdUmlUsecaseActor
  @class UsecaseActor
  @extends BaseObject
  @namespace flexberry.uml
  @constructor
*/
export let UseCase = joint.shapes.basic.Generic.define('flexberry.uml.Usecase', {
  attrs: {
    rect: { 'width': 400 },
    '.uml-usecase-rect': { 'rx': '120', 'ry': '120', 'stroke': 'black', 'strokeWidth': '1', 'fill': '#ffffff' },

    '.uml-usecase-text': {
      'ref': '.uml-usecase-rect',
      'textAnchor': 'middle',
      'yAlignment': 'middle',
      'fontWeight': 'bold',
      'refY': 0.5,
      'refX': 0.5,
      'fill': 'black',
      'fontSize': 12,
      'fontFamily': 'Arial'
    }
  },
  name: ''
}, {
  markup: [
    '<g class="rotatable">',
    '<g class="scalable">',
    '<rect class="uml-usecase-rect"/>',
    '</g>',
    '<image/>',
    '<text class="uml-usecase-text"/>',
    '</g>'
  ].join(''),

  initialize: function () {
    this.on('change:name', function() {
      this.updateRectangles();
    }, this);

    this.updateRectangles();

    joint.shapes.basic.Generic.prototype.initialize.apply(this, arguments);
  },

  getUsecaseName: function() {
    return this.get('name');
  },

  updateRectangles: function() {
    let attrs = this.get('attrs');
    let rect =  { type: 'name', text: this.getUsecaseName() };
    let lines = rect.text.split('\n');

    let maxStringChars = lines[0].length;
    for (let stringIndex = 0; stringIndex < lines.length; stringIndex++) {
      if (lines[stringIndex].length >= maxStringChars) {
        maxStringChars = lines[stringIndex].length;
      }
    }

    let hightStep = attrs['.uml-usecase-text'].fontSize;
    let rectHeight = lines.length * hightStep + 20;

    let widthStep = attrs['.uml-usecase-text'].fontSize / 1.5;
    let rectWidth = maxStringChars * widthStep  + 20;

    attrs['.uml-usecase-text'].text = rect.text;
    attrs['.uml-usecase-rect'].height = rectHeight;
    attrs['.uml-usecase-rect'].width = rectWidth;

    this.resize(rectWidth, rectHeight);
  }
});

@@ -179,25 +179,20 @@
 es a Use
-c
+C
 ase
-Actor
  on the 
@@ -218,33 +218,28 @@
 ass FdUmlUse
-c
+C
 ase
-Actor
 %0A  @extends 
@@ -714,25 +714,20 @@
 s a 'Use
-c
+C
 ase
-Actor
 ' object
@@ -763,25 +763,20 @@
 FdUmlUse
-c
+C
 ase
-Actor
 %0A  @clas
@@ -784,17 +784,12 @@
  Use
-c
+C
 ase
-Actor
 %0A  @
@@ -800,18 +800,21 @@
 nds 
-BaseObject
+basic.Generic
 %0A  @

Fix comments