import { pt } from "lively.graphics";

class Layout {
  constructor({spacing} = {}) {
    this.spacing = spacing || 0;
    this.active = false;
  }
}

export class VerticalLayout extends Layout {
   
  applyTo(morph) {
    if (this.active) return;
    var pos = pt(this.spacing, this.spacing),
        submorphs = morph.submorphs,
        maxWidth = 0;
       
    this.active = true;
    submorphs.forEach(m => {
        m.position = pos;
        pos = m.bottomLeft.addPt(pt(0, this.spacing));
        maxWidth = Math.max(m.height, maxWidth);
    });
    morph.extent = pt(maxWidth, pos.y)
    this.active = false;
  }
   
}

export class HorizontalLayout extends Layout {
 
 applyTo(morph) {
    if (this.active) return;
    var pos = pt(this.spacing, this.spacing),
        submorphs = morph.submorphs,
        maxHeight = 0;
        
    this.maxHeight = 0;
    this.active = true;  
    submorphs.forEach(m => {
      m.position = pos;
      pos = m.topRight.addPt(pt(this.spacing, 0));
      maxHeight = Math.max(m.width, maxHeight);
    });
    morph.extent = pt(pos.x, maxHeight);  
    this.active = false;
 }
  
}

export class GridLayout extends Layout {
  
  constructor(props) {
    super(props);
    this.rowCount =  props.rowCount;
    this.columnCount = props.columnCount;
    this.columenMinimumWidth = props.columnMinimumWidth || {};
  }
  
  applyTo(morph) {
    
  }
  
}

export class TilingLayout extends Layout {
  
  applyTo(morph) {
    
  }
  
}
@@ -69,16 +69,24 @@
 %7Bspacing
+, border
 %7D = %7B%7D) 
@@ -83,24 +83,93 @@
 er%7D = %7B%7D) %7B%0A
+    this.border = %7Btop: 0, left: 0, right: 0, bottom: 0, ...border%7D;%0A
     this.spa
@@ -1520,29 +1520,1521 @@
 pplyTo(morph) %7B%0A
-
     
+var width = this.getOptimalWidth(morph, morph.submorphs),%0A        currentRowHeight = 0,%0A        currentRowWidth = 0,%0A        spacing = this.spacing,%0A        previousRowHeight = spacing,%0A        i = 0, rowSwitch = true;%0A        %0A    if (this.active) return;%0A    this.active = true;%0A%0A    while (i %3C morph.submorphs.length) %7B%0A        var submorphExtent = morph.submorphs%5Bi%5D.extent;%0A        if (rowSwitch %7C%7C currentRowWidth + submorphExtent.x + 2*spacing %3C= width) %7B%0A            rowSwitch = false;%0A            morph.submorphs%5Bi%5D.position = pt(currentRowWidth + spacing, previousRowHeight);%0A            currentRowHeight = Math.max(currentRowHeight, submorphExtent.y);%0A            currentRowWidth += spacing + submorphExtent.x;%0A            i++;%0A        %7D else %7B%0A            previousRowHeight += spacing + currentRowHeight;%0A            currentRowWidth = currentRowHeight = 0;%0A            rowSwitch = true;%0A        %7D%0A    %7D%0A    %0A    this.active = false;%0A  %7D%0A  %0A  getMinWidth(container, submorphs) %7B%0A      return submorphs.reduce((s, e) =%3E (e.extent.x %3E s) ? e.extent.x : s, 0) +%0A          this.border.left + this.border.right;%0A  %7D%0A  %0A  getMinHeight(container, submorphs) %7B%0A      return submorphs.reduce((s, e) =%3E (e.extent.y %3E s) ? e.extent.y : s, 0) +%0A          this.border.top + this.border.bottom;%0A  %7D%0A  %0A  getOptimalWidth(container, submorphs) %7B%0A      var width = container.width,%0A          maxSubmorphWidth = this.getMinWidth(container, submorphs);%0A      return Math.max(width, maxSubmorphWidth);
 %0A  %7D%0A  %0A%7D

add tiling layout