package cn.net.caoyao.cpzshandset.service.presenter.impl.labelCheck.giftbox;



import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.config.HandsetVersionConstant;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.presenter.CheckLabelStatusConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.CheckLabelStatusConstactImpl;
import cn.net.caoyao.cpzshandset.ui.adapter.SingleLabelStrAdapter;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog.DSure;
import cn.net.caoyao.cpzshandset.ui.weight.DialogUtils;

public class JinLineGiftBoxCheckLabelPresenter extends CheckLabelStatusConstactImpl implements OnClickListener{
	private final CheckLabelStatusConstact.View cView;
	private Context context;
	private ProgressDialog bProgressDialog = null;
	private int currentHansetVersion = HandsetVersionConstant.HANDSET_JIN_LIQUOR_PRODUCT_LINE_VERSION_MODE;
	private TextWatcher labelT;
	private List<String> labels = new ArrayList<>();
	private SingleLabelStrAdapter adapter;
	private EditText et_check_label_adapter,et_check_code_adapter;
	private TipsDialog tipsDialog;
	private String giftBoxLabel = "1000235456202354";
	private TextView tv_package;
	private TextView tv_sub_label;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case PhoneScanner.BARCODE_READ:
			case PhoneScanner.BARCODE_ERR_LABEL: // 成功
				scannerSuccess(msg.obj.toString());;
				break;
			case PhoneScanner.BARCODE_NOREAD:
				// 数据扫描失败
				if (checkViewAvailable()) {
					cView.showMsgToast("取消扫描", Toast.LENGTH_SHORT);
				}
				break;
			}

		}

	};

	public JinLineGiftBoxCheckLabelPresenter(CheckLabelStatusConstact.View cView, Context context) {
		super(cView);
		this.cView = cView;
		this.context = context;
		start();
	}





	@Override
	public void start() {
		if (!checkViewAvailable()) return;
		cView.setPageTitle("校验盒箱");
		labelT = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if(!checkViewAvailable()) return;
				clearCheckCodeStr();
//				clearResultStr();
			}
		};
