package com.sometrik.framework;

import android.graphics.Bitmap.Config;

import com.sometrik.framework.NativeCommand.Selector;

import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class NavigationBar extends FrameLayout implements NativeCommandHandler {
  
  FrameWork frame;
  LinearLayout baseLayout;
  LinearLayout.LayoutParams childParams;
  int displayScale = 1;
  
  public NavigationBar(FrameWork frame) {
    super(frame);
    this.frame = frame;
//    this.setBackground(frame.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame));
//    setBackgroundColor(Color.parseColor("#ffffff"));
    baseLayout = new LinearLayout(frame);
    baseLayout.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    
    baseLayout.setLayoutParams(params);
    setLayoutParams(params);
    DisplayMetrics metrics = frame.setupDisplayMetrics();
    displayScale = (int) metrics.scaledDensity;
    childParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 60 * displayScale);
    childParams.weight = 1;
    
    
    addView(baseLayout);
  }

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

  @Override
  public void addChild(View view) {
    final int buttonId = view.getId();
    view.setLayoutParams(childParams);
    view.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
	System.out.println("Navigation OnClick: " + buttonId);
	frame.intChangedEvent(System.currentTimeMillis() / 1000.0, getId(), 1, buttonId);
      }
    });
    baseLayout.addView(view);
  }

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

  @Override
  public void addColumn(String text, int columnType) {
    
  }

  @Override
  public void addData(String text, int row, int column, int sheet) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void setValue(String v) {
    // TODO Auto-generated method stub
    
  }

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

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

  @Override
  public void setViewEnabled(Boolean enabled) {
    // TODO Auto-generated method stub
    
  }

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

  @Override
  public void setStyle(Selector selector, String key, String value) {
    if (key.equals("width")) {
      LinearLayout.LayoutParams params = (android.widget.LinearLayout.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 = (android.widget.LinearLayout.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);
    }
  }

  @Override
  public void setError(boolean hasError, String errorText) {
    // TODO Auto-generated method stub
    
  }

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

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

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

  @Override
  public void setImage(byte[] bytes, int width, int height, Config config) {
    // TODO Auto-generated method stub
    
  }

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

}

@@ -530,16 +530,75 @@
 le = 1;%0A
+  ViewStyleManager normalStyle, activeStyle, currentStyle;%0A
   %0A  pub
@@ -1404,21 +1404,16 @@
 1;%0A    %0A
-    %0A
     addV
@@ -1429,16 +1429,234 @@
 ayout);%0A
+    %0A    final float scale = getContext().getResources().getDisplayMetrics().density;%0A    this.normalStyle = currentStyle = new ViewStyleManager(scale, true);%0A    this.activeStyle = new ViewStyleManager(scale, false);%0A
   %7D%0A%0A  @
@@ -3148,1118 +3148,295 @@
 if (
-key.equals(%22width%22)) %7B%0A      LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) getLayoutParams();%0A      if (value.equals(%22wrap-content%22)) %7B%0A%09params.width = LinearLayout.LayoutParams.WRAP_CONTENT;%0A      %7D else if (value.equals(%22match-parent%22)) %7B%0A%09params.width = LinearLayout.LayoutParams.MATCH_PARENT;%0A      %7D else %7B%0A%09final float scale = getContext().getResources().getDisplayMetrics().density;%0A%09int pixels = (int) (Integer.parseInt(value) * scale + 0.5f);%0A%09params.width = pixels;%0A      %7D%0A      setLayoutParams(params);%0A    %7D else if (key.equals(%22height%22)) %7B%0A      LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) getLayoutParams();%0A      if (value.equals(%22wrap-content%22)) %7B%0A%09params.height = LinearLayout.LayoutParams.WRAP_CONTENT;%0A      %7D else if (value.equals(%22match-parent%22)) %7B%0A%09params.height = LinearLayout.LayoutParams.MATCH_PARENT;%0A      %7D else %7B%0A%0A%09final float scale = getContext().getResources().getDisplayMetrics().density;%0A%09int pixels = (int) (Integer.parseInt(value) * scale + 0.5f);%0A%09params.height = pixels;%0A      %7D%0A      setLayoutParams(param
+selector == Selector.NORMAL) %7B%0A      normalStyle.setStyle(key, value);%0A      if (normalStyle == currentStyle) normalStyle.apply(this);%0A    %7D else if (selector == Selector.ACTIVE) %7B%0A      activeStyle.setStyle(key, value);      %0A      if (activeStyle == currentStyle) activeStyle.apply(thi
 s);%0A

use ViewStyleManager for styles