package com.sometrik.framework;

import java.util.ArrayList;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FormView implements NativeMessageHandler {

  private LinearLayout baseLayout;
  private Context context;
  private ArrayList<ViewGroup> elementList;
  private ArrayList<TextView> TextElementList;
  private int viewId;
  private Handler mainHandler;
  
  public FormView(int id, Context context) {
    this.context = context;
    baseLayout = new LinearLayout(context);
    baseLayout.setOrientation(0);
    baseLayout.setId(0);
    viewId = id;
    FrameWork frame = (FrameWork)context;
    mainHandler = frame.mainHandler;
    
    EditText usernameEdit = new EditText(context);
    usernameEdit.setText("Username");
    baseLayout.addView(usernameEdit);
    EditText passwordEdit = new EditText(context);
    passwordEdit.setText("Password");
    baseLayout.addView(passwordEdit);
    
    Button submitButton = new Button(context);
    submitButton.setText("Submit");
    baseLayout.addView(submitButton);
    
  }
  
  //1 = vertical, 0 = horizontal
  public void changeBaseLayoutOrientation(int orientation){
    baseLayout.setOrientation(orientation);
  }
  
  public void createButton(final int id){
    Button button = new Button(context);
    button.setId(id);
    TextElementList.add(button);
    
    button.setOnClickListener(new Button.OnClickListener() {
      @Override
      public void onClick(View arg0) {
	Message msg = Message.obtain(mainHandler, 9, id);
	msg.sendToTarget();
      }
    });
  }
  
  public void createLayout(int id, int targetId, int orientation){
    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(orientation);
    elementList.add(linearLayout);
    getElementById(targetId).addView(linearLayout);
  }
  
  public void createEditText(int id, int targetId){
    EditText editText = new EditText(context);
    editText.setId(id);
    TextElementList.add(editText);
    getElementById(targetId).addView(editText);
  }
  
  public void createTextView(int id, int targetId){
    TextView textView = new TextView(context);
    textView.setId(id);
    TextElementList.add(textView);
    getElementById(targetId).addView(textView);
  }
  
  public void setTextElementText(int id, String text){
    for (TextView textView : TextElementList){
      if (textView.getId() == id){
	textView.setText(text);
      }
    }
  }
  
  public void showView(){
    FrameWork frame = (FrameWork)context;
    frame.setContentView(baseLayout);
  }
  
  private ViewGroup getElementById(int id){
    for (ViewGroup element : elementList){
      if (element.getId() == id){
	return element;
      }
    }
    return baseLayout;
  }
  
  public int getViewId(){
    return viewId;
  }

  @Override
  public void handleMessage(NativeMessage message) {
    // TODO Auto-generated method stub
    
  }

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

@@ -916,393 +916,8 @@
    %0A
-    EditText usernameEdit = new EditText(context);%0A    usernameEdit.setText(%22Username%22);%0A    baseLayout.addView(usernameEdit);%0A    EditText passwordEdit = new EditText(context);%0A    passwordEdit.setText(%22Password%22);%0A    baseLayout.addView(passwordEdit);%0A    %0A    Button submitButton = new Button(context);%0A    submitButton.setText(%22Submit%22);%0A    baseLayout.addView(submitButton);%0A    %0A
   %7D%0A
@@ -1435,244 +1435,91 @@
 oid 
-createLayout(int id, int targetId, int orientation)%7B%0A    LinearLayout linearLayout = new LinearLayout(
+showView()%7B%0A    FrameWork frame = (FrameWork)
 context
-)
 ;%0A    
-linearLayout.setOrientation(orientation);%0A    elementList.add(linearLayout);%0A    getElementById(targetId).addView(linear
+frame.setContentView(base
 Layo
@@ -1519,34 +1519,44 @@
 aseLayout);%0A  %7D%0A
+%0A
   
+@Override
 %0A  public void c
@@ -1558,393 +1558,285 @@
 oid 
-createEditText(int id, int targetId)%7B%0A    EditText editText = new EditText(context);%0A    editText.setId(id);%0A    TextElementList.add(editText);%0A    getElementById(targetId).addView(editText);%0A  %7D%0A  %0A  public void createTextView(int id, int targetId)%7B%0A    TextView textView = new TextView(context);%0A    textView.setId(id);%0A    TextElementList.add(textView
+handleMessage(NativeMessage message) %7B%0A    %0A    switch (message.getMessage())%7B%0A    case CREATE_LINEAR_LAYOUT:%0A      System.out.println(%22Formview creating layout%22);%0A      FWLayout layout = new FWLayout(context);%0A      layout.setId(message.getChildInternalId()
 );%0A    
-getElementById(targetId)
+  baseLayout
 .add
@@ -1844,657 +1844,261 @@
 iew(
-textView
+layout
 );%0A  
-%7D%0A
   
-%0A
   
-public void setTextElementText(int id, String text)%7B%0A    for (TextView textView : TextElementList)%7B%0A      if (textView.getId() == id)%7B%0A%09textView.setText(text);%0A      %7D%0A    %7D%0A  %7D%0A  %0A  public void showView()%7B%0A    FrameWork frame = (FrameWork)context;%0A    frame.setContentView(baseLayout
+break;%0A%0A    case CREATE_TEXTLABEL:%0A      TextView textView = new TextView(context);%0A      textView.setId(message.getChildInternalId()
 );%0A  
-%7D%0A
   
-%0A
   
-private ViewGroup getElementById(int id)%7B%0A    for (ViewGroup element : elementList)%7B%0A      if (element.getId() == id)%7B%0A%09return element;%0A      %7D%0A    %7D%0A    return baseLayout;%0A  %7D%0A  %0A  public int getViewId()%7B%0A    return viewId
+textView.setText(message.getTextValue());%0A      baseLayout.addView(textView)
 ;%0A  
-%7D%0A%0A  @Override%0A  public void handleMessage(NativeMessage message) %7B%0A    // TODO Auto-generated method stub
+    break;%0A    %7D
 %0A   

Remove unused functions