package com.sometrik.framework;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class NavigationBar extends FrameLayout implements NativeCommandHandler {
  
  FrameWork frame;
  LinearLayout baseLayout;
  LinearLayout.LayoutParams childParams;
  ArrayList<ImageView> childList;
  int displayScale = 1;
  
  public NavigationBar(FrameWork frame) {
    super(frame);

    this.frame = frame;
    this.setBackground(frame.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame));
    childList = new ArrayList<ImageView>();
    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) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void addOption(int optionId, String text) {

    for (ImageView view : childList) {
      if (optionId == view.getId()) {
	try {
	  InputStream stream = frame.getAssets().open(text);
	  view.setImageBitmap(BitmapFactory.decodeStream(stream));
	} catch (IOException e) {
	  e.printStackTrace();
	}
      }
    }
  }

  @Override
  public void addColumn(String text, int columnType) {
    final int buttonId = columnType;
    ImageView view = new ImageView(frame);
    view.setLayoutParams(childParams);
    view.setId(columnType);
    InputStream stream;
    view.setScaleType(ScaleType.CENTER_INSIDE);
    
    if (text != "") {
      try {
        stream = frame.getAssets().open(text);
        view.setImageBitmap(BitmapFactory.decodeStream(stream));
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    
    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);
      }
    });

    childList.add(view);
    baseLayout.addView(view);
  }

  @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(String key, String value) {
    // TODO Auto-generated method stub
    
  }

  @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) {
    // TODO Auto-generated method stub
    
  }

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

}

@@ -150,16 +150,47 @@
 actory;%0A
+import android.graphics.Color;%0A
 import a
@@ -566,42 +566,8 @@
 ms;%0A
-  ArrayList%3CImageView%3E childList;%0A
   in
@@ -780,52 +780,8 @@
 ));%0A
-    childList = new ArrayList%3CImageView%3E();%0A
     
@@ -1550,906 +1550,150 @@
     
-// TODO Auto-generated method stub%0A    %0A  %7D%0A%0A  @Override%0A  public void addOption(int optionId, String text) %7B%0A%0A    for (ImageView view : childList) %7B%0A      if (optionId == view.getId()) %7B%0A%09try %7B%0A%09  InputStream stream = frame.getAssets().open(text);%0A%09  view.setImageBitmap(BitmapFactory.decodeStream(stream));%0A%09%7D catch (IOException e) %7B%0A%09  e.printStackTrace();%0A%09%7D%0A      %7D%0A    %7D%0A  %7D%0A%0A  @Override%0A  public void addColumn(String text, int columnType) %7B%0A    final int buttonId = columnType;%0A    ImageView view = new ImageView(frame);%0A    view.setLayoutParams(childParams);%0A    view.setId(columnType);%0A    InputStream stream;%0A    view.setScaleType(ScaleType.CENTER_INSIDE);%0A    %0A    if (text != %22%22) %7B%0A      try %7B%0A        stream = frame.getAssets().open(text);%0A        view.setImageBitmap(BitmapFactory.decodeStream(stream));%0A      %7D catch (IOException e) %7B%0A        e.printStackTrace();%0A      %7D%0A    %7D%0A    
+System.out.println(%22addChild on NavigationBar %22 + view.getId());%0A    final int buttonId = view.getId();%0A    view.setLayoutParams(childParams);
 %0A   
@@ -1738,16 +1738,17 @@
 stener()
+ 
 %7B%0A      
@@ -1952,26 +1952,30 @@
 %7D);%0A
-%0A
     
-childLis
+baseLayou
 t.add
+View
 (vie
@@ -1984,35 +1984,217 @@
 ;%0A  
-  baseLayout.addView(view);
+%7D%0A%0A  @Override%0A  public void addOption(int optionId, String text) %7B%0A    System.out.println(%22navigationBar couldn't handle command%22);%0A  %7D%0A%0A  @Override%0A  public void addColumn(String text, int columnType) %7B%0A    
 %0A  %7D

Change NavigationBar to use child views