package com.sometrik.framework;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class FWEditText extends EditText implements NativeCommandHandler {

  private FrameWork frame;
  private Date lastTypeTime;
  
  public FWEditText(FrameWork frameWork) {
    super(frameWork);
    this.frame = frameWork;

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    setLayoutParams(params);
  }
  
  public void addDelayedChangeListener(final int viewId){
    
    addTextChangedListener(new TextWatcher() {
      public void afterTextChanged(Editable editable) {  }

      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	lastTypeTime = new Date();
      }

      @Override
      public void onTextChanged(final CharSequence text, int arg1, int arg2, int arg3) {

	System.out.println("BigEditText TextChanged");
	// dispatch after done typing (1 sec after)
	Timer t = new Timer();
	TimerTask tt = new TimerTask() {
	  @Override
	  public void run() {
	    Date myRunTime = new Date();
	    System.out.println("BigEditText TextChanged run!");

	    if ((lastTypeTime.getTime() + 2000) <= myRunTime.getTime()) {
	      post(new Runnable() {
		@Override
		public void run() {
		  System.out.println("typing finished. Sending update");
		  System.out.println("update: " + text.toString());
//		  frame.textChangedEvent(System.currentTimeMillis() / 1000.0, viewId, text);
		}
	      });
	    } else {
	      System.out.println("Canceled");
	    }
	  }
	};
	t.schedule(tt, 2000);
      }
    });
  }

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

  @Override
  public void setValue(String v) {
    setText(v);
  }

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

  @Override
  public void setViewEnabled(Boolean enabled) {
    setEnabled(enabled);
  }

  @Override
  public void setStyle(String key, String value) {
    if (key.equals("add_weight")) {
      LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();
      params.weight += 1;
      setLayoutParams(params);
      System.out.println("edittext weight: " + params.weight);
    } else if (key.equals("width")) {
      LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();
      if (value.equals("wrap_content")) {
	params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
      } else if (value.equals("match_parent")) {
	params.width = LinearLayout.LayoutParams.MATCH_PARENT;
      }
      setLayoutParams(params);
    } else if (key.equals("height")) {
      LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();
      if (value.equals("wrap_content")) {
	params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
      } else if (value.equals("match_parent")) {
	params.height = LinearLayout.LayoutParams.MATCH_PARENT;
      }
      setLayoutParams(params);
    }
  }

  @Override
  public void setError(boolean hasError, String errorText) {
    setError(hasError ? errorText : null);
  }
  
  @Override
  public int getElementId() {
    return getId();
  }

  @Override
  public void onScreenOrientationChange(boolean isLandscape) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void addData(String text, int row, int column, int sheet) {
    System.out.println("FWEditText couldn't handle command");
  }

  @Override
  public void setViewVisibility(boolean visibility) {
    if (visibility){
      this.setVisibility(VISIBLE);
    } else {
      this.setVisibility(INVISIBLE);
    }
  }
}

@@ -193,24 +193,53 @@
 extWatcher;%0A
+import android.view.Gravity;%0A
 import andro
@@ -3468,24 +3468,461 @@
 ms(params);%0A
+    %7D else if (key.equals(%22gravity%22)) %7B%0A      LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();%0A      params.weight = 1;%0A      if (value.equals(%22bottom%22)) %7B%0A%09params.gravity = Gravity.BOTTOM;%0A      %7D else if (value.equals(%22top%22)) %7B%0A%09params.gravity = Gravity.TOP;%0A      %7D else if (value.equals(%22left%22)) %7B%0A%09params.gravity = Gravity.LEFT;%0A      %7D else if (value.equals(%22right%22)) %7B%0A%09params.gravity = Gravity.RIGHT;%0A      %7D%0A
     %7D%0A  %7D%0A%0A 

Add gravity style