package com.sometrik.framework;

import android.graphics.Bitmap.Config;

import com.sometrik.framework.NativeCommand.Selector;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

public class NavigationLayout extends LinearLayout implements NativeCommandHandler {

  FrameWork frame;
  ViewStyleManager normalStyle, activeStyle, currentStyle;

  public NavigationLayout(FrameWork frame) {
    super(frame);
    this.frame = frame;

    DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    lp.gravity = Gravity.START;
    setLayoutParams(lp);
    setBackgroundColor(Color.parseColor("#dddbd6"));
    // setBackground(frame.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame));
    ColorDrawable cd = new ColorDrawable();
    cd.setColor(Color.WHITE);
    setBackgroundDrawable(cd);
    
    final float scale = getContext().getResources().getDisplayMetrics().density;
    this.normalStyle = currentStyle = new ViewStyleManager(scale, true);
    this.activeStyle = new ViewStyleManager(scale, false);
  }

  @Override
  public void onScreenOrientationChange(boolean isLandscape) {
    invalidate();
  }

  @Override
  public void onVisibilityChanged(View changedView, int visibility) {
    switch (visibility) {
    case VISIBLE:
      System.out.println("DRAWER java: visible");
      frame.visibilityChangedEvent(System.currentTimeMillis() / 1000.0, getId(), true);
      break;
    case GONE:
    case INVISIBLE:
      System.out.println("DRAWER java: invisible");
      frame.visibilityChangedEvent(System.currentTimeMillis() / 1000.0, getId(), false);
      break;
    }
  }

  @Override
  public void addChild(View view) {
    addView(view);
  }

  @Override
  public void addOption(int optionId, String text) {
    // TODO Auto-generated method stub

  }

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

  }

  @Override
  public void setValue(String v) {
    if (v.equals("toggle")) {
      if (getVisibility() == VISIBLE) {
	frame.getDrawerLayout().setDrawerVisibility(this, false);
      } else {
	frame.getDrawerLayout().setDrawerVisibility(this, true);
      }
    }
  }

  @Override
  public void setValue(int v) {
    if (v == 0) {
      frame.setCurrentDrawerViewId(0);
    } else {
      frame.setCurrentDrawerViewId(getId());
    }
  }

  @Override
  public void setViewVisibility(boolean visible) {
    System.out.println("Drawer visibility");
    frame.getDrawerLayout().setDrawerVisibility(this, visible);
  }

  @Override
  public void setStyle(Selector selector, String key, String value) {
    if (selector == Selector.NORMAL) {
      normalStyle.setStyle(key, value);
      if (normalStyle == currentStyle) normalStyle.apply(this);
    } else if (selector == Selector.ACTIVE) {
      activeStyle.setStyle(key, value);      
      if (activeStyle == currentStyle) activeStyle.apply(this);
    }
    
    if (key.equals("add-weight")) {
      LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();
      params.weight += 1;
      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 int getElementId() {
    return getId();
  }

  @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, int width, int height, Config config) {
    // TODO Auto-generated method stub

  }

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

  }

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

}

@@ -30,48 +30,8 @@
 k;%0A%0A
-import android.graphics.Bitmap.Config;%0A%0A
 impo
@@ -81,16 +81,55 @@
 ector;%0A%0A
+import android.graphics.Bitmap.Config;%0A
 import a
@@ -301,16 +301,51 @@
 w.View;%0A
+import android.widget.FrameLayout;%0A
 import a
@@ -371,16 +371,50 @@
 rLayout;
+%0Aimport android.widget.ScrollView;
 %0A%0Apublic
@@ -445,28 +445,26 @@
 extends 
-LinearLayout
+ScrollView
  impleme
@@ -568,16 +568,37 @@
 ntStyle;
+%0A  LinearLayout base;
 %0A%0A  publ
@@ -678,16 +678,204 @@
  frame;%0A
+    base = new LinearLayout(frame);%0A%0A    FrameLayout.LayoutParams scrollParams = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
 %0A    Dra
@@ -1069,24 +1069,88 @@
 ms(lp);%0A    
+addView(base);%0A    base.setLayoutParams(scrollParams);%0A    base.
 setBackgroun
@@ -1179,22 +1179,22 @@
 r(%22#
-dddbd6
+ffffff
 %22));%0A   
  // 
@@ -1189,19 +1189,16 @@
 %22));%0A   
- //
  setBack
@@ -1207,88 +1207,47 @@
 ound
-(frame.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame
+Color(Color.parseColor(%22#ffffff%22
 ));%0A
+%0A//
     
@@ -1282,24 +1282,26 @@
 Drawable();%0A
+//
     cd.setCo
@@ -1318,16 +1318,56 @@
 WHITE);%0A
+//    base.setBackgroundDrawable(cd);%0A//
     setB
@@ -2235,24 +2235,29 @@
 view) %7B%0A    
+base.
 addView(view
@@ -3506,187 +3506,8 @@
   %7D%0A
-    %0A    if (key.equals(%22add-weight%22)) %7B%0A      LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();%0A      params.weight += 1;%0A      setLayoutParams(params);%0A    %7D%0A
   %7D%0A

Add Scrolling to NavigationLayout. fix #259