package cn.net.caoyao.cpzshandset.service.presenter.impl.query.chinatex;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.bean.ConfigBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.config.HandsetVersionConstant;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.AppUtils;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.bll.config.ConfigBll;
import cn.net.caoyao.cpzshandset.service.presenter.QueryConstact;
import cn.net.caoyao.cpzshandset.service.presenter.QueryConstact.View;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.QueryConstactImpl;
import cn.net.caoyao.cpzshandset.service.thread.ThreadPoolManager;
import cn.net.caoyao.cpzshandset.service.thread.impl.chinatex.ChinatexSubmitQueryThread;
import cn.net.caoyao.cpzshandset.ui.weight.DialogUtils;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20019ResBean;
import cn.net.handsetlib.utils.ReadDeviceIDUtil;

/**
 * @author 作者 李扬:
 * @date 创建时间：2017-5-4 下午2:15:21
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class ChinatexQueryPresenter extends QueryConstactImpl {
	private QueryConstact.View qView;
	private Context context;
	private ConfigBll configBll; // 配置设备id操作
	private ProgressDialog bProgressDialog = null;
	private int currentHansetVersion = HandsetVersionConstant.HANDSET_CHINATEX_VERSION_MODE;
	private HandsetOperation option_type = HandsetOperation.OPERATION_QUERY_CONSTANT;

	public ChinatexQueryPresenter(Context context, View qView) {
		this.qView = qView;
		this.context = context;
		start();
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
		init();
		qView.setPageTitle("查询界面");
	}
	private void init() {
		configBll = new ConfigBll(context);
	}
	@Override
	public boolean checkViewAvailable() {
		boolean b = false;
		if (qView != null && context != null) {
			b = true;
		}
		return b;
	}

	@Override
	public Handler getHandler() {
		// TODO Auto-generated method stub

		return mHandler;
	}

	

	@Override
	public void btnScannerClick() {
		if (!checkViewAvailable())
			return;
		qView.readQRCode();
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case PhoneScanner.BARCODE_READ:
				scannerRecuess(msg.obj.toString());// 开启线程

				break;
			case PhoneScanner.BARCODE_NOREAD:
				// 数据扫描失败
				if (checkViewAvailable()) {
					qView.showMsgToast("取消扫描", Toast.LENGTH_SHORT);
				}
				break;
			case RequestConstant.PROT_CHECK_LABEL_SUCCESS:
				dismissProgress();
				Prot20019ResBean res = (Prot20019ResBean) msg.obj;
				qView.setScanData(res);

				break;
			case RequestConstant.PROT_CHECK_LABEL_FAIL:
				// 请求失败
				if (!checkViewAvailable())
					return;
				dismissProgress();
				qView.showMsgToast("请求失败！", Toast.LENGTH_SHORT);
				break;
			case RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT:
				// 超时
				if (!checkViewAvailable())
					return;
				dismissProgress();
				qView.showMsgToast("请求超时！", Toast.LENGTH_SHORT);
				break;
			case RequestConstant.REQUEST_PROT01_LOGIN_FAIL:
				// 登录失败
				if (!checkViewAvailable())
					return;
				dismissProgress();
				String errorMsg = msg.obj.toString();
				int errorCode = msg.arg1;
				qView.showMsgToast(errorMsg + errorCode, Toast.LENGTH_SHORT);
				break;

			}
		}
	};

	private void scannerRecuess(String str) {
		LogUtil.d(TConfig.Echo, "scannerRecuess：" + str);
		ConfigBean configBean = configBll.queryConfigByHandsetMode(currentHansetVersion);
		qView.vibrate(100);
		if (configBean == null) {// 判断是否配置过id
			qView.showMsgToast("当前设备还未配置过手持机id，请先配置手持机的设备id",
					Toast.LENGTH_SHORT);
			return;
		}
		if (StringUtils.isEmpty(str)) {
			qView.showMsgToast("扫描标签不能为空！", Toast.LENGTH_SHORT);
			return;
		}
		if (!StringUtils.isNumber(str)) {
			qView.showMsgToast("您所扫描的不是中国追溯的合法标签！", Toast.LENGTH_SHORT);
			return;
		}
		if (!(str.length() == 16)) {
			qView.showMsgToast("您所扫描的不是中国追溯的合法标签！" + str.length(),
					Toast.LENGTH_SHORT);
			return;
		}
		String iemi = AppUtils.getIMEI(context);
		if("".equals(iemi)){
			qView.showMsgToast("iemi为空" + iemi,
					Toast.LENGTH_SHORT);
			return;
		}
	//	qView.showMsgToast(str + "", 1);
		showProgressDialog("提示", "正在提交返库信息，请稍后……");
		long deviceId = Long.parseLong(ReadDeviceIDUtil
				.readScanDecrypt(configBean.getDeviceId()));
		ThreadPoolManager.getThreadPool().execute(
				new ChinatexSubmitQueryThread(deviceId, mHandler,str, iemi));
	}

	/**
	 * 提示
	 */
	protected void showProgressDialog(String titleStr, String tipsMsg) {
		bProgressDialog = DialogUtils.showProgressDialog(bProgressDialog,
				titleStr, tipsMsg, context);
	}

	public void dismissProgress() {
		if (bProgressDialog != null) {
			try {
				DialogUtils.dismissProgressDialog(bProgressDialog);
			} catch (Exception e) {
			}
		}
	}

}
