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

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
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.bitmap.MBitmapService;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.config.SDCardConfig;
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.ProductDetailGvActivity;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.DateTimePickDialogUtil;
import cn.net.cpzslibs.prot.handset.bean.Product;

public class YthImageActivateConfigPresenter extends ActivateConfigConstactImpl {

	private final ActivateConfigConstact.View configView;
	private Context context;
	private int choiceProductReqCode = 10001;
	private Product currentProduct;
	private ActivateBean aBean;
	private ActivateBll activateBll;
	private ProductBll productBll;
	private EditText et_activate_config_custom_common_batch_no, et_activate_config_custom_common_producer,
			et_activate_config_custom_group_number;
	private ImageView image_activate_config_custom_common_p_image;
	private MBitmapService bitmapService;
	private Bitmap loadingBitmap;
	private boolean isCreate = true;
	private int activateId = -1;

	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 YthImageActivateConfigPresenter(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);
		}
		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);
//		View lin_group_number = v.findViewById(R.id.lin_group_number);
//		lin_group_number.setVisibility(View.VISIBLE);
		View lin_activate_config_custom_common_producer = v.findViewById(R.id.lin_activate_config_custom_common_producer);
		lin_activate_config_custom_common_producer.setVisibility(View.GONE);
		View lin_p_image = v.findViewById(R.id.lin_p_image);
		lin_p_image.setVisibility(View.VISIBLE);
		et_activate_config_custom_group_number = (EditText) v.findViewById(R.id.et_activate_config_custom_group_number);
		image_activate_config_custom_common_p_image = (ImageView) v
				.findViewById(R.id.image_activate_config_custom_common_p_image);
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
		configView.setBatchActivateBtnVisiable(View.GONE);
//		configView.setSingleActivateBtnVisiable(View.GONE);
		loadingBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.product_image_bg_default);
		bitmapService = MBitmapService.create(MBitmapService.getInstance(context), context,
				SDCardConfig.getFileCachePath(context));
		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;
			et_activate_config_custom_common_batch_no.setText(aBean.getProduct_batch_number());
			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());
			image_activate_config_custom_common_p_image.setImageBitmap(loadingBitmap);
			if(!StringUtils.isEmpty(currentProduct.getImage())) bitmapService.display(image_activate_config_custom_common_p_image, currentProduct.getImage(), loadingBitmap, loadingBitmap);
	}

	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() {
		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());
				image_activate_config_custom_common_p_image.setImageBitmap(loadingBitmap);
				if(!StringUtils.isEmpty(currentProduct.getImage()))bitmapService.display(image_activate_config_custom_common_p_image, currentProduct.getImage(), loadingBitmap);
			}

		}
	}

	@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, ProductDetailGvActivity.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;
		if (currentProduct == null) {
			configView.showMsgToast("产品信息不能为空！", Toast.LENGTH_SHORT);
			return;
		}
		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
		intent.putExtra("optionMode", HandsetOperation.OPERATION_ACTIVATE_CONSTANT);
		organizeActivateBean(Config.SINGLE_OPTION_LABEL_TYPE);
//		int activateId = activateBll.insertActivate(aBean);
//		aBean.setId(activateId);
		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());
//		String group_number = et_activate_config_custom_group_number.getText().toString().trim();
//		aBean.setGroup_number(group_number);
	}

	@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);
		organizeActivateBean(Config.BATCH_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();
	}

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

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

}
