import Attribute from './Attribute';
import Reference from './Reference';

/*
 * Entity ✓✗
 * ======
 *
 * ✓ attributes   : Entity's attributes list (e.g. id, name, age, gender, size)
 * ✓ name         : Entity's name (e.g. Game Object, Home Screen)
 * ✓ [plural]     : Entity's name's plural if appropriated (e.g. Game Objects, Screens)
 */
export default class Entity {

  static fromXMLObject(obj) {
    var ent = new Entity(obj.$.name, obj.$.plural);

    if (obj.attributes instanceof Array) {
      for (var attrs of obj.attributes) {
        for (var attr of attrs.attribute) {
          ent.addAttribute(Attribute.fromXMLObject(attr));
        }
      }
    }
    if (obj.references instanceof Array) {
      for (var refs of obj.references) {
        for (var ref of refs.reference) {
          ent.addReference(Reference.fromXMLObject(ref));
        }
      }
    }

    return ent;
  }

  constructor(name, plural = null) {
    this.attributes = [];
    this.name = name;
    this.plural = plural;
    this.references = [];
  }

  addAttribute(attr) {
    this.attributes.push(attr);
  }

  addReference(ref) {
    ref.source = this;
    this.references.push(ref);
  }
}

@@ -366,16 +366,193 @@
 tity %7B%0A%0A
+  static add(entity) %7B%0A    if (Entity.instances === undefined)%0A      Entity.instances = %7B%7D;%0A%0A    Entity.instances%5Bentity.name%5D = entity;%0A    console.log(Entity.instances);%0A  %7D%0A%0A
   static
@@ -569,24 +569,24 @@
 ject(obj) %7B%0A
-
     var ent 
@@ -1068,16 +1068,246 @@
 t;%0A  %7D%0A%0A
+  static get(name) %7B%0A    if (!Entity.instances)%0A      return null;%0A    if (!Entity.instances%5Bname%5D) %7B%0A      console.log('Unknown entity with name %22' + name + '%22');%0A      return null;%0A    %7D%0A%0A    return Entity.instances%5Bname%5D;%0A  %7D%0A%0A
   constr
@@ -1434,16 +1434,39 @@
 es = %5B%5D;
+%0A%0A    Entity.add(this);
 %0A  %7D%0A%0A  
@@ -1519,24 +1519,24 @@
 attr);%0A  %7D%0A%0A
-
   addReferen
@@ -1541,24 +1541,65 @@
 ence(ref) %7B%0A
+    ref.entity = Entity.get(ref.entity);%0A
     ref.sour

+|instances array +|add(entity) +|get(name)