//		cView.addLabelTextChangedListener(labelT);
		adapter = new SingleLabelStrAdapter(labels);
		addCustomView();
		cView.setResultAdapter(adapter);
		addTestData();
	}
	
	private void addTestData() {
		for (int i = 0; i < 6; i++) {
			labels.add("100002536547895236547" + i);
		}
		
	}





	private void showWarnTipsDialog(String tipContent){
		if (tipsDialog == null) {
			tipsDialog = new TipsDialog(context);
		}
		tipsDialog.show();
		tipsDialog.setDelWarnView("提示", tipContent, 0, false, new DSure() {
			
			@Override
			public void sureClick() {
				if(!checkViewAvailable()) return;
				cView.finishPage();
			}
		});
	}
	
	private void addCustomView() {
		View checkView = LayoutInflater.from(context).inflate(R.layout.adapter_check_label_header, null);
		cView.addHeaderView(checkView);
		et_check_label_adapter = (EditText) checkView.findViewById(R.id.et_check_label_adapter);
		et_check_code_adapter = (EditText) checkView.findViewById(R.id.et_check_code_adapter);
		et_check_label_adapter.addTextChangedListener(labelT);
		View btn_check_label_scanner_adapter = checkView.findViewById(R.id.btn_check_label_scanner_adapter);
		btn_check_label_scanner_adapter.setOnClickListener(this);
		View btn_check_label_status_adapter = checkView.findViewById(R.id.btn_check_label_status_adapter);
		btn_check_label_status_adapter.setOnClickListener(this);
		
		View lin_check_code_adapter = checkView.findViewById(R.id.lin_check_code_adapter);
		lin_check_code_adapter.setVisibility(View.GONE);
		
		et_check_label_adapter.setText(giftBoxLabel);
		tv_package = (TextView) checkView.findViewById(R.id.tv_package);
		tv_sub_label = (TextView) checkView.findViewById(R.id.tv_sub_label);
		tv_package.setText("箱码：" + giftBoxLabel.replaceAll("\\d{4}(?!$)", "$0  "));
		tv_sub_label.setText("盒码：");
		
		
	}

	protected void clearCheckCodeStr() {
//		cView.setCheckCodeStr("");
		et_check_code_adapter.setText("");
	}

	public void dismissProgress() {
		if (bProgressDialog != null) {
			try {
				DialogUtils.dismissProgressDialog(bProgressDialog);
			} catch (Exception e) {
			}
		}
	}

	/**
	 * 提示正在更新数据
	 */
	protected void showProgressDialog(String titleStr, String tipsMsg) {
		bProgressDialog = DialogUtils.showProgressDialog(bProgressDialog, titleStr, tipsMsg, context);
	}

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

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

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		
	}

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

	@Override
	public void backClick() {
		if(!checkViewAvailable()) return;
		if (labels.size() == 0) {
			cView.finishPage();
			return;
		}
		String tipContent = "返回后，当前界面的数据将不再保存！是否确认返回？";
		showWarnTipsDialog(tipContent);
	}

	public void btnCheckClick() {
//		ConfigBean configBean = configBll
//				.queryConfigByHandsetMode(currentHansetVersion);
//		if (configBean == null) {
//			cView.showMsgToast("当前设备还未配置过手持机id，请先配置手持机的设备id", Toast.LENGTH_SHORT);
//			return;
//		}
//		if (!NetWorkUtils.isNetworkConnected(context)) {
//			DialogUtils.showSetNetDialog(context);
//			return;
//		}
////		String labelStr = cView.getLabelStr();
////		String checkCodeStr = cView.getCheckCodeStr();
//		String labelStr = et_check_label_adapter.getText().toString().trim();
//		String checkCodeStr = et_check_code_adapter.getText().toString().trim();
//		if (!checkLabelStatus(labelStr,checkCodeStr)) {
//			return;
//		}
//		showProgressDialog("提示", "正在请求信息，请稍后……");
//		long deviceId = Long.parseLong(ReadDeviceIDUtil.readScanDecrypt(configBean.getDeviceId()));
//		ThreadPoolManager.getThreadPool().execute(new MedicineXYCheckLabelStatusThread(mHandler, deviceId, new Prot20042ReqBean(labelStr, Integer.parseInt(checkCodeStr)),checkCodeStr));
	}

	private boolean checkLabelStatus(String labelStr, String checkCodeStr) {
		if (StringUtils.isEmpty(labelStr)) {
			cView.showMsgToast("标签不能为空", Toast.LENGTH_SHORT);
			return false;
		}
		if (labelStr.length() != 16|| (!StringUtils.isNumber(labelStr))) {
			cView.showMsgToast("您所扫描的不是中国追溯的合法标签", Toast.LENGTH_SHORT);
			return false;
		}
		if (StringUtils.isEmpty(checkCodeStr)) {
			cView.showMsgToast("验证码不能为空", Toast.LENGTH_SHORT);
			return false;
		}
		if (checkCodeStr.length() != 4) {
			cView.showMsgToast("请输入正确的四位验证码", Toast.LENGTH_SHORT);
			return false;
		}
		return true;
	}

	@Override
	public void scannerSuccess(String scannerContentStr) {
		super.scannerSuccess(scannerContentStr);
		
		et_check_label_adapter.removeTextChangedListener(labelT);
		et_check_label_adapter.setText(scannerContentStr);
		if (scannerContentStr.length() != 16|| (!StringUtils.isNumber(scannerContentStr))) {
			cView.showMsgToast("您所扫描的不是中国追溯的合法标签", Toast.LENGTH_SHORT);
		}
		clearCheckCodeStr();
		et_check_label_adapter.addTextChangedListener(labelT);
		
	}
	@Override
	public void onBackPressed() {
		if(!checkViewAvailable()) return;
		backClick();
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_check_label_status_adapter:
			if(!checkViewAvailable()) return;
			btnCheckClick();
			break;

		case R.id.btn_check_label_scanner_adapter:
			if(!checkViewAvailable()) return;
			cView.readQRCode();
			break;
		}
		
	}


}
