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.MATCH_PARENT, 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());
      if (frame.measureViewWidth(row) + frame.measureViewWidth(view) > (frame.getScreenWidth())) {
	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 resizeAutoSize(){
    if (autoSize){
      Log.d("table", "Resizing autoSize table");
      
      for (TableRow row : rowList){
	row.removeAllViews();
      }
      this.removeAllViews();
      rowList = new ArrayList<TableRow>();
      TableRow firstRow = new TableRow(frame);
      firstRow.setLayoutParams(params);
      rowList.add(firstRow);
      for (View view : dataList) {
	TableRow row = getCurrentRow(view);
	row.addView(view);
      }
    }
  }
  
  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) { }

  @Override
  public void onScreenOrientationChange(boolean isLandscape) {
    resizeAutoSize();
  }

  @Override
  public void addData(int rowNumber, int columnNumber, String text) {
    System.out.println("FWTable couldn't handle command");
  }
}

@@ -201,16 +201,52 @@
 Parent;%0A
+import android.widget.LinearLayout;%0A
 import a
@@ -264,32 +264,32 @@
 et.TableLayout;%0A
-
 import android.w
@@ -518,25 +518,60 @@
 List;%0A  
-ViewGroup
+LinearLayout.LayoutParams params;%0A  TableRow
 .LayoutP
@@ -568,33 +568,41 @@
 ow.LayoutParams 
-p
+rowChildP
 arams;%0A  boolean
@@ -712,17 +712,25 @@
 rk;%0A    
-p
+rowChildP
 arams = 
@@ -733,25 +733,140 @@
 s = new 
-ViewGroup
+TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);%0A    params = new LinearLayout
 .LayoutP
@@ -871,25 +871,28 @@
 tParams(
-ViewGroup
+LinearLayout
 .LayoutP
@@ -911,25 +911,28 @@
 PARENT, 
-ViewGroup
+LinearLayout
 .LayoutP
@@ -948,24 +948,47 @@
 P_CONTENT);%0A
+    params.weight = 1;%0A
     setLayou
@@ -2913,24 +2913,66 @@
 iew view) %7B%0A
+    view.setLayoutParams(rowChildParams);%0A
     dataList
@@ -3050,81 +3050,8 @@
 w);%0A
-    Log.d(%22table%22, %22Ellipsis Full Row: %22 + frame.measureViewWidth(row));%0A
   %7D%0A

Fix nested linearLayouts not showing elements