package BWP.MobileSale.Return;

import java.util.LinkedHashMap;
import java.util.List;

import org.forks.jsonrpc.DFDataTable;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;

import BWP.MobileSale.LoginedActivity;
import BWP.MobileSale.R;
import BWP.MobileSale.Util.CallableTask;
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.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

public class SelectSaleOutStoreActivity extends LoginedActivity {
	private EditText searchKey;
	private ListView listView;
	private SimpleAdapter adapter;
	private Boolean mStarred;
	LinkedHashMap<Long, RpcObject> selected = new LinkedHashMap<Long, RpcObject>();

	ViewBinder binder = new ViewBinder() {

		public boolean setViewValue(View view, Object data,
				String textRepresentation) {
			//System.out.println("进入了");
			if (view.getId() == R.id.return_selectsaleoutstore_listview_check) {
				CheckBox box = (CheckBox) view;
				box.setChecked(selected.containsKey(data));
				//System.out.println(data.toString());
				return true;
			} 
			else {
				return false;
			}
		}

	};
	

	
	String[] s=new String[]{"SaleOutStore_ID","Goods_Name","Price","ID"};
	CallableTask searchTask=new CallableTask(this,"查询中...") {
		
		@Override
		public Object call() throws Exception {
			
			
			Intent intent=getIntent();
			long buyerid=intent.getLongExtra("Buyer_ID", 0);
			//System.out.println("得到的购货客户ID"+buyerid);
			String method = "/Sale/Returns/Rpcs/MobileReturnRpc/SearchSaleOutStore";
			RpcObject argument = RpcObject.create("/Sale/Sale/Rpcs/RpcSearch");
			argument.setString("SearchKey", searchKey.getText().toString());
			argument.setLong("LastMaxId", buyerid);
			
			DFDataTable table = RpcFacade.rpcCall(method, argument);
			List<RpcObject> rows = table.getRows();
			adapter = new SimpleAdapter(SelectSaleOutStoreActivity.this, rows,
					R.layout.return_selectsaleoutstore_listview, s, new int[] {
							R.id.return_selectsaleoutstore_listview_id, R.id.return_selectsaleoutstore_listview_goodsname,
							R.id.return_selectsaleoutstore_listview_price,R.id.return_selectsaleoutstore_listview_check});
			adapter.setViewBinder(binder);
			
			
			return null;
		}
		
		@Override
		protected void successUI() {
			listView.setAdapter(adapter);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.return_selectsaleoutstore);
		
		
		Button btnOk=(Button)findViewById(R.id.SaleOutStore_Button_OK);
		btnOk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.putExtra("Result", selected.values().toArray());
				setResult(RESULT_OK, intent);
				finish();
			}
		});
		
		
		Button btnCancel=(Button)findViewById(R.id.SaleOutStore_Button_Cancel);
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		initSearch();		
		
		initListView();
		
		
		searchTask.execute();
	}
	private void initSearch()
	{
		searchKey=(EditText)findViewById(R.id.SaleOutStore_EditText_Search);
		Button searchButton=(Button)findViewById(R.id.SaleOutStore_Button_Search);
		searchButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				searchTask.execute();
			}
		});
	}
	
	private void initListView()
	{
		listView=(ListView)findViewById(R.id.SaleOutStore_ListView);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				RpcObject data=(RpcObject)arg0.getItemAtPosition(arg2);
				Long id = data.getLong("ID");
				
				if (selected.containsKey(id)) {
					selected.remove(id);
					//box.setChecked(false);
				} else {
					selected.put(id, data);
					//System.out.println("Insert id :"+id);
					//box.setChecked(true);					
				
				}
				
			}
		});
		
	}

}
