package com.chaoran.lx.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import com.chaoran.component.ButAdapter;
import com.chaoran.db.DBManager;
import com.chaoran.lx.thread.LxDataDownThread;
import com.activity.chaoran.R;
import com.activity.chaoran.RunYmupThread;
import com.util.DialogUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TextView;

public class DownDataActivity extends Activity {
	private ListView listView;
	private ArrayList mData;
	private ButAdapter butAdapter;
	private ProgressDialog pd;
	private DBManager dbManager;
	private TextView tv;// 有参数页面的才保存到这里 ，没参数页面的次控件随流程走 以免后期改造成多线程下载造成并发问题；
	private Handler dataDownHandler = new Handler() {
		@Override
		// 当有消息发送出来的时候就执行Handler的这个方法
		public void handleMessage(Message msg) {
			if (msg.what == 10) {
				pd.setMessage(msg.obj.toString());
			} else if (msg.what == -1) {
				pd.dismiss();
				DialogUtil.builder(DownDataActivity.this, "错误信息", "下载数据失败！"
						+ msg.obj,0);
			} else {
				pd.dismiss();
				ArrayList tempList = (ArrayList) msg.obj;
				String tablename = tempList.get(0).toString();
				int size = dbManager.rsCount(tablename);
				DialogUtil.builder(DownDataActivity.this, "提示信息", "下载数据成功！共下载"
						+ size + "条",0);
				((TextView) tempList.get(1)).setText(tempList.get(2).toString().concat("(")
						.concat(String.valueOf(size)).concat(")"));
			}
		}
	};
	private Handler tableDownhandler = new Handler() {
		@Override
		// 当有消息发送出来的时候就执行Handler的这个方法
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == -1) {
				pd.dismiss();
				DialogUtil.builder(DownDataActivity.this, "错误信息", "下载资料表失败！"
						+ msg.obj,0);
			} else {
				mData = (ArrayList) msg.obj;
				butAdapter = new ButAdapter(DownDataActivity.this, mData, pd,
						dbManager, "tablename","fanganmc");
				listView.setAdapter(butAdapter);
				pd.dismiss();
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.down_data_activity);
		pd = new ProgressDialog(DownDataActivity.this);
		pd.setCancelable(false);
		DialogUtil.setDialog(pd, "信息提示", "正在下载离线表资料");
		new RunYmupThread(null, tableDownhandler, "tableListDown", 1).start();
		listView = (ListView) findViewById(R.id.downlist);
		dbManager = new DBManager(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (butAdapter != null) {
			butAdapter.clean();
			butAdapter = null;
		}
		if (mData != null) {
			mData.clear();
			mData = null;
		}
		if (dbManager != null) {
			dbManager.closeDB();
			dbManager = null;
		}
		if (listView != null) {
			listView = null;
		}
		if (pd != null) {
			pd.dismiss();
			pd = null;
		}
		tv = null;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case 1:
			Bundle bunde = data.getExtras();
			HashMap map = (HashMap) bunde.getSerializable("map");
			runDownThread(map, tv);
			break;
		default:
			break;
		}
	}

	public void downData(HashMap map, TextView tv) {
		Object formlx = map.get("formlx");
		System.out.println(formlx.toString()
				+ "===================================");
		if (formlx != null && formlx.toString().trim().length() > 0) {
			this.tv = tv;
			Bundle bundle = new Bundle();
			bundle.putSerializable("map", map);
			Intent intent = new Intent();
			intent.putExtras(bundle);
			intent.setClass(getApplicationContext(), Lx_Param_Activity.class);
			startActivityForResult(intent, 1);
		} else {
			runDownThread(map, tv);
			// HashMap paramMap=new HashMap();
			// String
			// sql="select * from (".concat(map.get("downsql").toString()).concat(") temptable where rowNumber between")
			// paramMap.put("sql",map.get("downsql"));
			// if(map.containsKey("param")){
			// paramMap.put("param", map.get("param"));
			// }
			// byte[] b;
			// try {
			// b = IoUtil.getbyte(paramMap);
			// String paramString = Base64.encode(b);
			// new RunYmupThread(paramString, dataDownHandler , "lxDataDown",
			// 1).start();
			// DialogUtil.setDialog(pd, "下载数据",
			// "正在下载"+map.get("tablename")+"表数据");
			// } catch (Exception e) {
			// DialogUtil.builder(DownDataActivity.this, "错误信息",
			// "组织下载数据失败！" + e.toString());
			// }
		}
	}

	public void runDownThread(HashMap map, TextView tv) {
		HashMap param = null;
		if (map.containsKey("param")) {
			param = (HashMap) map.get("param");
		}
		Set<String> set = map.keySet();
		for (String s : set) {
			System.out.println(s + "-----" + map.get(s));
		}
		LxDataDownThread lxDataDownThread = new LxDataDownThread(
				dataDownHandler, 1, Integer.parseInt(map.get("paging")
						.toString()), dbManager, map.get("downsql").toString().toUpperCase(),
				param, map.get("tablename").toString().toUpperCase(), map.get(
						"createtabelsql").toString().toUpperCase(), pd, tv, map.get("fanganmc").toString());
		new Thread(lxDataDownThread).start();
		DialogUtil.setDialog(pd, "下载数据", "正在下载" + map.get("tablename")
				+ "表数据,第1页");
	}
}
