package BWP.MobileSale;

import java.util.List;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;

import BWP.MobileSale.Util.ActivityUtil;
import BWP.MobileSale.Util.StringUtil;
import BWP.MobileSale.Util.ViewOnClickTask;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SelectRemarkActivity extends LoginedActivity {

	private TextView textViewRemark;
	private Spinner filter;
	ArrayAdapter<String> adapter;
	
	ViewOnClickTask searchTask = new ViewOnClickTask(this, "查询中...") { 
		public Object call() throws Exception {
			String method = "/Sale/Sale/Rpcs/MobileOrderRemarkRpc/Load"; 
			List<String> remarks = RpcFacade.rpcCall(method);
			remarks.add(0, ""); 
			adapter = new ArrayAdapter<String>(SelectRemarkActivity.this,
					android.R.layout.simple_spinner_item, remarks);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
			return null;
		}

		@Override
		protected void successUI() {
			filter.setAdapter(adapter);

		}

	};

ViewOnClickTask saveTask = new ViewOnClickTask(this, "保存中...") {
		
		public Object call() throws Exception {
			String method = "/Sale/Sale/Rpcs/MobileOrderRemarkRpc/Save";

			RpcObject argument = RpcObject
					.create("/Sale/Sale/Rpcs/RpcSearch");
			argument.setString("SearchKey", textViewRemark.getText()
					.toString());
			 
			RpcFacade.rpcCall(method, argument); 
			return null;
		}

		@Override
		protected void successUI() {
			Intent intent = new Intent();
			intent.putExtra("Result", textViewRemark.getText()
					.toString());
			ActivityUtil.Finish(SelectRemarkActivity.this, intent);
		}
		
	};
	
	
	private void InitRemark() {
		textViewRemark = ActivityUtil.Find(this, R.id.EditText_Remark);
		ActivityUtil.Find(this, R.id.EditText_Remark).setVisibility(
				View.VISIBLE);
	}

	private void initFilter() {
		filter = ActivityUtil.Find(this, R.id.Spinner_Filter);
		filter.setSelection(-1);
		filter.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				String v = filter.getSelectedItem().toString();
				textViewRemark.setText(v);
				if (!StringUtil.IsNullOrEmpty(v)) {
					Intent intent = new Intent();
					intent.putExtra("Result", filter.getSelectedItem()
							.toString());
					ActivityUtil.Finish(SelectRemarkActivity.this, intent);
				}
			}

			public void onNothingSelected(AdapterView<?> parent) {

			}
		});
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.selectremark);
		ActivityUtil.FindButton(this, R.id.Button_Save).setOnClickListener(
				new OnClickListener() {

					public void onClick(View v) {
						saveTask.execute(); 
					}
				});
		InitRemark();
		initFilter();

		searchTask.execute();

	}
	
}
