package com.chaoran.lx.activity;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.DocumentException;

import com.chaoran.entiry.DanJuEntity;
import com.chaoran.entiry.Sys_DanJuFormsOptions;
import com.chaoran.thread.DownDJRun;
import com.activity.chaoran.R;
import com.util.DialogUtil;
import com.util.IoUtil;
import com.util.LxParamPageCreate;
import com.util.SqlUtil;

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.view.View;
import android.widget.RelativeLayout;

public class Lx_Param_Activity extends Activity {
	private HashMap map;
	private RelativeLayout lx_page;
	private ProgressDialog pd;
	private Handler downDJhandler = new Handler() {
		@Override
		// 当有消息发送出来的时候就执行Handler的这个方法
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			pd.dismiss();
			if (msg.what == -1) {
				DialogUtil.builder(Lx_Param_Activity.this, "错误信息",
						"查询参数页面内容失败:" + msg.obj,0);
			} else {
				Object initOb = msg.obj;
				if (initOb instanceof Map) {
					HashMap djInitMap = (HashMap) initOb;
					Sys_DanJuFormsOptions danju = (Sys_DanJuFormsOptions) djInitMap
							.get("danju");
					ByteArrayInputStream in = null;
					try {
						DanJuEntity dj = (DanJuEntity) IoUtil.byte_obj2(danju
								.getFORM());
						String[] arrs = dj.pageNr.split("\\^");
						if (arrs.length > 1) {
							dj.pageNr = arrs[0];
						}
						in = new ByteArrayInputStream(dj.pageNr.getBytes());
					} catch (Exception e) {
						DialogUtil.builder(Lx_Param_Activity.this, "错误信息",
								"生成参数页面失败！" + e.toString(),0);
					} finally {
						try {
							in.close();
							LxParamPageCreate.lxParamPageCreate(in,
									Lx_Param_Activity.this, lx_page);

						} catch (IOException e) {
							DialogUtil.builder(Lx_Param_Activity.this, "错误信息",
									"生成参数页面流失败！" + e.toString(),0);
						} catch (DocumentException e) {
							DialogUtil.builder(Lx_Param_Activity.this, "错误信息",
									"生成参数页面失败！" + e.toString(),0);
						}
						in = null;
					}
				}else{
					if (initOb.equals("0")) {
						DialogUtil.builder(Lx_Param_Activity.this, "提示信息", "PDA未注册！",0);
					} else if (initOb.equals("1")) {
						DialogUtil.builder(Lx_Param_Activity.this, "提示信息",
								"PDA还未授权使用！",0);
					} else if (initOb.equals("2")) {
						DialogUtil.builder(Lx_Param_Activity.this, "提示信息", "试用期已过！",0);
					} else if (initOb.equals("该模块未授权")) {
						DialogUtil.builder(Lx_Param_Activity.this, "提示信息", "该模块未授权！",0);
					}
				}
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lx_param_activity);
		Bundle bundle = this.getIntent().getExtras();
		map = (HashMap) bundle.getSerializable("map");
		lx_page = (RelativeLayout) findViewById(R.id.lx_page);
		pd = new ProgressDialog(Lx_Param_Activity.this);
		pd.setCancelable(false);
		DialogUtil.setDialog(pd, "信息提示", "正在下载参数页面数据");
		new Thread(new DownDJRun(map.get("formlx").toString(), downDJhandler))
				.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(map!=null){
			map.clear();
			map=null;
		}
		lx_page=null;
		pd=null;
	}

	public void sub_back(View v) {
		HashMap tempMap = SqlUtil
				.regSql(map.get("downsql").toString(), lx_page);
		map.put("downsql", tempMap.get("sql"));
		map.put("param", tempMap.get("param"));
		Bundle bundle = new Bundle();
		bundle.putSerializable("map", map);
		Intent intent = getIntent();
		intent.putExtras(bundle);
		setResult(1, intent);
		finish();
	}
}
