package BWP.MobileSale;

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.RpcFacade;
import org.forks.jsonrpc.RpcObject;

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 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.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter.ViewBinder;

public class UserMessageActivity extends LoginedActivity {
	ListView listView;
	SimpleAdapter adapter;
	List<RpcObject> list;
	Button btn;
	public UserMessageActivity() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.usermassage);
		btn = (Button) findViewById(R.id.Button_Return);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		initListView();
		StartQuery();
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		StartQuery();
		super.onResume();
	}

	private void StartQuery() {
		searchTask.execute();
	}
	
	String[] columnNames = new String[]{"ID","Message_SentTime","Message_Sender","Message_Category"};
	
	int[] columnIDs = new int[]{R.id.TextViewID,R.id.TextView_SentTime,R.id.TextView_Sender,R.id.TextView_Category};
	
	CallableTask searchTask = new CallableTask(this, "查询中...") {
		public Object call() throws Exception {
			String method = "/MainSystem/MainSystem/Rpcs/MessageRpc/GetList";
			list = RpcFacade.rpcCall(method,false);			
			adapter = new SimpleAdapter(UserMessageActivity.this, list,
			R.layout.usermessage_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.TextView_SentTime) {
				((TextView)view).setText(DateTimeUtil.FormatDate((Date) data));
				return true;
			}
			else{
				return false;
			}
		}
	};
	
	private void initListView() {
		listView = ActivityUtil.Find(this, R.id.userMessageListView);
		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();
				long id = (Long) map.get("ID");
				intent.putExtra("ID", id);
				intent.setClass(arg1.getContext(), UserMessageDetailActivity.class);


				startActivity(intent);
			}

		});
	}

}
