package com.sometrik.framework;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class FWTable extends TableLayout implements NativeCommandHandler {
  
  FrameWork frame;
  int columnCount = 4;
  ArrayList<TableRow> rowList;
  ArrayList<View> dataList;
  ViewGroup.LayoutParams params;
  boolean autoSize = false;
  
  public FWTable(FrameWork frameWork) {
    super(frameWork);
    frame = frameWork;
    params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    setLayoutParams(params);
    rowList = new ArrayList<TableRow>();
    TableRow firstRow = new TableRow(frame);
    firstRow.setLayoutParams(params);
    rowList.add(firstRow);
    addView(firstRow);
    dataList = new ArrayList<View>();
  }

  private TableRow getCurrentRow(View view) {
    if (autoSize) {
      // Check that row wouldn't go over the screen
      TableRow row = rowList.get(rowList.size() - 1);
      Log.d("table", "Row Size Check. Before: " + frame.measureViewWidth(row));
      Log.d("table", "Row Size Check. New: " + frame.measureViewWidth(view));
      Log.d("table", "Row Size Check. Screen Size: " + frame.getScreenWidth() / 2);
      if (frame.measureViewWidth(row) + frame.measureViewWidth(view) > frame.getScreenWidth() / 2) {
	Log.d("table", "Too big yo");
	return createNewRow();
      } else {
	return rowList.get(rowList.size() - 1);
      }
    } else {
      if ((dataList.size() + 1) / columnCount > rowList.size()) {
	return createNewRow();
      } else {
	return rowList.get(rowList.size() - 1);
      }
    }
  }
  
  private TableRow createNewRow() {
    Log.d("table", "New Row");
    TableRow row = new TableRow(frame);
    row.setLayoutParams(params);
    rowList.add(row);
    addView(row);
    return row;
  }
  
  public void setAutoSize(boolean enabled){
    autoSize = enabled;
  }
  
  public void setColumnCount(int columnCount) { this.columnCount = columnCount; };


  @Override
  public void addChild(View view) {
    dataList.add(view);
    TableRow row = getCurrentRow(view);
    row.addView(view);
    Log.d("table", "Ellipsis Full Row: " + frame.measureViewWidth(row));
  }
  
  @Override
  public void addOption(int optionId, String text) {
    System.out.println("FWTable couldn't handle command");
  }

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

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

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

  @Override
  public void setViewEnabled(Boolean enabled) {
    System.out.println("FWTable couldn't handle command");
  }

  @Override
  public void setStyle(String key, String value) { }

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

@@ -692,25 +692,25 @@
 tParams.
-WRAP_CONT
+MATCH_PAR
 ENT, Vie
@@ -1397,20 +1397,16 @@
 nWidth()
- / 2
 );%0A     
@@ -1471,16 +1471,17 @@
 view) %3E 
+(
 frame.ge
@@ -1498,12 +1498,9 @@
 th()
- / 2
+)
 ) %7B%0A
@@ -1993,32 +1993,507 @@
 urn row;%0A  %7D%0A  %0A
+  public void resizeAutoSize()%7B%0A    if (autoSize)%7B%0A      Log.d(%22table%22, %22Resizing autoSize table%22);%0A      %0A      for (TableRow row : rowList)%7B%0A%09row.removeAllViews();%0A      %7D%0A      this.removeAllViews();%0A      rowList = new ArrayList%3CTableRow%3E();%0A      TableRow firstRow = new TableRow(frame);%0A      firstRow.setLayoutParams(params);%0A      rowList.add(firstRow);%0A      for (View view : dataList) %7B%0A%09TableRow row = getCurrentRow(view);%0A%09row.addView(view);%0A      %7D%0A    %7D%0A  %7D%0A  %0A
   public void se
@@ -2635,16 +2635,18 @@
 nt; %7D;%0A%0A
+ %0A
 %0A  @Over
@@ -3462,32 +3462,32 @@
  %7D%0A%0A  @Override%0A
-
   public void se
@@ -3532,11 +3532,113 @@
 ext) %7B %7D
+%0A%0A  @Override%0A  public void onScreenOrientationChange(boolean isLandscape) %7B%0A    resizeAutoSize();%0A  %7D
 %0A%7D%0A

Add resizeAutoSize