package com.sometrik.framework;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class FWTextView extends TextView implements NativeCommandHandler {

  private FrameWork frame;
  
  public FWTextView(FrameWork frame) {
    super(frame);
    this.frame = frame;
//    Color color = new Color();
    this.setBackground(null);
    this.setBackgroundColor(Color.rgb(255, 255, 255));
  }

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

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

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

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

  @Override
  public void setStyle(String key, String value) {
    if (key.equals("font-size")) {
      if (value.equals("small")) {
	this.setTextSize(9);
      } else if (value.equals("medium")) {
	this.setTextSize(12);
      } else if (value.equals("large")) {
	this.setTextSize(15);
      } else {
	setTextSize(Integer.parseInt(value));
      }
    } else if (key.equals("padding-top")) {
      setPadding(getPaddingLeft(), Integer.parseInt(value), getPaddingRight(), getPaddingBottom());
    } else if (key.equals("padding-bottom")) {
      setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), Integer.parseInt(value));
    } else if (key.equals("padding-left")) {
      setPadding(Integer.parseInt(value), getPaddingTop(), getPaddingRight(), getPaddingBottom());
    } else if (key.equals("padding-right")) {
      setPadding(getPaddingLeft(), getPaddingTop(), Integer.parseInt(value), getPaddingBottom());
    }  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;
      } else {
	final float scale = getContext().getResources().getDisplayMetrics().density;
	int pixels = (int) (Integer.parseInt(value) * scale + 0.5f);
	params.width = pixels;
      }
      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;
      } else {

	final float scale = getContext().getResources().getDisplayMetrics().density;
	int pixels = (int) (Integer.parseInt(value) * scale + 0.5f);
	params.height = pixels;
      }
      setLayoutParams(params);
    } else if (key.equals("typeface")) {
      if (value.equals("bold")) {
        setTypeface(null, Typeface.BOLD);
      } else if (value.equals("normal")) {
        setTypeface(null, Typeface.NORMAL);
      } else if (value.equals("italic")) {
        setTypeface(null, Typeface.ITALIC);
      } else if (value.equals("bold-italic")) {
        setTypeface(null, Typeface.BOLD_ITALIC);
      }
    }
  }

  @Override
  public void setError(boolean hasError, String errorText) {
    //TODO
    System.out.println("FWTextView couldn't handle command");
  }

  @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("FWTextView couldn't handle command");
  }

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

  @Override
  public void clear() {
    System.out.println("couldn't handle command");
  }

  @Override
  public void flush() {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void addColumn(String text, int columnType) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void reshape(int value, int size) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setImage(byte[] bytes) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void reshape(int size) {
    // TODO Auto-generated method stub
    
  }
}

@@ -123,16 +123,45 @@
 peface;%0A
+import android.view.Gravity;%0A
 import a
@@ -3514,24 +3514,621 @@
 C);%0A      %7D%0A
+    %7D else if (key.equals(%22borders%22)) %7B%0A      this.setBackground(frame.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame));%0A    %7D else if (key.equals(%22weight%22)) %7B%0A      LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();%0A      params.weight = Integer.parseInt(value);%0A      setLayoutParams(params);%0A    %7D else if (key.equals(%22align-text%22)) %7B%0A      if (value.equals(%22left%22)) %7B%0A%09setTextAlignment(TEXT_ALIGNMENT_TEXT_START);%0A      %7D else if (value.equals(%22center%22)) %7B%0A%09setTextAlignment(TEXT_ALIGNMENT_CENTER);%0A%09this.setGravity(Gravity.CENTER_HORIZONTAL);%0A      %7D%0A
     %7D%0A  %7D%0A%0A 

Add alignment options to style