package cn.net.caoyao.cpzshandset.service.presenter.impl.setting.seeds;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.widget.Toast;

import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.bean.ConfigBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetVersionConstant;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.db.SeedsDBHelper;
import cn.net.caoyao.cpzshandset.service.bll.config.ConfigBll;
import cn.net.caoyao.cpzshandset.service.presenter.DeviceIdConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.DeviceIdConfigConstactImpl;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog.DSure;
import cn.net.handsetlib.utils.ReadDeviceIDUtil;

public class SeedsDeviecIdConfigPresenter extends
		DeviceIdConfigConstactImpl {
	private final DeviceIdConfigConstact.View dView;
	private Context context;
	private ConfigBll bll;
	private ConfigBean currentConfig;
	private TipsDialog tipsDialog;
	private int currentHandsetVersion = HandsetVersionConstant.HANDSET_SEED_VERSION_MODE;
	private String dbName = SeedsDBHelper.db_name;

	public SeedsDeviecIdConfigPresenter(DeviceIdConfigConstact.View dView,
			Context context) {
		this.dView = dView;
		this.context = context;
		start();
	}

	@Override
	public void start() {

		if (!checkViewAvailable())
			return;
		bll = new ConfigBll(context);
		currentConfig = bll.queryConfigByHandsetMode(currentHandsetVersion);
		if (currentConfig != null) {
			dView.setCurrentIdText(ReadDeviceIDUtil
					.readScanDecrypt(currentConfig.getDeviceId()));
		}
	}

	@Override
	public boolean checkViewAvailable() {
		boolean b = false;
		if (dView != null && context != null) {
			b = true;
		}
		return b;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub

	}

	@Override
	public Handler getHandler() {
		// TODO Auto-generated method stub
		return mHandler;
	}

	@Override
	public void scannerClick() {
		if (!checkViewAvailable())
			return;
		// dView.showMsgToast("扫码", Toast.LENGTH_SHORT);
		if (currentConfig != null) {
			// DialogUtils.showCallBackDialog(this, "提示",
			// "已经存在配置过的设备："+ReadDeviceIDUtil.getDevicId(bContext)+"\n"+
			// "重新配置后，将会删除本地所有数据\n" +
			// "是否确认删除？", back);
			showTipsDialog();
		} else {
			dView.readQRCode();
		}
	}

	@Override
	public void completeClick() {
		if (!checkViewAvailable())
			return;
		dView.showMsgToast("完成", Toast.LENGTH_SHORT);

		String strScanId = dView.getEtText();

		if (StringUtils.isEmpty(strScanId)) {
			dView.showMsgToast("设备ID不能为空", Toast.LENGTH_SHORT);
			return;
		}
		final String encrypt = ReadDeviceIDUtil.readScanDecrypt(strScanId);
		String deviceId = encrypt.split(PhoneScanner.splitStr)[0];

		if (!StringUtils.isNumber(deviceId)) {
			dView.showMsgToast("请输入正确的ID", Toast.LENGTH_SHORT);
			return;
		}
		// 配置成功，删除本地数据
		context.deleteDatabase(dbName);
		bll.insertConfig(new ConfigBean(currentHandsetVersion, strScanId));
		dView.showMsgToast("设置设备id完成", Toast.LENGTH_SHORT);
		dView.finishPage();
	}

	private void showTipsDialog() {
		if (tipsDialog == null) {
			tipsDialog = new TipsDialog(context);
		}
		tipsDialog.show();
		String tipsContent = "已经存在配置过的设备："
				+ ReadDeviceIDUtil.readScanDecrypt(currentConfig.getDeviceId())
				+ "\n\n" + "重新配置后，将会删除本地所有数据\n\n" + "是否确认删除？";
		tipsDialog.setWarnView("警告", tipsContent, false, new DSure() {

			@Override
			public void sureClick() {
				// LogUtil.d(TConfig.Echo, "点击了删除界面的确认按钮：" + tipsContent + "
				// position:" + position); //
				if (checkViewAvailable()) {
					dView.readQRCode();
				}
			}
		});
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case PhoneScanner.BARCODE_ERR_LABEL:
			case PhoneScanner.BARCODE_READ:
				if (!checkViewAvailable())
					return;
//				PhoneCilicoSoundUtils.play();
				dView.vibrate(100);
				String strScanLabel = msg.obj.toString();
				dView.setEtText(strScanLabel);
				dView.setCurrentScanIdText(ReadDeviceIDUtil
						.readScanDecrypt(strScanLabel));
				// etId.setText(strScanLabel);
				// tvScanId.setText(ReadDeviceIDUtil.readScanDecrypt(strScanLabel));
				break;
			case PhoneScanner.BARCODE_NOREAD:

				break;
			}
		};
	};
}
