package BWP.MobileSale;

import java.util.HashMap;
import java.util.List;

import org.forks.jsonrpc.DFDataTable;
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.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class SelectBuyerActivity extends LoginedActivity {
	private SimpleAdapter adapter;
	private ListView listView;
	private EditText search;
	private Boolean mStarred;
	

	private void BeginSearch() {
		searchTask.execute();
	}
	
	String addMethod = "/Sale/Sale/Rpcs/MobileOrder_User_BuyerRpc/Add";
	String removeMethod = "/Sale/Sale/Rpcs/MobileOrder_User_Buyerrpc/Remove";
	
	private void RemoveBuyer(long buyerID){
		RpcFacade.rpcCall(removeMethod, buyerID);
		
	}

	private void AddBuyer(long buyerID){
		RpcFacade.rpcCall(addMethod, buyerID);
		
	}
	
	private String buyerType;
	private boolean useUserVsCustomerSet;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectbuyer);
		Intent intent = getIntent();
		buyerType = intent.getStringExtra("BuyerType");
		useUserVsCustomerSet =  intent.getBooleanExtra("UserVsCustomerSet",true);
		listView = ActivityUtil.Find(this, R.id.ListView);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@SuppressWarnings("unchecked")			
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				HashMap<String, Object> map = (HashMap<String, Object>) arg0
						.getItemAtPosition(arg2);
				Intent intent = new Intent();
				intent.putExtra("Result", map);
				ActivityUtil.Finish(SelectBuyerActivity.this, intent);

			}	

		});
		
		
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				RpcObject data = (RpcObject) parent
				.getItemAtPosition(position);
				Long buyerID = data.getLong("ID");
				Boolean stared = data.getBoolean("Starred");
				stared = !stared;
				data.put("Starred", stared);
				CheckBox box = (CheckBox) view.findViewById(R.id.CheckBox_Star);
				box.setChecked(stared);
				Log.i("SaleOrder", StringUtil.ToString(stared));
				if(stared){
					AddBuyer(buyerID);
				}else{
					RemoveBuyer(buyerID);
				}
				/*
				UserBuyerStarCommand command = new UserBuyerStarCommand(buyerID,stared);
				CommandService.AddCommand(command);
				Intent intent = new Intent();
				intent.setClass(view.getContext(), CommandService.class);
				startService(intent);
				*/
				return true;
			}
		});
		
		search = ActivityUtil.Find(this, R.id.EditText_Search);

		Button button = ActivityUtil.Find(this, R.id.Button_Search);

		button.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				BeginSearch();
			}

		});
		
		button.setOnLongClickListener(new OnLongClickListener() {
			

			public boolean onLongClick(View v) {
				new AlertDialog.Builder(v.getContext()).setTitle("搜索范围")
				.setItems(R.array.search_starred_options,new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						switch(which){
						case 0:
							mStarred = null;
							break;
						case 1:
							mStarred = true;
							break;
						case 2:
							mStarred = false;
							break;
						}
						BeginSearch();
					}
				}).show();
				// TODO Auto-generated method stub
				return true;
			}
		});
		mStarred = true;
		searchTask.execute();
		
		
		}
	
	ViewOnClickTask searchTask = new ViewOnClickTask(this, "查询中...") {
		
		public Object call() throws Exception {
			String method = "/Sale/Sale/Rpcs/MobileOrderBuyerRpc/Search";
			if(!useUserVsCustomerSet){
				method = "/Sale/Sale/Rpcs/MobileOrderBuyerRpc/Search2";
			}
			if(buyerType!=null && buyerType.equals("调出客户")){
				method = "/Sale/SaleXiWang/Rpcs/MobileAdjustGoodsBillRpc/SearchBuyerByUserVsBuyerSet";
			}
			
			RpcObject argument = RpcObject
					.create("/Sale/Sale/Rpcs/RpcSearch");
			argument.setString("SearchKey", search.getText()
					.toString());
			argument.setBoolean("Starred", mStarred);

			DFDataTable table = RpcFacade.rpcCall(method,argument);
			List<RpcObject> rows = table.getRows();
			
			adapter = new SimpleAdapter(SelectBuyerActivity.this,
					rows, R.layout.buyer_select_item,
					new String[] { "Name", "Code","Starred" },
					new int[] { R.id.TextView_Name,
							R.id.TextView_Code,R.id.CheckBox_Star });
			
			return null;
		}

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