package com.sometrik.framework;

import com.sometrik.framework.NativeCommand.Selector;

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;

  public ViewStyleManager(float displayScale) {
    this.displayScale = displayScale;
  }
    
  public void setStyle(Selector selector, 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")) {
      
    }
  }

  public void apply(View view) {
    if (alpha != null) view.setAlpha(alpha);
//    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;
  }

}

@@ -37,56 +37,32 @@
 ort 
-com.sometrik.framework.NativeCommand.Select
+android.graphics.Col
 or;%0A
-%0A
 impo
@@ -771,16 +771,91 @@
 = null;%0A
+  private Integer backgroundColor = null;%0A%0A  private Color color = null;%0A  
 %0A  publi
@@ -969,27 +969,8 @@
 yle(
-Selector selector, 
 Stri
@@ -3191,24 +3191,135 @@
 )) %7B%0A      %0A
+    %7D else if (key.equals(%22background-color%22)) %7B%0A      backgroundColor = new Integer(Color.parseColor(value));%0A
     %7D%0A  %7D%0A%0A 
@@ -3395,16 +3395,92 @@
 alpha);%0A
+    if (backgroundColor != null) view.setBackgroundColor(backgroundColor);%0A%0A
 //    if

add background color