package cn.net.caoyao.cpzshandset.service.presenter.impl.bing.replenish.jinLiquor;

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

import com.google.gson.Gson;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.bean.ReplenishBingBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.config.HandsetVersionConstant;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
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.presenter.OptionHisListStyleConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.OptionHisListStyleConstactImpl;
import cn.net.caoyao.cpzshandset.service.thread.ThreadReqTools;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.adapter.ReplenishBingListAdapter;
import cn.net.caoyao.cpzshandset.ui.dialog.OperationDialog;
import cn.net.caoyao.cpzshandset.ui.dialog.OperationDialog.DClick;
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 JinLiquorReplenishBingListPresenter extends OptionHisListStyleConstactImpl {
	private final OptionHisListStyleConstact.View hisView;
	private Context context;
	private OperationDialog operationDialog;
	private TipsDialog tipsDialog;
	private ProgressDialog bProgressDialog = null;
	private int currentHansetVersion = HandsetVersionConstant.HANDSET_JIN_LIQUOR_VERSION_MODE;
	private HandsetOperation option_type = HandsetOperation.OPERATION_REPLENISH_BING_CONSTANT;
	private Gson gson;
	private List<ReplenishBingBean> beans = new ArrayList<>();
	private ReplenishBingListAdapter adapter;

	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()) {
					hisView.showMsgToast("取消扫描", Toast.LENGTH_SHORT);
				}
				break;
			case RequestConstant.REQUEST_PROT01_LOGIN_FAIL:
				if (!checkViewAvailable())
					return;
				dismissProgress();
				hisView.showMsgToast("登录失败！" + "\n错误码：" + msg.arg1 + "\n原因：" + ThreadReqTools.getErrorMsg(msg.arg1),
						Toast.LENGTH_SHORT);
				break;
			case RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT:
				if (!checkViewAvailable())
					return;
				dismissProgress();
				hisView.showMsgToast("连接超时", Toast.LENGTH_SHORT);
				break;
			case RequestConstant.REQUEST_FAIL:
				if (!checkViewAvailable())
					return;
				dismissProgress();
				hisView.showMsgToast("请求失败!", Toast.LENGTH_SHORT);
				break;
			}

		}
	};

	public JinLiquorReplenishBingListPresenter(OptionHisListStyleConstact.View hisView, Context context, Intent intent) {
		super(hisView,context);
		this.hisView = hisView;
		this.context = context;
		
		start();
	}

	public void showReqOrderFailTipsDialog(final String tipsContent) {
		if (tipsDialog == null) {
			tipsDialog = new TipsDialog(context);
		}
		tipsDialog.show();
		tipsDialog.setTipsView(null, tipsContent, 0, true, new DSure() {

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

	@Override
	public void scannerSuccess(String scannerContent) {
		super.scannerSuccess(scannerContent);
		LogUtil.e(TConfig.Echo, "scannerContent:" + scannerContent);
//		hisView.vibrate(100);
		if (StringUtils.isEmpty(scannerContent)) {
			hisView.showMsgToast("未扫描到信息，请确认扫描的二维码是否正确", Toast.LENGTH_SHORT);
			return;
		}
		scannerContent = scannerContent.trim();
		if (!StringUtils.isNumber(scannerContent)) {
			hisView.showMsgToast("扫描到的数据格式不正确，请确认扫描的二维码是否正确", Toast.LENGTH_SHORT);
			return;
		}
		
	}

	@Override
	public void start() {
		if (!checkViewAvailable()) return;
		hisView.setPageTitle("补托");
		hisView.setMoreOptionText("上传");
		hisView.setBottomText("扫码");
		adapter = new ReplenishBingListAdapter(beans);
		hisView.setAdapter(adapter);
		
		addTestData();
	}

	private void addTestData() {
		
		for (int i = 0; i < 10; i++) {
			
			ReplenishBingBean bean = new ReplenishBingBean();
			bean.setPackageLabel("123589654789523" + i);
			int orginalCount = 65 + i* 3;
			if (orginalCount > 80) {
				orginalCount = 80-i;
			}
			bean.setOriginalCount(orginalCount);
			bean.setCanReplenishCount(80-orginalCount);
			
			beans.add(bean);
		}
		
	}

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

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

	@Override
	public void onItemClickListener(int position) {
		if (!checkViewAvailable())
			return;
		if (beans.size() > 0) {
			Intent intent = new Intent(context, SingleLabelScannerActivity.class);
			intent.putExtra("optionMode", option_type);
			intent.putExtra("currentReplenishBing", beans.get(position));
			context.startActivity(intent);
		}
	}

	@Override
	public void onItemLongClickListener(int position) {
		if (!checkViewAvailable())
			return;
		if (beans.size() > 0) {
//			showOptionDialog(position);
		}
	}

	private void showOptionDialog(final int position) {
		if (operationDialog == null) {
			operationDialog = new OperationDialog(context);
		}
		operationDialog.show();
		operationDialog.setDClick(position, new DClick() {

			@Override
			public void modifyClick() {
				// 修改
				if (!checkViewAvailable())
					return;
//				Intent intent = new Intent(context, TruckInvoiceInputStyleConfigActivity.class);
//				intent.putExtra("isCreate", false);
//				intent.putExtra("userInfo", uInfoBean);
//				intent.putExtra("currentHisId", driverInfos.get(position).getId());
//				context.startActivity(intent);
			}

			@Override
			public void delClick() {
				// 删除
				if (!checkViewAvailable())
					return;
				String msg = "本条数据还未上传\n\n是否确定删除该出库数据？\n\n确定删除后\n\n该数据对应的所有信息都会被删除";
				showWarnTipDialog(msg, position);
			}

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

			}
		});

	}

	protected void showWarnTipDialog(String msg, final int position) {
		if (tipsDialog == null) {
			tipsDialog = new TipsDialog(context);
		}
		tipsDialog.show();
		tipsDialog.setDelWarnView("警告", msg, position, false, new DSure() {

			@Override
			public void sureClick() {
//				truckInvoiceBll.deleteTruckInvoiceById(truckInvoices.get(position));
//				truckInvoiceBll.deleteOrderBeanById(driverInfos.get(position).getId());
				onResume();
				hisView.showMsgToast("删除成功！", Toast.LENGTH_SHORT);
			}
		});

	}

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

	}

	@Override
	public void moreOptionClick() {
		if (!checkViewAvailable())
			return;
//		Intent intent = new Intent(context, TruckInvoiceInputStyleConfigActivity.class);
//		intent.putExtra("isCreate", true);
//		intent.putExtra("userInfo", uInfoBean);
//		context.startActivity(intent);
	}

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

	}

	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 void onResume() {
		if (!checkViewAvailable())
			return;
//		driverInfos = driverInfoBll.queryAllDriverInfoDESC();
//		adapter.refreshData(driverInfos);
//		adapter.refeshData(beans);
	}

	@Override
	public void uploadAllHisList() {
		if (!checkViewAvailable())
			return;
		hisView.readQRCode();
	}
	

	private Gson getGson(){
		if (gson == null) {
			gson = new Gson();
		}
		return gson;
	}


}
