package cn.net.caoyao.cpzshandset.service.presenter.impl.pack.commonfarmers;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;
import cilico.tools.barcode.PhoneCilicoSoundUtils;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.bean.PackBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.bll.PackBll;
import cn.net.caoyao.cpzshandset.service.presenter.PackConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.PackConfigConstactImpl;
import cn.net.caoyao.cpzshandset.ui.activity.OptionSubListStyleActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog.DSure;

public class CommonFarmersPackConfigPresenter extends PackConfigConstactImpl {
	private final PackConfigConstact.View configView;
	private Context context;
	private PackBean packBean;
//	private SingleLabelBll labelBll;
	private PackBll packBll;
	private HandsetOperation option_type = HandsetOperation.OPERATION_PACK_CONSTANT;
	private boolean isCreate = true;
	private int packId = -1;
	private TipsDialog tipsDialog;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case PhoneScanner.BARCODE_ERR_LABEL:
			case PhoneScanner.BARCODE_READ:
				scannerSuccess(msg.obj.toString());
				break;
			case PhoneScanner.BARCODE_NOREAD:

				break;
			}
		}

	};

	private void scannerSuccess(String str) {
		if (!checkViewAvailable())
			return;
		PhoneCilicoSoundUtils.play();
		
		if (checkPackageIdAviable(str)) {
			configView.setPackageLabel(str);
		}
	};

	private boolean checkPackageIdAviable(String str) {
		if (StringUtils.isEmpty(str) || !StringUtils.isNumber(str)) {
			configView.showMsgToast("不合法的包标签！", Toast.LENGTH_SHORT);
			return false;
		}
		if (!(str.length() == 16 || str.length() == 22)) {
			configView.showMsgToast("不合法的包标签！", Toast.LENGTH_SHORT);
			return false;
		}
//		if (labelBll.hasExistSingleLabel(str, HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption())) {
//			String tips = "抱歉！该标签已经作为子标签存在！不能再作为打包的包标签\n" + str;
//			showIKnowTipsDialog(tips);
//			return false;
//		}
//		if (!isCreate) {
//			if (labelBll.hasExistSingleLabelInAId(str, option_type, packId)) {
//				String tips = "抱歉！该标签已经作为子标签存在！不能再作为打包的包标签\n" + str;
//				showIKnowTipsDialog(tips);
//				return false;
//			}
//		}
		
		return true;
		/*if (labelBll.hasExistSingleLabel(str, option_type)) {
//			configView.showMsgToast("该标签已经的别的打包信息中作为子标签存在！不能再作为打包的包标签", Toast.LENGTH_SHORT);
			String tips = "抱歉！该标签已经的别的打包信息中作为子标签存在！不能再作为打包的包标签\n" + str;
			showIKnowTipsDialog(tips);
			return false;
		} else {
			return true;
		}*/
	}

	public CommonFarmersPackConfigPresenter(PackConfigConstact.View configView, Context context,Intent intent) {
		this.configView = configView;
		this.context = context;
		isCreate = intent.getBooleanExtra("isCreate", true);
		packBll = new PackBll(context);
//		labelBll = new SingleLabelBll(context);
		configView.setPackageLabelVisibility(View.GONE);
		LogUtil.d(TConfig.Echo, "isCreate:" + isCreate);
		if (!isCreate) {
			packId = intent.getIntExtra("currentHisId", -1);
			LogUtil.d(TConfig.Echo, "packId:" +packId);
			packBean = packBll.queryPackById(packId);
			configView.setMoreOptionBtnVisiable(View.VISIBLE);
			configView.setMoreOptionBtnStr("保存");
		}
		start();
	}

	private void showIKnowTipsDialog(String tips) {
		if (tipsDialog == null) {
			tipsDialog = new TipsDialog(context);
		}
		tipsDialog.show();
		tipsDialog.setWarnView("提示", tips, true, null);
	}
	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
		configView.setPageTitle("打包配置");
		configView.setBatchPackBtnVisiable(View.GONE);
		if (!isCreate) {
			initDataView();
		}
	}

	private void initDataView() {
		if (packBean == null) return;
		configView.setDescribe(packBean.getDescribe());
		configView.setPackageLabel(packBean.getPackage_label_id());
		configView.setPreditCount(packBean.getCount()+"");
	}

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

	@Override
	public Handler getHandler() {
		return mHandler;
	}

	

	@Override
	public void backClick() {
		if (!checkViewAvailable())
			return;
		configView.finishPage();
	}

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

	@Override
	public void singlePackClick() {
		if (!checkViewAvailable())
			return;
		
		int predictCount = StringUtils.isEmpty(configView.getPreditCount()) ? 0: Integer.parseInt(configView.getPreditCount());
		if (predictCount <= 0) {
			configView.showMsgToast("抱歉，打包的预设数量必须大于0", Toast.LENGTH_SHORT);
			return;
		}
		if (predictCount > 2000) {
			configView.showMsgToast("抱歉，打包的预设数量不能超过2000", Toast.LENGTH_SHORT);
			return;
		}
		packBean = organziedPackBean();
		if (isCreate) {
			int packId = packBll.insertPack(packBean);
			packBean.setId(packId);
		} else {
			packBll.updatePackById(packBean);
		}
		Intent activateIntent = new Intent(context, OptionSubListStyleActivity.class);
		activateIntent.putExtra("optionMode", option_type);
		activateIntent.putExtra("currentPackBean", packBean);
		context.startActivity(activateIntent);
		configView.finishPage();
//		if (checkPackageIdAviable(configView.getPackageLabel())) {
//			packBean = organziedPackBean();
//			if (isCreate) {
//				int packId = packBll.insertPack(packBean);
//				packBean.setId(packId);
//			} else {
//				packBll.updatePackById(packBean);
//			}
//			Intent intent = new Intent(context, SingleLabelScannerActivity.class);
//			intent.putExtra("optionMode", HandsetOperation.OPERATION_PACK_CONSTANT);
//			intent.putExtra("currentPackBean", packBean);
//			context.startActivity(intent);
//			configView.finishPage();
//		}
		
//		configView.showMsgToast("单标打包", Toast.LENGTH_SHORT);

	}

	private PackBean organziedPackBean() {
		if (packBean == null) {
			packBean = new PackBean();
		}
		String describe = StringUtils.isEmpty(configView.getDescribe()) ? "无": configView.getDescribe();
		packBean.setDescribe(describe);
		int predictCount = StringUtils.isEmpty(configView.getPreditCount()) ? 0: Integer.parseInt(configView.getPreditCount());
		packBean.setCount(predictCount);
//		packBean.setPackage_label_id(configView.getPackageLabel());
		
		return packBean;
	}

	@Override
	public void batchPackClick() {
		if (!checkViewAvailable())
			return;
		configView.showMsgToast("批量打包", Toast.LENGTH_SHORT);

	}
	
	protected void showTipsDialog(String msg) {
		if (tipsDialog == null) {
			tipsDialog = new TipsDialog(context);
		}
		tipsDialog.show();
		tipsDialog.setTipsView("提示", msg, 0, false, new DSure() {
			
			@Override
			public void sureClick() {
				if(!checkViewAvailable()) return;
				packBean = organziedPackBean();
				if (isCreate) {
					int packId = packBll.insertPack(packBean);
					packBean.setId(packId);
				} else {
					packBll.updatePackById(packBean);
				}
				configView.finishPage();
			}
		});
		
	}

	@Override
	public void moreOptionClick() {
		if(!checkViewAvailable()) return;
		
		int predictCount = StringUtils.isEmpty(configView.getPreditCount()) ? 0: Integer.parseInt(configView.getPreditCount());
		if (predictCount <= 0) {
			configView.showMsgToast("抱歉，打包的预设数量必须大于0", Toast.LENGTH_SHORT);
			return;
		}
		if (predictCount > 2000) {
			configView.showMsgToast("抱歉，打包的预设数量不能超过2000", Toast.LENGTH_SHORT);
			return;
		}
		showTipsDialog("是否保存当前修改的信息？");
//		if (checkPackageIdAviable(configView.getPackageLabel())) {
//			showTipsDialog("是否保存当前修改的信息？");
//		}
//		
		
	}

}

