package BWP.MobileSale.ChinaPay;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

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

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.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SimpleAdapter.ViewBinder;
import BWP.MobileSale.LoginedActivity;
import BWP.MobileSale.R;

import BWP.MobileSale.Util.ActivityUtil;
import BWP.MobileSale.Util.CallableTask;
import BWP.MobileSale.Util.DateTimeUtil;
import BWP.MobileSale.Util.DialogUtil;
import BWP.MobileSale.Util.StringUtil;
import BWP.MobileSale.Util.DialogUtil.SetDate;

public class PayListActivity extends LoginedActivity {
	boolean flag = false;
	int pageCount = 0;
	private Button buttonMinDate;
	private Button buttonMaxDate;
	int pageIndex = 1;
	ListView listView;
	private SimpleAdapter adapter;
	SumInfo mSumInfo = new SumInfo();

	String[] columnNames = new String[] {"Money","Money2", "TransDate","IsSuccess", "HeadOffice_Name",
			  "Message" };

	int[] columnIDs = new int[] { R.id.TextView1,R.id.TextView6, R.id.TextView2,
			R.id.TextView3, R.id.TextView_4, R.id.TextView_5 };

	class SumInfo {
		public int RowCount;
		public BigDecimal Money;

		@Override
		public String toString() {
			return String.format("共%s元", Money);
		}

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);

		StartQuery(false);

	}

	private void StartQuery(boolean flag2) {

		searchTask.execute();
	}

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

		public Object call() throws Exception {
			String method = "/Sale/SaleExportBase/ChinaPayRpc/Search";

			if (flag == false) {
				pageCount = GetPageCount();
			}

			RpcObject argument = SetArgument();
			DFDataTable table = RpcFacade.rpcCall(method, argument);
			List<RpcObject> rows = table.getRows();
			mSumInfo.Money = new BigDecimal(0);
			for (RpcObject row : rows) {
				mSumInfo.Money = mSumInfo.Money.add(row.getMoney("Money"));
			}
			adapter = new SimpleAdapter(PayListActivity.this, rows,
					R.layout.chinapay_list_item, columnNames, columnIDs);
			adapter.setViewBinder(viewBinder);
			return null;

		}

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

	};

	ViewBinder viewBinder = new ViewBinder() {
		public boolean setViewValue(View view, Object data,
				String textRepresentation) {
			if (view.getId() == R.id.TextView2) {
				((TextView) view).setText(DateTimeUtil.FormatDate((Date) data));
				return true;
			}else if (view.getId() == R.id.TextView3) {
				String cg = "失败";
				if ((Boolean) data)
					cg = "成功";
				((TextView) view).setText(cg);
				return true;
			} else {
				return false;
			}
		}
	};

	private int GetPageCount() {
		String method = "/Sale/SaleExportBase/ChinaPayRpc/GetPageNum";
		try {
			RpcObject argument = SetArgument();
			return RpcFacade.rpcCall(method, argument);
		} catch (Exception e) {

		}
		return 0;
	}

	private RpcObject SetArgument() throws ParseException {
		RpcObject argument = RpcObject.create("/Sale/Sale/Rpcs/RpcSearch");
		argument.setString("SearchKey2", (String)suc.getSelectedItem());
		String dateText = buttonMinDate.getText().toString();
		String dateText2 = buttonMaxDate.getText().toString();
		if (!StringUtil.IsNullOrEmpty(dateText)) {
			Date parseDate = (Date) DateTimeUtil.ParseDate(dateText);
			argument.setDate("Date1", parseDate);
		}
		if (!StringUtil.IsNullOrEmpty(dateText2)) {
			Date parseEndDate = (Date) DateTimeUtil.ParseDate(dateText2);
			argument.setDate("Date2", parseEndDate);
		}
		argument.setInt("PageIndex", pageIndex);
		return argument;
	}

	@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(false);
			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.chinapay_list);

		initButtonReturn();
		initFilter();
		initListView();

		initButtonDate();
		initButtonNextPage();
		initButtonUpPage();
		StartQuery(false);

	}

	private void initButtonReturn() {
		ActivityUtil.FindButton(this, R.id.Button_Return)

		.setOnClickListener(new OnClickListener() {

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

	private void initButtonNextPage() {
		ActivityUtil.FindButton(this, R.id.Button_NextPage).setOnClickListener(
				new OnClickListener() {
					public void onClick(View v) {
						pageIndex++;
						if (pageIndex >= pageCount) {
							return;
						}
						StartQuery(true);
					}
				});
	}

	private void initButtonUpPage() {
		ActivityUtil.FindButton(this, R.id.Button_UpPage).setOnClickListener(
				new OnClickListener() {
					public void onClick(View v) {

						pageIndex--;
						if (pageIndex <= 0) {
							return;
						}
						StartQuery(true);
					}
				});
	}

	private void initListView() {
		listView = ActivityUtil.Find(this, R.id.ListView);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@SuppressWarnings("unchecked")
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			}

		});
	}

	private void initButtonDate() {
		buttonMinDate = ActivityUtil.FindButton(this, R.id.Button_Date);
		DialogUtil.AddSelectDateDialog(buttonMinDate, new SetDate() {

			@Override
			public void setDate(java.util.Date date) {
				pageIndex = 1;
				StartQuery(false);
			}

		});
		buttonMaxDate = ActivityUtil.FindButton(this, R.id.Button_EndDate);
		DialogUtil.AddSelectDateDialog(buttonMaxDate, new SetDate() {

			@Override
			public void setDate(java.util.Date date) {
				pageIndex = 1;
				StartQuery(false);
			}

		});
	}

	Spinner suc;
	private void initFilter() {
		suc = (Spinner) findViewById(R.id.Spinner_suc);
		 
		OnItemSelectedListener listener = new OnItemSelectedListener() {

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

			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		};
		
		suc.setOnItemSelectedListener(listener); 
		
	}

}
