package com.sometrik.framework;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class NativeCommand {

  private int internalId = 0;
  private int childInternalId = 0;
  private int value = 0;
  private String textValue = "";
  private String textValue2 = "";
  private CommandType command;
  private String key;
  private FrameWork frame;
  
  public enum CommandType{
    CREATE_APPLICATION,
    SHOW_VIEW,
    SHOW,
    CREATE_FORMVIEW,
    CREATE_BUTTON,
    CREATE_PICKER, // called Spinner in Android
    CREATE_LINEAR_LAYOUT,
    CREATE_OPENGL_VIEW,
    CREATE_NATIVE_OPENGL_VIEW,
    CREATE_TEXTFIELD,
    CREATE_TEXTLABEL,
    CREATE_DIALOG, // For future
    CREATE_IMAGE_ELEMENT,
    CREATE_ACTION_SHEET,
    REQUEST_REDRAW,
    SHOW_MESSAGE_DIALOG,
    SHOW_INPUT_DIALOG,
    LAUNCH_BROWSER,
    POST_NOTIFICATION,
    SET_VALUE, //Sets value of textfields.
    SET_LABEL, //this sets label for buttons and labels.
    SET_ATTRIBUTE,
    SET_CAPTION,
    ADD_OPTION,
    QUIT_APP,

    // Timers
    CREATE_TIMER,
    DELETE_TIMER,
    
    // In-app purchases
    LIST_PRODUCTS,
    BUY_PRODUCT,
    LIST_PURCHASES,
    CONSUME_PURCHASE
  }
  
  public NativeCommand(FrameWork frame, CommandType command, int internalId, int childInternalId, String textValue, String textValue2){
    this.frame = frame;
    this.command = command;
    this.internalId = internalId;
    this.childInternalId = childInternalId;
    this.textValue = textValue;
    this.textValue2 = textValue2;

    apply(FrameWork.views.get(internalId));
  }
  public NativeCommand(FrameWork frame, int messageTypeId, int internalId, int childInternalId, int value, String textValue, String textValue2){
    this.frame = frame;
    command = CommandType.values()[messageTypeId];
    this.internalId = internalId;
    this.childInternalId = childInternalId;
    this.value = value;
    this.textValue = textValue;
    this.textValue2 = textValue2;
    
    apply(FrameWork.views.get(internalId));
  }
  
  public void apply(NativeCommandHandler view) {

    System.out.println("Processing message " + command);

    switch (command) {

    case CREATE_FORMVIEW:
      System.out.println("creating formView " + getChildInternalId());
      createFormView();
      break;
      
    case CREATE_LINEAR_LAYOUT:
      FWLayout layout = createLinearLayout();
      view.addChild(layout);
      break;

    case CREATE_BUTTON:
      Button button = createButton();
      view.addChild(button);
      break;

    case CREATE_PICKER:
      FWPicker picker = createSpinner();
      view.addChild(picker);
      break;

    case CREATE_OPENGL_VIEW:
      break;

    case CREATE_TEXTFIELD:
      EditText editText = createEditText();
      view.addChild(editText);
      break;

    case CREATE_TEXTLABEL:
      TextView textView = createTextView();
      view.addChild(textView);
      break;

    case SET_ATTRIBUTE:
      break;
    case CREATE_IMAGE_ELEMENT:
      ImageView imageView = new ImageView(frame);
      imageView.setId(getChildInternalId());
      // Missing image set
      // imageView.setImageBitmap();
      break;
      
    case SHOW_VIEW:
      view.showView();
      break;
    case ADD_OPTION:
      //Forward Command to FWPicker
      view.addOption(getValue(), getTextValue());
    case POST_NOTIFICATION:
      // TODO
      break;
    case CREATE_APPLICATION:
      frame.setAppId(getInternalId());
      break;
    case SET_CAPTION:
      frame.setTitle(getTextValue());
      break;
    case LAUNCH_BROWSER:
      // TODO
      break;
    default:
      System.out.println("Message couldn't be handled");
      break;
    }
  }
  
  private FWLayout createLinearLayout(){
    FWLayout layout = new FWLayout(frame);
    layout.setId(getChildInternalId());
    FrameWork.addToViewList(layout);
    if (getValue() == 2) {
	layout.setOrientation(LinearLayout.HORIZONTAL);
    } else if (getValue() == 1) {
	layout.setOrientation(LinearLayout.VERTICAL);
    }
    return layout;
  }

  private Button createButton() {
    Button button = new Button(frame);
    button.setId(getInternalId());
    button.setText(getTextValue());

    button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
	frame.buttonClicked(getInternalId());
      }
    });
    return button;
  }
  
  private EditText createEditText(){
    final EditText editText = new EditText(frame);
    editText.setId(getChildInternalId());
    editText.setText(getTextValue());
    editText.setMinimumWidth(400000 / (int)frame.getScreenWidth());
    editText.addTextChangedListener(new TextWatcher() {
      public void afterTextChanged(Editable editable) {

	  frame.textChangedEvent(getChildInternalId(), editable.toString());
      }
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
      public void onTextChanged(CharSequence s, int start, int before, int count) {}
   });
    return editText;
  }
  
  private FWPicker createSpinner(){
    FWPicker picker = new FWPicker(frame);
    picker.setId(getChildInternalId());
    FrameWork.views.put(getChildInternalId(), picker);
    
    return picker;
  }

  private TextView createTextView() {
    TextView textView = new TextView(frame);
    textView.setId(getChildInternalId());
    textView.setText(getTextValue());
    return textView;
  }
  
  private void createFormView(){
    FWLayout layout = new FWLayout(frame);
    layout.setId(getChildInternalId());
    FrameWork.views.put(getChildInternalId(), layout);
    ScrollView scrollView = new ScrollView(frame);
    scrollView.addView(layout);
  }

  public String getKey() {
    return key;
  }
  public void setKey(String key) {
    this.key = key;
  }
  public int getInternalId() {
    return internalId;
  }
  public int getChildInternalId() {
    return childInternalId;
  }
  public String getTextValue() {
    return textValue;
  }
  public String getTextValue2() {
    return textValue2;
  }
  public CommandType getCommand() {
    return command;
  }
  public int getValue() {
    return value;
  }
}

@@ -3551,24 +3551,37 @@
 xtValue());%0A
+      break;%0A
     case POS

add missing break;