package cn.net.caoyao.cpzshandset.service.presenter.impl.activate.commonEgg;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.ActivateBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
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.ActivateBll;
import cn.net.caoyao.cpzshandset.service.bll.ProductBll;
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.BatchLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.activity.ProductDetailListActivity;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;
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 CommonEggActivateConfigPresenter extends ActivateConfigConstactImpl{

	private final ActivateConfigConstact.View configView;
	private Context context;
	private EditText et_activate_config_custom_common_batch_no,et_activate_config_custom_common_producer; 
	private int choiceProductReqCode = 10001;
	private Product currentProduct;
	private ActivateBean aBean;
	private ActivateBll activateBll;
	private ProductBll productBll;
	private boolean isCreate = true;
	private int activateId = -1;
	private TipsDialog tipsDialog;
	
	
	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 CommonEggActivateConfigPresenter(Context context,ActivateConfigConstact.View configView,Intent intent) {
		this.context = context;
		this.configView = configView;
		isCreate = intent.getBooleanExtra("isCreate", true);
		LogUtil.d(TConfig.Echo, "isCreate:" + isCreate);
		if (!isCreate) {
			activateId = intent.getIntExtra("currentHisId", -1);
			configView.setMoreOptionBtnVisiable(View.VISIBLE);
			configView.setMoreOptionBtnStr("保存");
		}
		start();
	}
	
	
	private void initCustomView(View v) {
		et_activate_config_custom_common_batch_no = (EditText) v.findViewById(R.id.et_activate_config_custom_common_batch_no);
		et_activate_config_custom_common_producer = (EditText) v.findViewById(R.id.et_activate_config_custom_common_producer);
		
	}


	@Override
	public void start() {
		if (!checkViewAvailable()) return;
		activateBll = new ActivateBll(context);
		configView.setPageTitle(context.getResources().getString(R.string.activate_config_title_str));
		View customConfigView = LayoutInflater.from(context).inflate(R.layout.common_activate_config_custom_loyout, null);
		configView.addCustomView(customConfigView);
		initCustomView(customConfigView);
		initDataView();
	}
	
	private void initDataView() {
			
			aBean = activateBll.queryActivateBeanById(activateId);
			if (aBean == null) return;
//			LogUtil.d(TConfig.Echo, "aBean:"+aBean.toString());
			if (aBean.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
				configView.setBatchActivateBtnVisiable(View.GONE);
			} else if (aBean.getOption_mode() == Config.BATCH_OPTION_LABEL_TYPE) {
				configView.setSingleActivateBtnVisiable(View.GONE);
			}
			et_activate_config_custom_common_batch_no.setText(aBean.getProduct_batch_number());
			if (!StringUtils.isEmpty(aBean.getProducer())) et_activate_config_custom_common_producer.setText(aBean.getProducer());
			if(!StringUtils.isEmpty(aBean.getProduct_date())) configView.setProductDate(aBean.getProduct_date());
			currentProduct = getProductBll().queryProductById(aBean.getProduct_id());
			if (currentProduct == null) return;
			configView.setPId(currentProduct.getId());
			if(!StringUtils.isEmpty(currentProduct.getName())) configView.setPName(currentProduct.getName());
			if(!StringUtils.isEmpty(currentProduct.getNid())) configView.setPNid(currentProduct.getNid());
				
	}
	private ProductBll getProductBll() {
		if (productBll == null) {
			productBll = new ProductBll(context);
		}
		return productBll;
	}


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

	@Override
	public Handler getHandler() {
        Log.d("getHandler","mHandler=="+mHandler);
		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 = "2017年8月21日 14:30";
		DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
                context, initEndDateTime,false);
        dateTimePicKDialog.dateTimePicKDialog(configView.getDateTextView());
		
	}

	@Override
	public void singleActivateClick() {
		if(!checkViewAvailable()) return;
		if (currentProduct == null) {
			configView.showMsgToast("产品信息不能为空！", Toast.LENGTH_SHORT);
			return;
		}
		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
//		intent.putExtra("optionMode", HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption());
		intent.putExtra("optionMode", HandsetOperation.OPERATION_ACTIVATE_CONSTANT);
		organizeActivateBean(Config.SINGLE_OPTION_LABEL_TYPE);
		
		if (isCreate) {
			int activateId = activateBll.insertActivate(aBean);
			aBean.setId(activateId);;
		} else {
			activateBll.updateActivateById(aBean);
		}
		
		intent.putExtra("currentActivateBean", aBean);
		
		context.startActivity(intent);
		configView.finishPage();
	}

	private void organizeActivateBean(int option_mode) {
		if (aBean == null) {
			aBean = new ActivateBean();
		}
		aBean.setProduct_id(currentProduct.getId());
		aBean.setOption_mode(option_mode);
		aBean.setProduct_date(configView.getProductDate());
		String batchNo = et_activate_config_custom_common_batch_no.getText().toString().trim();
		aBean.setProduct_batch_number(batchNo);
		aBean.setProducer(et_activate_config_custom_common_producer.getText().toString().trim());
	}


	@Override
	public void batchActivateClick() {
		if(!checkViewAvailable()) return;
		if (currentProduct == null) {
			configView.showMsgToast("产品信息不能为空！", Toast.LENGTH_SHORT);
			return;
		}
		Intent intent = new Intent(context, BatchLabelScannerActivity.class);
//		intent.putExtra("optionMode", HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption());
		intent.putExtra("optionMode", HandsetOperation.OPERATION_ACTIVATE_CONSTANT);
		organizeActivateBean(Config.BATCH_OPTION_LABEL_TYPE);
		
		if (isCreate) {
			int activateId = activateBll.insertActivate(aBean);
			aBean.setId(activateId);
		} else {
			activateBll.updateActivateById(aBean);
		}
		
		
		/*if (isCreate) {
			int activateId = activateBll.insertActivate(aBean);
			aBean.setId(activateId);;
		} else {
			activateBll.updateActivateById(aBean);
		}*/
		intent.putExtra("currentActivateBean", aBean);
		context.startActivity(intent);
		configView.finishPage();	
	}
	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) {
					organizeActivateBean(Config.SINGLE_OPTION_LABEL_TYPE);
				} else if (aBean.getOption_mode() == Config.BATCH_OPTION_LABEL_TYPE) {
					organizeActivateBean(Config.BATCH_OPTION_LABEL_TYPE);
				}
				activateBll.updateActivateById(aBean);
				configView.finishPage();
			}
		});
		
	}

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


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

}
