package BWP.MobileSale.Approval;

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.LoginedActivity;
import BWP.MobileSale.MainActivity;
import BWP.MobileSale.R;
import BWP.MobileSale.Utils;
import BWP.MobileSale.Util.ActivityUtil;
import BWP.MobileSale.Util.CallableTask;
import BWP.MobileSale.Util.DialogUtil;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class ApprovalListActivity extends LoginedActivity {

	ListView listView;
	private SimpleAdapter adapter;

	class SumInfo {
		public int RowCount;

		@Override
		public String toString() {
			return String.format("共%s条记录", RowCount);
		}

	}

	SumInfo mSumInfo = new SumInfo();

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case ApprovalEditActivity.RequestCode_Open:
			StartQuery();
			break;
		}
	}

	String[] columnNames = new String[] { "Staff_Name", "Title" };

	int[] columnIDs = new int[] { R.id.approvalStaffView,
			R.id.approvalTitleView };

	CallableTask searchTask = new CallableTask(this, "查询中...") {

		public Object call() throws Exception {
			String method = "/Breed/Approval/Rpcs/MobileApprovalRpc/Search";
			RpcObject argument = RpcObject
					.create("/Breed/Approval/Rpcs/RpcSearch");

			DFDataTable table = RpcFacade.rpcCall(method, argument);
			List<RpcObject> rows = table.getRows();
			mSumInfo.RowCount = rows.size();

			adapter = new SimpleAdapter(ApprovalListActivity.this, rows,
					R.layout.approval_list_item, columnNames, columnIDs);

			return null;
		}

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

	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST, 0, "刷新");
		menu.add(0, 2, 1, "统计");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST:
			StartQuery();
			break;
		case 2:
			DialogUtil.Alert(this, mSumInfo.toString());
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.approval_list);

		initButtonWaitingApproval();
		// initButtonMore();
		initButtonReturn();
		initListView();

		StartQuery();
	}

	private void initButtonWaitingApproval() {
		ActivityUtil.FindButton(this, R.id.Approval_Button_WaitingApproval)
				.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						StartQuery();
					}
				});
	}

	// private void initButtonMore() {
	//  ActivityUtil.FindButton(this, R.id.Approval_Button_More)
	//  .setOnClickListener(new OnClickListener() {
	// 
	//  public void onClick(View v) {
	//
	//  }
	//  });
	// }

	private void initButtonReturn() {
		ActivityUtil.FindButton(this, R.id.Approval_Button_Return)
				.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						Utils.ActivityUtil.Go(ApprovalListActivity.this,
								MainActivity.class);
						finish();
					}
				});
	}

	private void initListView() {
		listView = ActivityUtil.Find(this, R.id.ApprovalListView);

		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(), ApprovalEditActivity.class);
				startActivityForResult(intent,
						ApprovalEditActivity.RequestCode_Open);
				finish();
			}

		});
	}

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

}
