package cn.net.caoyao.cpzshandset.service.thread.impl.checkAvaiable;

import java.io.IOException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import cn.net.caoyao.cpzshandset.bean.ConfigBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.util.AppUtils;
import cn.net.caoyao.cpzshandset.common.util.CacheGet;
import cn.net.caoyao.cpzshandset.common.util.NetWorkUtils;
import cn.net.caoyao.cpzshandset.service.bll.config.ConfigBll;
import cn.net.caoyao.cpzshandset.service.tools.BaseHandler;
import cn.net.caoyao.cpzshandset.ui.weight.DialogUtils;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.handsetlib.utils.ReadDeviceIDUtil;

abstract class BaseThread extends Thread {

	private SocketCreate create;
	private int iErrCode = -1;
	protected String bErrMsg = "";
	protected String bErrCode = null;
	protected Context bContext;
	protected Handler bHandler;
	protected ProgressDialog bProgressDialog = null;
	private Long devicId;
	private void login() throws BindException, ConnectException,
			SocketTimeoutException, UnknownHostException, IOException {
		// long devicId = ReadDeviceIDUtil.getDevicId(bContext);
		String ip = Config.ip;
		int port = Config.port;
		int timeout = 60 * 1000;
		String imei = AppUtils.getIMEI(bContext);

		create = new SocketCreate(ip, port, timeout, timeout, imei);
		create.createLoginCpzsHandset(devicId);
	}

	@Override
	public void run() {
		super.run();
		int what = -1;
		try {
			if (!NetWorkUtils.isNetworkConnected(bContext)) {
				what = BaseHandler.netNo;
				return;
			}
			login();
			if (create.isResult()) {
				loge("连接成功");
				runProcess(create);
				bErrMsg = null;
			} else {
				loge("BaseThread-->" + create.getErrMsg());
				bErrMsg = create.getErrMsg();
				what = BaseHandler.loginErr;
				iErrCode = create.getiErrCode();
				DialogUtils.dismissProgressDialog(bProgressDialog);
			}
		} catch (ConnectException e) {
			// 连接服务器失败
			e.printStackTrace();
			loge("连接异常");
			bErrMsg = "连接异常";
			what = BaseHandler.netErr;
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
			// 超时
			loge("连接超时");
			bErrMsg = "连接超时";
			what = BaseHandler.netErr;
		} catch (UnknownHostException e) {
			e.printStackTrace();
			// 未知服务器
			loge("连接异常");
			bErrMsg = "连接异常";
			what = BaseHandler.netErr;
		} catch (BindException e) {
			e.printStackTrace();
			// IP地址或端口绑定异常
			loge("连接异常");
			bErrMsg = "连接异常";
			what = BaseHandler.netErr;
		} catch (IOException e) {
			e.printStackTrace();
			loge("未知异常");
			bErrMsg = "未知异常";
			what = BaseHandler.netErr;
		} finally {
			DialogUtils.dismissProgressDialog(bProgressDialog);
			logout();
			if (what > 0) {
				sendMsg(bHandler, what, bErrMsg, iErrCode);
			}
		}
	}

	public abstract void runProcess(SocketCreate create);

	private void logout() {
		if (null != create && create.isResult()) {
			new Prot02Logout().dealLogout(create);
		}
	}

	protected void init(Context upContext) {
		// new ResetBJTime();
		// UpStateDao dao = new UpStateDao(upContext);
		// dao.isExitToday();
	}

	protected void showProgressHorizontalDialog(int iMax) {
		bProgressDialog = DialogUtils.showProgressHorizontalDialog(
				bProgressDialog, "提示", "上传数据中", bContext, iMax);
	}

	protected void showProgressDialog() {
		bProgressDialog = DialogUtils.showProgressDialog(bProgressDialog, "提示",
				"正在加载数据", bContext);
	}

	protected void loge(Object obj) {
		Log.e("AI", obj.toString());
	}

	protected void logi(Object obj) {
		Log.i("AI", obj.toString());
	}

	protected void sendMsg(Handler mHandler, int what) {
		sendMsg(mHandler, what, "", -1, -1);
	}

	protected void sendMsg(Handler mHandler, int what, Object obj) {
		sendMsg(mHandler, what, obj, -1, -1);
	}

	protected void sendMsg(Handler mHandler, int what, Object obj, int arg1) {
		sendMsg(mHandler, what, obj, -1, -1);
	}

	protected void sendMsg(Handler mHandler, int what, Object obj, int arg1,
			int arg2) {
		Message msg = Message.obtain();
		msg.what = what;
		msg.obj = obj;
		msg.arg1 = arg1;
		msg.arg2 = arg2;
		mHandler.sendMessage(msg);
	}

	private void getDevicId() {
		int mode = 0;
		if (Config.canSwitchHandsetVersionMode) {// true是测试 含有缓存文件可切换各种模式
			mode = new CacheGet()
					.getCacheIntegerG(bContext, Config.handsetVersionConfigSp,
							Config.currentHandsetVersion);//缓存中没有则取0
		} else {
			mode = Config.CURRENT_HANDSET_VERSION_MODE;
		}

		ConfigBll bll = new ConfigBll(bContext);
		ConfigBean configBean = bll.queryConfigByHandsetMode(mode);
		if("".equals(configBean.getDeviceId())||configBean.getDeviceId()==null){
			//sendMsg(bHandler, what);
			return;
		}
		devicId = Long.parseLong(ReadDeviceIDUtil
				.readScanDecrypt(configBean.getDeviceId()));
		Log.d("--==当前检测设备id是", "--==" + devicId);

		
	}
}
