/**
 * Created by rozwad on 08.04.17.
 */

var app = app || {};

app.Shop = class {
  constructor(limit) {
    this.products = [];
    this.cart = [];
    this.limit = limit;
    this.shelfSize = 5;
  }

  loadRandomProducts(set, number) {
    this.products = set.map(name => [name, Math.random()])
      .sort((a, b) => a[1] - b[1])
      .slice(0, number)
      .map(item => item[0]);
  }

  insertProduct(productName) {
    this.products.push(productName);
  }

  buyProduct(productName) {
    if (this.limit > 0) {
      this.cart.push(productName);
      this.limit--;
    }
  }

  checkout() {
    // put the products into the fridge
  }

  draw(container) {
    let well = container.getElementsByClassName("well")[0];
    well.innerHTML = '';
    let shop = this;
    let shelf, j;
    for (let i=0; i<this.products.length; i++) {
      if (!shelf) {
        console.log('new shelf!!!');
        shelf = document.createElement("div");
        shelf.className = "shelf";
        j = 0;
      }
      let productName = this.products[i];
      let div = document.createElement("div");
      div.className = "item";
      let product = app.products[productName];
      product.draw(div);
      div.addEventListener("click", function() {
        shop.buyProduct(productName);
        console.log(shop.cart);
      });
      shelf.appendChild(div);
      if (++j >= this.shelfSize) {
        well.appendChild(shelf);
        shelf = null;
      }
    }
    if (shelf) well.appendChild(shelf);
  }
};
@@ -140,18 +140,18 @@
 .cart = 
-%5B%5D
+%7B%7D
 ;%0A    th
@@ -527,34 +527,65 @@
 his.cart
-.push(productName)
+%5BproductName%5D = (this.cart%5BproductName%5D %7C%7C 0) + 1
 ;%0A      
@@ -886,45 +886,8 @@
 ) %7B%0A
-        console.log('new shelf!!!');%0A
     

represent the cart as an object, not array