package BWP.MobileSale.BreedLog;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

import BWP.MobileSale.LoginedActivity;
import BWP.MobileSale.R;
import BWP.MobileSale.Utils;
import BWP.MobileSale.Order.OrderEditActivity;
import BWP.MobileSale.Order.OrderListActivity;
import BWP.MobileSale.Util.ActivityUtil;
import BWP.MobileSale.Util.CallableTask;
import BWP.MobileSale.Util.DateTimeUtil;
import BWP.MobileSale.Util.StringUtil;
import android.content.Intent;
import android.content.res.Resources;
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.AdapterView.OnItemSelectedListener;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class BreedLogListActivity extends LoginedActivity {

	Button btnReturn;
	Button btnNew;
	ListView listView;
	private SimpleAdapter adapter;
	private Spinner filter;
	private Spinner billStateFilter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.breedlog_list);
		
		
		
		btnReturn=(Button)findViewById(R.id.breedlog_list_btn_return);
		btnReturn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		
		btnNew=(Button)findViewById(R.id.breedlog_list_btn_new);
		btnNew.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(v, BreedLogEditActivity.class);
				
			}
		});
		initFilter();
		
		initListView();
		try {
			RpcObject argument = RpcObject.create("/Hatch/BreedingPoultry/Rpcs/RpcSearch");
		} catch (JsonRpcException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void initFilter() {
		filter = ActivityUtil.Find(this, R.id.breedlog_list_spinnersearch);
		billStateFilter = (Spinner) findViewById(R.id.breedlog_list_spinnerbillstate);
		
		OnItemSelectedListener listener = new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				StartQuery();
				
			}

			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		};
		
		filter.setOnItemSelectedListener(listener);
		billStateFilter.setOnItemSelectedListener(listener);
		
	}
	
	
		private void initListView() {
			listView = ActivityUtil.Find(this, R.id.breedlog_list_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("ID", (Long) map.get("ID"));
					intent.setClass(arg1.getContext(),BreedLogEditActivity.class);
					startActivityForResult(intent,BreedLogEditActivity.RequestCode_Open);
				}

			});
		}		
		
		
		ViewBinder viewBinder = new ViewBinder() {
			public boolean setViewValue(View view, Object data,String textRepresentation) {
				if (view.getId() == R.id.breedlog_list_item_textviewdate) {
					((TextView)view).setText(DateTimeUtil.FormatDate((Date) data));
					return true;
				}
				else{
					return false;
				}
			}
		};
		String[] columnNames = new String[]{"ID","AccountingUnit_Name","Date","BreedFactory_Name","Batch_Name","BreederCage_Name"};
		
		int[] columnIDs = new int[]{R.id.breedlog_list_item_textviewid,R.id.breedlog_list_item_textviewaccount,R.id.breedlog_list_item_textviewdate, R.id.breedlog_list_item_textviewfactory,R.id.breedlog_list_item_textviewbatch,R.id.breedlog_list_item_textviewcage};
		
		
		
		CallableTask searchTask = new CallableTask(this, "查询中...") {

			public Object call() throws Exception {
				String method = "/Hatch/BreedingPoultry/Rpcs/MobileBreedLogRpc/Search";
				RpcObject argument = RpcObject.create("/Hatch/BreedingPoultry/Rpcs/RpcSearch");
				
				//Resources resource = getResources();
				String searchkey = (String) filter.getSelectedItem();
				argument.setString("SearchKey", searchkey);
			
				argument.setString("SearchKey2", (String)billStateFilter.getSelectedItem());				
			
				
				DFDataTable table = RpcFacade.rpcCall(method, argument);
				List<RpcObject> rows = table.getRows();		
			
				
				
				adapter = new SimpleAdapter(BreedLogListActivity.this, rows,R.layout.breedlog_list_item, columnNames,columnIDs);
				adapter.setViewBinder(viewBinder);
				return null;
			}

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


		};
		
		
		
		@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			// TODO Auto-generated method stub
			super.onActivityResult(requestCode, resultCode, data);
			
			switch(requestCode){
			case BreedLogEditActivity.RequestCode_Open:
				StartQuery();
				break;
			}
		}

		private void StartQuery() {
			searchTask.execute();
		}

	
	
}
