package com.sometrik.framework;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Configuration;
import android.view.View;
import android.widget.GridLayout;
import android.widget.GridLayout.Spec;
import android.widget.LinearLayout;

public class AutoColumnLayout extends LinearLayout implements NativeCommandHandler {
  
  //For small view AutoLayout puts views vertically
  //For big view Autolayout groups 2 views next to each other
  
  private ArrayList<View> viewList;
  private Context context;
  private FrameWork frame;
  
  public AutoColumnLayout(FrameWork frameWork){
    super(frameWork);
    frame = frameWork;
    viewList = new ArrayList<View>();
    setOrientation(LinearLayout.VERTICAL);
  }
  
  public void addChild(View view){
    viewList.add(view);
  }
  
  public void showView(){
    buildAutoLayout(checkForBigView());
    frame.setContentView(this);
  }
  
  private void buildAutoLayout(Boolean bigView) {
    
    GridLayout layout = new GridLayout(context);

    Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 2);
    Spec colspan = GridLayout.spec(GridLayout.UNDEFINED, 3);
    GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(rowSpan, colspan);

    for (int i = 0; i < viewList.size(); i++){
      layout.addView(viewList.get(i), gridParam);
    }
    
//    if (bigView) {
//      for (int i = 0; i < viewList.size(); i++) {
//	LinearLayout layout = new LinearLayout(context);
//	layout.setOrientation(LinearLayout.HORIZONTAL);
//	layout.addView(viewList.get(i));
//
//	if (viewList.size() - 1 > i) {
//	  layout.addView(viewList.get(i + 1));
//	  i++;
//	}
//	addView(layout);
//      }
//
//    } else {
//      for (View view : viewList) {
//	addView(view);
//      }
//    }
  }

  private boolean checkForBigView(){
    //TODO Truly detect screen size
   int orientation =  frame.getResources().getConfiguration().orientation;
   if (orientation == Configuration.ORIENTATION_PORTRAIT){
     return true;
   } else {
     return false;
   }
  }

  @Override
  public void removeChild(int id) {
    for (int i = 0; i < getChildCount(); i++) {
      View v = getChildAt(i);
      if (v.getId() == id) {
	removeView(v);
	System.out.println("Child " + id + " removed succesfully");
	return;
      }
    }
    System.out.println("Child " + id + " wasn't found and wasn't removed");
  }

  @Override
  public void addOption(int optionId, String text) {
    System.out.println("AutoColumnLayout couldn't handle command");
  }

  @Override
  public void setValue(String v) {
    System.out.println("AutoColumnLayout couldn't handle command");
  }

  @Override
  public void setValue(int v) {
    if (v > 0){
      frame.setCurrentView(this);
    }
  }

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

  @Override
  public void setEnabled(Boolean enabled) {
    System.out.println("AutoColumnLayout couldn't handle command");
  }
  
  @Override
  public void setStyle(String key, String value) { }

  @Override
  public void setError(boolean hasError, String errorText) { }
}

@@ -149,20 +149,80 @@
 id.view.
-View
+Gravity;%0Aimport android.view.View;%0Aimport android.view.ViewGroup
 ;%0Aimport
@@ -580,50 +580,130 @@
 ate 
-Context contex
+FrameWork frame;%0A  private GridLayout layou
 t;%0A  
-private FrameWork frame
+GridLayout.LayoutParams gridParam;%0A  LinearLayout.LayoutParams childParams
 ;%0A  
@@ -883,408 +883,292 @@
 ;%0A  
-%7D%0A  %0A  public void addChild(View view)%7B%0A    viewList.add(view);%0A  %7D
+  layout = new GridLayout(frame);%0A
 %0A  
-%0A
   
-public void showView()%7B%0A    buildAuto
+childParams = new LinearLayout.
 Layout
-(checkForBigView());%0A    frame.setContentView(this);%0A  %7D%0A  %0A  private void buildAutoLayout(Boolean bigView) %7B%0A    %0A    GridLayout layout = new GridLayout(context);%0A%0A    Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 2);%0A    Spec colspan = GridLayout.spec(GridLayout.UNDEFINED, 3
+Params(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);%0A    childParams.weight = 1.0f;%0A    childParams.gravity = Gravity.FILL;%0A%0A    layout.setColumnCount(2);%0A    layout.setRowCount(5
 );%0A 
@@ -1190,29 +1190,25 @@
 ayoutParams 
-gridP
+p
 aram = new G
@@ -1234,445 +1234,141 @@
 ams(
-rowSpan, colspan
 );%0A
-%0A
     
-for (int i = 0; i %3C viewList.size(); i++)%7B%0A      layout.addView(viewList.get(i), gridParam);%0A    %7D%0A    %0A//    if (bigView) %7B%0A//      for (int i = 0; i %3C viewList.size(); i++) %7B%0A//%09LinearLayout layout = new LinearLayout(context);%0A//%09layout.setOrientation(LinearLayout.HORIZONTAL);%0A//%09layout.addView(viewList.get(i));%0A//%0A//%09if (viewList.size() - 1 %3E i) %7B%0A//%09  layout.addView(viewList.get(i + 1));%0A//%09  i++;%0A//%09%7D%0A//%09
+param.height = LayoutParams.WRAP_CONTENT;%0A    param.width = LayoutParams.MATCH_PARENT;%0A    layout.setLayoutParams(param);%0A    
 addV
@@ -1384,108 +1384,116 @@
 t);%0A
-//      %7D%0A//%0A//
+  %7D%0A
   
+%0A
   
-%7D else %7B%0A//      for (View view : viewList) %7B%0A//%09addView(view);%0A//      %7D%0A//    %7D
+public void addChild(View view)%7B%0A    layout.setLayoutParams(childParams);%0A    layout.addView(view);
 %0A  %7D
@@ -2414,16 +2414,60 @@
 v %3E 0)%7B%0A
+//      buildAutoLayout(checkForBigView());%0A
       fr
@@ -2478,11 +2478,11 @@
 setC
-urr
+ont
 entV

Remove buildLayout function