package cn.net.caoyao.cpzshandset.ui.activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.ConfigBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.CacheGet;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.service.bll.config.ConfigBll;
import cn.net.caoyao.cpzshandset.service.thread.ThreadPoolManager;
import cn.net.caoyao.cpzshandset.service.thread.impl.checkAvaiable.TestBaiDuThread;
import cn.net.caoyao.cpzshandset.service.thread.impl.checkAvaiable.TestThread;
import cn.net.caoyao.cpzshandset.service.tools.BaseHandler;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.DialogUtils;
import cn.net.handsetlib.utils.ReadDeviceIDUtil;

public class JianCeActivity extends BaseScanActivity {
	private Button btnNet;
	private Button btnScan;
	private TextView tvShowInfo;
	static int test = 0;
	private MHandler mHandler;
	private Long devicId = 0L;
	private ProgressDialog bProgressDialog = null;
	@SuppressLint("HandlerLeak")
	private class MHandler extends BaseHandler {

		public MHandler(Context mContext) {
			super(mContext);
		}

		@Override
		public void handleMessage(Message msg) {
			LogUtil.d(TConfig.Echo, "what:" + msg.what + "  obj:" + msg.obj);
			switch (msg.what) {
			case scanErrLabel:
			case PhoneScanner.BARCODE_READ:
				test++;
//				PhoneCilicoSoundUtils.play();
				vibrateReadQRComplete(100);
				String strScanLabel = msg.obj.toString();
				tvShowInfo.setText("第" + test + "次扫码:" + strScanLabel + "\n"
						+ tvShowInfo.getText().toString());
				break;
			case PhoneScanner.BARCODE_NOREAD:

				break;
			case RequestConstant.REQUEST_PROT01_LOGIN_SUCCESS:
				// TODO 连接成功
				dismissProgress();
//				String strOk = "连接成功";
				String strOk = (String) msg.obj;
				tvShowInfo.setText(strOk + "\n"
						+ tvShowInfo.getText().toString());
				break;
			case RequestConstant.REQUEST_PROT01_LOGIN_FAIL:
				dismissProgress();
//				String errorMsg = msg.obj.toString();
//				int errorCode = msg.arg1;
				String strErr1 = (String) msg.obj;
				tvShowInfo.setText(strErr1 
						+ "\n" + tvShowInfo.getText().toString());
				break;
			case RequestConstant.REQUEST_FAIL:
				dismissProgress();
//				String strErr2 = "请求失败！";
				String strErr2 = (String) msg.obj;
				tvShowInfo.setText(strErr2 + "\n"
						+ tvShowInfo.getText().toString());
				break;
			case RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT:
				dismissProgress();
//				String strErr3 = "请求超时！";
				String strErr3 = (String) msg.obj;
				tvShowInfo.setText(strErr3 + "\n"
						+ tvShowInfo.getText().toString());
				break;
			/*
			 * case loginErr: // TODO 连接失败 String strErr = msg.obj.toString();
			 * tvShowInfo.setText(strErr + "\n" +
			 * tvShowInfo.getText().toString()); break;
			 */

			default:
				super.handleMessage(msg);
				break;
			}
		}
	}

	@Override
	protected void onDestroy() {
		PhoneScanner.isForTestData = false;
		super.onDestroy();
	}

	@Override
	protected Handler initHandler() {
		return mHandler;
	}

	@Override
	public int loadView() {
		mHandler = new MHandler(this);
		PhoneScanner.isForTestData = true;
		return R.layout.activity_testhandset;
	}

	@Override
	public void initTitleView() {
		TextView tv_title_common = (TextView) findViewById(R.id.tv_title_common);
		tv_title_common.setText("检测");
		View image_title_back_common = findViewById(R.id.image_title_back_common);
		image_title_back_common.setVisibility(View.VISIBLE);
		image_title_back_common.setOnClickListener(new OnClickListener() {

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

	@Override
	public void initPresenter() {

	}

	@Override
	public void setListener() {
		btnScan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				PhoneScanner.Read(createService, cacheG, bContext);
			}
		});
		btnNet.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 走协议
				// new TestThread(mHandler, bContext).start();
//				getDevicId();
//				devicId = 35800020L;
				
//				if (devicId != 0L) {
					showProgressDialog("提示", "正在连接...");
					ThreadPoolManager.getThreadPool().execute(
							new TestThread(mHandler, devicId));
//					ThreadPoolManager.getThreadPool().execute(
//							new Test109Thread(mHandler, devicId));
//					ThreadPoolManager.getThreadPool().execute(
//							new Test8002Thread(mHandler, devicId));
//					ThreadPoolManager.getThreadPool().execute(
//							new Test8990Thread(mHandler, devicId));
					ThreadPoolManager.getThreadPool().execute(
							new TestBaiDuThread(mHandler));
//					ThreadPoolManager.getThreadPool().execute(
//							new TestJinThread(mHandler));
//				} else {
//					Toast.makeText(bContext, "请先配置id", 1).show();
//				}

			}
		});
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void initView() {
		btnNet = (Button) findViewById(R.id.test_btn_net);
		btnScan = (Button) findViewById(R.id.test_btn_scan);
		tvShowInfo = (TextView) findViewById(R.id.test_showInfo);
	}
	/**
	 * 提示正在更新数据
	 */
	protected void showProgressDialog(String titleStr, String tipsMsg) {
		bProgressDialog = DialogUtils.showProgressDialog(bProgressDialog, titleStr,
				tipsMsg, bContext);
	}
	
	public void dismissProgress() {
		if (bProgressDialog != null) {
			try {
				DialogUtils.dismissProgressDialog(bProgressDialog);
			} catch (Exception e) {
			}
		}
	}
	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 (configBean == null) {
			devicId = 0L;//根据模式查不到sd卡中的配置过的id 请先配置id
			return;
		}
		if ("".equals(configBean.getDeviceId())
				|| configBean.getDeviceId() == null) {
			devicId = 0L;//根据模式查不到sd卡中的配置过的id 请先配置id
			return;
		}
		devicId = Long.parseLong(ReadDeviceIDUtil.readScanDecrypt(configBean
				.getDeviceId()));
		Log.d("--==当前检测设备id是", "--==" + devicId);

	}

}
