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

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
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.ConfigCreateService;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.util.CacheGet;
import cn.net.caoyao.cpzshandset.db.dal.kia.KIAActivateDal;
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.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog;
import cn.net.cpzslibs.prot.handset.bean.Product;

public class KIADanActivateConfigPresenter 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 Product currentProduct;
	private ActivateBean aBean;
	private KIAActivateDal activateBll;
	
	private boolean isCreate = true;
	
	private TipsDialog tipsDialog;
	public ConfigCreateService createService;
	public CacheGet cacheG;
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			
			case PhoneScanner.BARCODE_READ:
			case PhoneScanner.BARCODE_ERR_LABEL: // 成功
				//Log.d("--==msg.obj.toString()","--==msg.obj.toString()"+msg.obj.toString());
				configView.setPId("");
				configView.setPId(msg.obj.toString());
				break;
			case PhoneScanner.BARCODE_NOREAD:
				// 数据扫描失败
				if (checkViewAvailable()) {
					configView.showMsgToast("取消扫描", Toast.LENGTH_SHORT);
				}
				break;
			}

		}
	};
	public KIADanActivateConfigPresenter(Context context,ActivateConfigConstact.View configView,Intent intent) {
		this.context = context;
		this.configView = configView;
		
		createService = new ConfigCreateService();
		cacheG = new CacheGet();
		start();
	}
	
	
	


	@Override
	public void start() {
		if (!checkViewAvailable()) return;
		activateBll = new KIAActivateDal(context);
		configView.setPageTitle(context.getResources().getString(R.string.activate_config_title_str));
		configView.setProductCodeVisiable(View.GONE);
		configView.setProductNameVisiable(View.GONE);
		configView.setProductDateVisiable(View.GONE);
		configView.setBatchActivateBtnVisiable(View.GONE);
		configView.setEmplateChoiceBtnStr("扫描配件");
		configView.setSetProductIdStr("配件号 ：");
		initDataView();
	}
	
	private void initDataView() {
	
				
	}
	


	@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) {
		
	}

	@Override
	public void onResume() {
		
	}

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

	@Override
	public void choiceProductClick() {//去选择
		if(!checkViewAvailable()) return;
		PhoneScanner.Read(createService, cacheG, context);	
		
	}

	@Override
	public void choiceProductDateClick() {
		
	}

	@Override
	public void singleActivateClick() {//单标激活
		if(!checkViewAvailable()) return;
		String str  = configView.getPId();
		if ("".equals(str)||null==str) {
			configView.showMsgToast("请扫描配型编号！", Toast.LENGTH_SHORT);
			return;
		}

		if(str.length()>19||str.length()<5){ 
			configView.showMsgToast("配件号需要5到19位之间", Toast.LENGTH_SHORT);
			return;
		}
		
		if(str.length()>=6&&str.startsWith("010694")){
			configView.showMsgToast("请不要扫描上面的一维码", Toast.LENGTH_SHORT);
			return;
		}
		if(activateBll.hasExistKIAProduct(str)){
			configView.showMsgToast("该配件已经存在,不可重复添加！", Toast.LENGTH_SHORT);
			return;
		};
		
		/*if(!checkInput(str)){
			configView.showMsgToast("配件号只能是数字和字母", Toast.LENGTH_SHORT);
			return;
		}*/
		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
		
		intent.putExtra("optionMode", HandsetOperation.OPERATION_DAN_ACTIVATE_CONSTANT);
		
		organizeActivateBean(30,str);
		
		if (isCreate) {//新建插入
			int activateId = activateBll.insertActivate(aBean);
			aBean.setId(activateId);
			
		}
		
		intent.putExtra("currentActivateBean", aBean);
		
		context.startActivity(intent);
		configView.finishPage();
	}
	
	private void organizeActivateBean(int option_mode,String strProductCode) {
		if (aBean == null) {
			aBean = new ActivateBean();
		}
		aBean.setOption_mode(option_mode);//0 单标激活
		aBean.setProduct_id(strProductCode);
		

	}


	@Override
	public void batchActivateClick() {
		
	}
	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
		
	}
	/**
	 * 只可以字母和数字
	 * @param input
	 * @return
	 */
	private boolean checkInput(String input){  
	    Pattern pattern = Pattern.compile("^[A-Za-z0-9]+$");   
	    Matcher m = pattern.matcher(input);  
	    if( !m.matches() ){ //匹配不到,說明輸入的不符合條件  
	       return false;   
	    }   
	    return true;   
	}
}
