package cn.net.caoyao.cpzshandset.service.presenter.impl.packRelation.bottleBox;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.bean.ActivateBean;
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.service.presenter.ActivateConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.ActivateConfigConstactImpl;
import cn.net.caoyao.cpzshandset.ui.activity.ProductDetailListActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.DateTimePickDialogUtil;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog.DSure;
import cn.net.cpzslibs.prot.handset.bean.Product;

public class JinLineBottleBoxConfigPresenter extends ActivateConfigConstactImpl{

	private final ActivateConfigConstact.View configView;
	private Context context;
	private int choiceProductReqCode = 10001;
	private Product currentProduct;
	private ActivateBean aBean;
	private TipsDialog tipsDialog;
	private HandsetOperation option_type = HandsetOperation.OPERATION_PACK_BOTTLE_GIFTBOX_CONSTANT;
	
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case PhoneScanner.BARCODE_READ:
			case PhoneScanner.BARCODE_ERR_LABEL: // 成功
				scannerRecuess(msg.obj.toString());
				break;
			case PhoneScanner.BARCODE_NOREAD:
				// 数据扫描失败
				if (checkViewAvailable()) {
					configView.showMsgToast("取消扫描", Toast.LENGTH_SHORT);
				}
				break;
			}

		}

	};
	
	public JinLineBottleBoxConfigPresenter(Context context,ActivateConfigConstact.View configView,Intent intent) {
		this.context = context;
		this.configView = configView;
		configView.setMoreOptionBtnStr("确定");
		start();
	}
	
	private void scannerRecuess(String str) {
		if (!checkViewAvailable())
			return;
//		PhoneCilicoSoundUtils.play();
		configView.vibrate(100);
		
		
	}
	


	@Override
	public void start() {
		if (!checkViewAvailable()) return;
		configView.setPageTitle("选择产品");
		configView.setBatchActivateBtnVisiable(View.GONE);
		configView.setSingleActivateBtnVisiable(View.GONE);
		configView.setProductDateVisiable(View.GONE);
		configView.setMoreOptionBtnVisiable(View.VISIBLE);
		configView.setMoreOptionBtnStr("确定");
	}
	
	
	


	@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 onActivityResult(int requestCode, int resultCode, Intent data) {
		LogUtil.d(TConfig.Echo, "requestCode:" + requestCode +"  resultCode:" + resultCode);
		if (!checkViewAvailable()) return;
		if (requestCode == choiceProductReqCode && resultCode == 1001) {
			if (data != null) {
				currentProduct = (Product) data.getSerializableExtra("choicedProduct");
				configView.setPId(currentProduct.getId());
				configView.setPName(currentProduct.getName());
				configView.setPNid(currentProduct.getNid());
			}
			
		}
	}

	@Override
	public void onResume() {
		
	}

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

	@Override
	public void choiceProductClick() {
		if(!checkViewAvailable()) return;
//		configView.showMsgToast("选择产品", Toast.LENGTH_SHORT);
		Intent intent = new Intent(context, ProductDetailListActivity.class);
		configView.startAcForResult(choiceProductReqCode, intent);
		
	}

	@Override
	public void choiceProductDateClick() {
		if(!checkViewAvailable()) return;
//		configView.showMsgToast("选择生产日期", Toast.LENGTH_SHORT);
		String initEndDateTime = "2016年11月17日 13:30";
		DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
                context, initEndDateTime,true);
        dateTimePicKDialog.dateTimePicKDialog(configView.getDateTextView());
		
	}

	@Override
	public void singleActivateClick() {
		if(!checkViewAvailable()) return;
	}





	@Override
	public void batchActivateClick() {
		if(!checkViewAvailable()) return;
		
	}
	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;
				
				/*if (aBean.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
					if (!organizeActivateBean(Config.SINGLE_OPTION_LABEL_TYPE)) {
						activateBll.updateActivatePack(aBean);
						configView.finishPage();
					}
				} else if (aBean.getOption_mode() == Config.BATCH_OPTION_LABEL_TYPE) {
					if (!organizeActivateBean(Config.BATCH_OPTION_LABEL_TYPE)) {
						activateBll.updateActivatePack(aBean);
						configView.finishPage();
					}
				}*/
//				activateBll.updateActivateById(aBean);
				
			}
		});
		
	}

	@Override
	public void moreOptionClick() {
		if (currentProduct == null) {
			configView.showMsgToast("产品信息不能为空！", Toast.LENGTH_SHORT);
			return;
		}
		showTipsDialog("是否保存当前修改的信息？");
		
		
	}



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

}
