package com.sometrik.framework;

import java.util.Map.Entry;
import java.util.TreeMap;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class FWPicker extends Spinner implements NativeCommandHandler {
  
  private Context context;
  private ArrayAdapter<String> adapter;
  private TreeMap<Long, String> valueMap;
  private final int id;
  
  private native void pickerOptionSelected(double timestamp, int id, int position);

  public FWPicker(Context context) {
    super(context);
    adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
    valueMap = new TreeMap<Long, String>();
    id = getId();
    this.context = context;
    
    setOnItemSelectedListener(new OnItemSelectedListener() {

      @Override
      public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
	pickerOptionSelected(System.currentTimeMillis() / 1000.0, id, position);
      }

      @Override
      public void onNothingSelected(AdapterView<?> arg0) {
      }
    });
  }

  public void handleCommand(NativeCommand command) {
  }

  @Override
  public void showView() {
    System.out.println("Picker couldn't handle showView");
  }

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

  @Override
  public void addChild(View view) {
    System.out.println("Picker couldn't handle addChild");
    //TODO
  }

  @Override
  public void addOption(long optionId, String text) {
    adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
    valueMap.put(optionId, text);
    for(Entry<Long, String> entry : valueMap.entrySet()) {
	adapter.add(entry.getValue());
    }
    setAdapter(adapter);
  }

  @Override
  public void removeChild(int id) {
    System.out.println("Picker couldn't handle remove element");
    // TODO Auto-generated method stub
  }
}

@@ -26,16 +26,44 @@
 ework;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -82,16 +82,16 @@
 .Entry;%0A
-
 import j
@@ -426,38 +426,33 @@
 ate 
-TreeMap%3CLong, String%3E valueMap
+ArrayList%3CInteger%3E idList
 ;%0A  
@@ -713,43 +713,38 @@
     
-valueMap = new TreeMap%3CLong, String
+idList = new ArrayList%3CInteger
 %3E();
@@ -921,20 +921,20 @@
 View%3C?%3E 
-arg0
+view
 , View a
@@ -961,12 +961,14 @@
 ong 
-arg3
+itemId
 ) %7B%0A
@@ -1030,16 +1030,27 @@
 .0, id, 
+idList.get(
 position
@@ -1050,16 +1050,17 @@
 osition)
+)
 ;%0A      
@@ -1545,20 +1545,19 @@
 dOption(
-long
+int
  optionI
@@ -1582,107 +1582,18 @@
     
-adapter = new ArrayAdapter%3CString%3E(context, android.R.layout.simple_spinner_item);%0A    valueMap.put
+idList.add
 (opt
@@ -1601,113 +1601,33 @@
 onId
-, text
 );%0A    
-for(Entry%3CLong, String%3E entry : valueMap.entrySet()) %7B%0A%09adapter.add(entry.getValue());%0A    %7D
+adapter.add(text);
 %0A   

Modify picker returning id