package com.sometrik.framework;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

class ViewStyleManager {
  private float displayScale = 1.0f;

  private boolean isPaddingSet = false;
  private int paddingLeft = 0, paddingTop = 0, paddingRight = 0, paddingBottom = 0;
  private Integer marginLeft = null, marginTop = null;
  private Integer marginRight = null, marginBottom = null;
  private Integer width = null, height = null;
  private Float alpha = null;
  private Integer leftPosition = null, topPosition = null;
  private Integer rightPosition = null, bottomPosition = null;
  private int minWidth = 0, minHeight = 0;
  private String title = null;
  private Integer weight = null;
  private Integer backgroundColor = null;

  private Color color = null;
  
  public ViewStyleManager(float displayScale) {
    this.displayScale = displayScale;
  }
    
  public void setStyle(String key, String value) {
    if (key.equals("padding-top")) {
      isPaddingSet = true;
      paddingTop = Integer.parseInt(value);
    } else if (key.equals("padding-bottom")) {
      isPaddingSet = true;
      paddingBottom = Integer.parseInt(value);
    } else if (key.equals("padding-left")) {
      isPaddingSet = true;
      paddingLeft = Integer.parseInt(value);
    } else if (key.equals("padding-right")) {
      isPaddingSet = true;
      paddingRight = Integer.parseInt(value);
    } else if (key.equals("margin-right")) {
      marginRight = new Integer(value);
    } else if (key.equals("margin-left")) {
      marginLeft = new Integer(value);
    } else if (key.equals("margin-top")) {
      marginTop = new Integer(value);      
    } else if (key.equals("margin-bottom")) {
      marginBottom = new Integer(value);      
    } else if (key.equals("weight")) {
      weight = new Integer(value);
    } else if (key.equals("opacity")) {
      alpha = new Float(value);
    } else if (key.equals("text-shadow")) {

    } else if (key.equals("box-shadow")) {

    } else if (key.equals("left")) {
      leftPosition = new Integer(value);
    } else if (key.equals("top")) {
      topPosition = new Integer(value);
    } else if (key.equals("right")) {
      rightPosition = new Integer(value);
    } else if (key.equals("bottom")) {
      bottomPosition = new Integer(value);
    } else if (key.equals("min-width")) {
      minWidth = Integer.parseInt(value);
    } else if (key.equals("min-height")) {
      minHeight = Integer.parseInt(value);
    } else if (key.equals("title")) {
      title = value;
    } else if (key.equals("width")) {
      if (value.equals("wrap-content")) {
	width = new Integer(LayoutParams.WRAP_CONTENT);
      } else if (value.equals("match-parent")) {
	width = new Integer(LayoutParams.MATCH_PARENT);
      } else {
	width = new Integer(value);
      }
    } else if (key.equals("height")) {
      if (value.equals("wrap-content")) {
	height = new Integer(LayoutParams.WRAP_CONTENT);
      } else if (value.equals("match-parent")) {
	height = new Integer(LayoutParams.MATCH_PARENT);
      } else {
	height = new Integer(value);
      }
    } else if (key.equals("gravity")) {
      
    } else if (key.equals("background-color")) {
      backgroundColor = new Integer(Color.parseColor(value));
    }
  }

  public void apply(View view) {
    if (alpha != null) view.setAlpha(alpha);
    if (backgroundColor != null) view.setBackgroundColor(backgroundColor);

//    if (title != null) view.setTooltipText(title);

    // Scaled parameters
    if (isPaddingSet) {
      view.setPadding(applyScale(paddingLeft),
		      applyScale(paddingTop),
		      applyScale(paddingRight),
		      applyScale(paddingBottom));
    }
    if (leftPosition != null) view.setLeft(applyScale(leftPosition));
    if (rightPosition != null) view.setRight(applyScale(rightPosition));
    if (topPosition != null) view.setTop(applyScale(topPosition));
    if (bottomPosition != null) view.setBottom(applyScale(bottomPosition));
    if (minWidth > 0) view.setMinimumWidth(applyScale(minWidth));
    if (minHeight > 0) view.setMinimumHeight(applyScale(minHeight));

    // Layout parameters
    if (weight != null || width != null || height != null ||
	marginRight != null || marginLeft != null ||
	marginTop != null || marginBottom != null) {
      
      if (view.getParent() instanceof LinearLayout) {
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();
      	if (weight != null) params.weight = weight;
      	if (marginRight != null) params.rightMargin = applyScale(marginRight);
      	if (marginLeft != null) params.leftMargin = applyScale(marginLeft);
      	if (marginTop != null) params.rightMargin = applyScale(marginTop);
      	if (marginBottom != null) params.bottomMargin = applyScale(marginBottom);
      	if (width != null) params.width = applyScale(width);
      	if (height != null) params.height = applyScale(height);
      	view.setLayoutParams(params);
      } else {
	System.out.println("this style cannot be applied to view that doesn't have LinearLayout as parent");
      }
    }
  }

  protected int applyScale(int v) {
    return (int)(v * displayScale + 0.5f);
  }

  protected float applyScale(float v) {
    return v * displayScale;
  }

}

@@ -57,16 +57,45 @@
 .Color;%0A
+import android.view.Gravity;%0A
 import a
@@ -112,16 +112,16 @@
 w.View;%0A
-
 import a
@@ -842,17 +842,16 @@
 = null;%0A
-%0A
   privat
@@ -872,16 +872,50 @@
 = null;%0A
+  private Integer gravity = null;%0A
   %0A  pub
@@ -3364,24 +3364,385 @@
 or(value));%0A
+    %7D else if (key.equals(%22gravity%22)) %7B%0A      if (value.equals(%22bottom%22)) %7B%0A%09gravity = new Integer(Gravity.BOTTOM);%0A      %7D else if (value.equals(%22top%22)) %7B%0A%09gravity = new Integer(Gravity.TOP);%0A      %7D else if (value.equals(%22left%22)) %7B%0A%09gravity = new Integer(Gravity.LEFT);%0A      %7D else if (value.equals(%22right%22)) %7B%0A%09gravity = new Integer(Gravity.RIGHT);%0A      %7D%0A
     %7D%0A  %7D%0A%0A 
@@ -4739,24 +4739,44 @@
 ginBottom !=
+ null %7C%7C%0A%09gravity !=
  null) %7B%0A   
@@ -5344,16 +5344,16 @@
 width);%0A
-
       %09i
@@ -5403,24 +5403,78 @@
 le(height);%0A
+      %09if (gravity != null) params.gravity = gravity;%0A
       %09view.

add gravity