package com.sometrik.framework;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

public class FWAuto extends LinearLayout implements NativeCommandHandler {
  
  private FrameWork frame;
  private ArrayList<View> viewList;
  private ArrayList<LinearLayout> layoutList;
  private LinearLayout.LayoutParams params;

  public FWAuto(FrameWork frame) {
    super(frame);
    this.frame = frame;
    viewList = new ArrayList<View>();
    layoutList = new ArrayList<LinearLayout>();
    params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    setLayoutParams(params);
    setOrientation(LinearLayout.HORIZONTAL);
  }
  
  
  private void buildAuto(int columns){
    
    ArrayList<View> copyViewList = new ArrayList<View>();
    for (View view : viewList){
      copyViewList.add(view);
    }
    layoutList = new ArrayList<LinearLayout>();

    for (int i = 1; i <= columns; i++) {
      LinearLayout newLayout = new LinearLayout(frame);
      newLayout.setOrientation(LinearLayout.VERTICAL);
      newLayout.setLayoutParams(params);
      layoutList.add(newLayout);
      for (int viewI = 0; viewI < viewList.size() / columns; viewI++) {
	newLayout.addView(copyViewList.get(0));
	copyViewList.remove(0);
      }
      addView(newLayout);
    }
  }

  @Override
  public void onScreenOrientationChange(boolean isLandscape) {
    // TODO Auto-generated method stub

  }
  
  private void resetLayout(){
    for (LinearLayout layout : layoutList) {
      Log.d("auto", "Removing layout");
      layout.removeAllViews();
      removeView(layout);
    }
  }
  
  private boolean canFitColumns(){
    int width = 0;
    for (LinearLayout layout : layoutList){
      width += frame.measureViewWidth(layout);
      Log.d("autoMeasure", "thisWidth +: " + width);
    }
//    int width = frame.measureViewWidth(this);
    Log.d("autoMeasure", "thisWidth final: " + width);
    Log.d("autoMeasure", "screenWidth " + frame.getScreenWidth());
    if (width >= frame.getScreenWidth() || width == 0){
      return false;
    } else {
      return true;
    }
  }
  
  private int measureColumnCount(){
    
    int columns = 1;
    while (true){
      buildAuto(columns + 1);
      if (!canFitColumns()){
	Log.d("auto", "cant fit anymore. Returning " + columns);
	break;
      } else {
	resetLayout();
	columns++;
      }
    }

    resetLayout();
    return columns;
  }

  @Override
  public void addChild(View view) {
    resetLayout();
    viewList.add(view);
    int columns = measureColumnCount();
    buildAuto(columns);
  }

  @Override
  public void addOption(int optionId, String text) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setValue(String v) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setValue(int v) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setViewEnabled(Boolean enabled) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setStyle(String key, String value) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setError(boolean hasError, String errorText) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public int getElementId() {
    return getId();
  }

}

@@ -875,24 +875,123 @@
 ist%3CView%3E();
+%0A%0A    int remainder = viewList.size() %25 columns;%0A    Log.d(%22auto%22, %22remainder: %22 + remainder);%0A    
 %0A    for (Vi
@@ -1464,24 +1464,204 @@
 0);%0A      %7D%0A
+      %0A      if (remainder %3E 0)%7B%0A%09Log.d(%22auto%22, %22Aaaaa %22 + remainder);%0A%09newLayout.addView(copyViewList.get(0));%0A%09copyViewList.remove(0);%0A%09remainder = remainder - 1;%0A      %7D%0A      %0A
       addVie
@@ -1676,24 +1676,29 @@
 out);%0A    %7D%0A
+    %0A
   %7D%0A%0A  @Over
@@ -2528,20 +2528,16 @@
 ount()%7B%0A
-    
 %0A    int
@@ -2566,18 +2566,74 @@
 e (true)
+ 
 %7B%0A
+      Log.d(%22auto%22, %22checking to fit %22 + columns + 1);%0A
       bu
@@ -2680,17 +2680,48 @@
 olumns()
-)
+ %7C%7C columns %3E= viewList.size()) 
 %7B%0A%09Log.d

Fix missing remainder elements