package com.sometrik.framework;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Configuration;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
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 FrameWork frame;
  private GridLayout layout;
  GridLayout.LayoutParams gridParam;
  LinearLayout.LayoutParams childParams;
  
  public AutoColumnLayout(FrameWork frameWork){
    super(frameWork);
    frame = frameWork;
    viewList = new ArrayList<View>();
    setOrientation(LinearLayout.VERTICAL);
    layout = new GridLayout(frame);

    childParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    childParams.weight = 1.0f;
    childParams.gravity = Gravity.FILL;

    layout.setColumnCount(2);
    layout.setRowCount(5);
    GridLayout.LayoutParams param = new GridLayout.LayoutParams();
    param.height = LayoutParams.WRAP_CONTENT;
    param.width = LayoutParams.MATCH_PARENT;
    layout.setLayoutParams(param);
    addView(layout);
  }
  
  public void addChild(View view){
    layout.setLayoutParams(childParams);
    layout.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){
//      buildAutoLayout(checkForBigView());
      frame.setContentView(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) { }
}

@@ -607,79 +607,34 @@
 ate 
-GridLayout layout;%0A  GridLayout.LayoutParams gridParam;%0A  LinearLayout.
+int maxColumns;%0A  private 
 Layo
@@ -642,22 +642,17 @@
 tParams 
-childP
+p
 arams;%0A 
@@ -835,608 +835,1062 @@
     
-layout = new GridLayout(frame);%0A%0A    childParams = new LinearLayout.LayoutParams(ViewGroup.
+%0A    %0A  %7D%0A  %0A  private void buildAutoLayout()%7B%0A    %0A    ArrayList%3CLinearLayout%3E rowList = new ArrayList%3CLinearLayout%3E();%0A    LinearLayout row = new Linear
 Layout
-Params.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);%0A    childParams.weight = 1.0f;%0A    childParams.gravity = Gravity.FILL;%0A%0A    layout.setColumnCount(2
+(frame);%0A    rowList.add(row);%0A    int columnCounter = 0;%0A    %0A    while (true) %7B %0A      LinearLayout column = new LinearLayout(frame);%0A      fillColumn(column
 );%0A    
-layout.setRowCount(5);%0A    GridLayout.LayoutParams param = new GridLayout.
+  row.addView(column);%0A      columnCounter++;%0A      if (viewList.size() == 0)%7B%0A%09break;%0A      %7D else if (columnCounter %3E= maxColumns)%7B%0A%09row = new Linear
 Layout
-Params();%0A    param.height = LayoutParams.WRAP_CONTENT;%0A    param.width = LayoutParams.MATCH_PARENT;%0A    layout.setLayoutParams(param);%0A    addView(layout);%0A  %7D%0A  %0A  public void addChild(View view)%7B%0A    layout.setLayoutParams(childParams);%0A    layou
+(frame);%0A%09rowList.add(row);%0A      %7D%0A    %7D%0A    %0A    for (LinearLayout rowFromList : rowList)%7B%0A      addView(rowFromList);%0A    %7D%0A  %7D%0A  %0A  private Boolean fillColumn(LinearLayout column) %7B%0A    int counter = 0;%0A    while (true) %7B%0A      column.addView(viewList.get(counter));%0A      counter ++;%0A      if (counter %3E= 3 %7C%7C viewList.size() - counter == 0 )%7B%0A%09for (int i = 0; i %3C counter; i++)%7B%0A%09  viewList.remove(0);%0A%09%7D%0A%09break;%0A      %7D%0A    %7D%0A    if (counter %3E= 3) %7B%0A      return true;%0A    %7D else %7B%0A      return false;%0A    %7D%0A  %7D%0A%0A  public void addChild(View view)%7B%0A    viewLis
 t.add
-View
 (vie
@@ -2818,18 +2818,16 @@
 v %3E 0)%7B%0A
-//
       bu
@@ -2840,33 +2840,16 @@
 oLayout(
-checkForBigView()
 );%0A     

Change AutoColumnLayout