package cn.net.caoyao.cpzshandset.service.presenter.impl.packRelation.bottleBox;

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.view.View;
import android.widget.Toast;
import cn.net.caoyao.cpzshandset.bean.ActivateBean;
import cn.net.caoyao.cpzshandset.bean.PackPackageInfoBean;
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.NetWorkUtils;
import cn.net.caoyao.cpzshandset.service.presenter.OptionHisListStyleConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.OptionHisListStyleConstactImpl;
import cn.net.caoyao.cpzshandset.ui.activity.ActivateConfigActivity;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.adapter.BottleBoxListAdapter;
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 JinLineBottleBoxSubListPresenter extends OptionHisListStyleConstactImpl {
	private final OptionHisListStyleConstact.View hisView;
	private Context context;
	private BottleBoxListAdapter adapter;
	private List<PackPackageInfoBean> packBeans = new ArrayList<PackPackageInfoBean>();
	private int createActivateConfigReqCode = 1001;
	private int modifyActivateConfigReqCode = 1002;
//	private ActivateWithPackBll bll;
	private ActivateBean currentActivateBean;
	private TipsDialog tipsDialog;
	private OperationDialog operationDialog;
	private ProgressDialog bProgressDialog = null;
	private int currentHansetVersion = HandsetVersionConstant.HANDSET_JIN_LIQUOR_PRODUCT_LINE_VERSION_MODE;
	private HandsetOperation option_type = HandsetOperation.OPERATION_PACK_BOTTLE_GIFTBOX_CONSTANT;
	private int option_id = -1;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case RequestConstant.SUBMIT_ALL_ACTIVATE_COMPLETE:
				dismissProgress();
				onResume();
				break;
			case RequestConstant.REQUEST_FAIL:
				// 请求失败
				if (!checkViewAvailable())
					return;
				dismissProgress();
				hisView.showMsgToast("请求失败！", Toast.LENGTH_SHORT);
				break;
			case RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT:
				// 超时
				if (!checkViewAvailable())
					return;
				dismissProgress();
				hisView.showMsgToast("请求超时！", Toast.LENGTH_SHORT);
				break;
			case RequestConstant.REQUEST_PROT01_LOGIN_FAIL:
				// 登录失败
				if (!checkViewAvailable())
					return;
				dismissProgress();
				String errorMsg = msg.obj.toString();
				int errorCode = msg.arg1;
				hisView.showMsgToast(errorMsg + errorCode, Toast.LENGTH_SHORT);
				break;
			case RequestConstant.PROT_REQ_CHECK_LABEL_SUCCESS:
				if (!checkViewAvailable())
					return;
				dismissProgress();
				hisView.showMsgToast("检测标签信息成功，请在扫码界面中查看详细信息", Toast.LENGTH_SHORT);
				break;
			case RequestConstant.PROT_REQ_CHECK_LABEL_FAIL:
				if (!checkViewAvailable())
					return;
				dismissProgress();
				hisView.showMsgToast("检测标签信息失败", Toast.LENGTH_SHORT);
				break;
			}
		};
	};

	public JinLineBottleBoxSubListPresenter(Context context, OptionHisListStyleConstact.View hisView,Intent intent) {
		super(hisView, context);
		this.hisView = hisView;
		this.context = context;
		currentActivateBean = (ActivateBean) intent.getSerializableExtra("currentActivateBean");
		option_id = currentActivateBean.getId();
		start();
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
		hisView.setPageTitle("瓶盒关联");
		hisView.setMoreOptionText("上传");
		hisView.setBottomText("去关联");
		adapter = new BottleBoxListAdapter(packBeans,true);
		hisView.setAdapter(adapter);
		
		addTestData();
	}

	private void addTestData() {
		for (int i = 0; i < 10; i++) {
			PackPackageInfoBean b = new PackPackageInfoBean();
			b.setBottle_label("4568525478963" + i);
			b.setBox_label("152354789562456545614" + i);
			packBeans.add(b);
		}
		adapter.refreshData(packBeans);
		
	}

	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 (hisView != null && context != null) {
			b = true;
		}
		return b;
	}

	@Override
	public void onItemClickListener(int position) {
		if (packBeans.size() == 0) {
			return;
		}
		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
		intent.putExtra("optionMode", option_type);
		intent.putExtra("option_id", option_id);
		intent.putExtra("currentActivate", currentActivateBean);
		intent.putExtra("package_id", packBeans.get(position).getId());
		context.startActivity(intent);
		
		
		// ActivateBean a = packBeans.get(position);
		// LogUtil.d(TConfig.Echo,position + "position:" + a.getProduct_date());
	}

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

	}

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

			@Override
			public void modifyClick() {
				// 修改
				if (!checkViewAvailable())
					return;
				Intent intent = new Intent(context, ActivateConfigActivity.class);
				intent.putExtra("isCreate", false);
				intent.putExtra("optionMode", option_type.getOption());
				intent.putExtra("currentHisId", packBeans.get(position).getId());
				hisView.startAcForResult(createActivateConfigReqCode, intent);
			}

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

			@Override
			public void searckClick() {
				if (!checkViewAvailable())
					return;
				if (!NetWorkUtils.isNetworkConnected(context)) {
					DialogUtils.showSetNetDialog(context);
					return;
				}
//				ConfigBean configBean = configBll.queryConfigByHandsetMode(currentHansetVersion);
//				if (configBean == null) {
//					hisView.showMsgToast("当前设备还未配置过手持机id，请先配置手持机的设备id", Toast.LENGTH_SHORT);
//					return;
//				}
//				if (packBeans == null || packBeans.size() == 0) {
//					hisView.showMsgToast("抱歉，当前的激活打包数据中还没有标签，不可查询", Toast.LENGTH_SHORT);
//					return;
//				}
//
//				List<String> labels = getSubLabelBll().queryAllSubLabelStrByPackageId(packBeans.get(position).getId(),option_id,option_type.getOption());
//				if (labels == null) {
//					hisView.showMsgToast("抱歉，当前的激活打包数据中还没有标签，不可查询", Toast.LENGTH_SHORT);
//					return;
//				}
//
//				if (labels.size() == 0) {
//					hisView.showMsgToast("抱歉，当前的激活打包数据中还没有标签，不可查询", Toast.LENGTH_SHORT);
//					return;
//				}
//				showProgressDialog("提示", "正在检测标签信息，请稍后……");
//				long deviceId = Long.parseLong(ReadDeviceIDUtil.readScanDecrypt(configBean.getDeviceId()));
//				ThreadPoolManager.getThreadPool().execute(new CommonCheckPackLabelThread(mHandler, deviceId,packBeans.get(position).getId(), option_type,
//						currentActivateBean.getId(), labels, getSubLabelBll()));
			}
		});

	}


	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() {
				// bll.delActivatePackById(packBeans.get(position));
				// packBeans = bll.queryAllActivatePackList();
				// adapter.refreshActivateHisData(packBeans);
//				packageBll.deletePackPackageById(packBeans.get(position).getId());
//				packBeans = packageBll.queryAllPackPackageDESC(option_id, option_type.getOption());
//				adapter.refreshData(packBeans);
				hisView.showMsgToast("删除成功！", Toast.LENGTH_SHORT);
			}
		});

	}


	@Override
	public void backBtnClick() {
		if (!checkViewAvailable())
			return;
		hisView.finishPage();
	}

	@Override
	public void moreOptionClick() {
		if (!checkViewAvailable())
			return;
//		Intent intent = new Intent(context, ActivateConfigActivity.class);
//		intent.putExtra("isCreate", true);
//		intent.putExtra("optionMode", option_type.getOption());
//		hisView.startAcForResult(createActivateConfigReqCode, intent);
		// hisView.showMsgToast("新建", Toast.LENGTH_SHORT);
		if (!NetWorkUtils.isNetworkConnected(context)) {
			DialogUtils.showSetNetDialog(context);
			return;
		}
//		ConfigBean configBean = configBll.queryConfigByHandsetMode(currentHansetVersion);
//		if (configBean == null) {
//			hisView.showMsgToast("当前设备还未配置过手持机id，请先配置手持机的设备id", Toast.LENGTH_SHORT);
//			return;
//		}
//		int count = getSubLabelBll().queryLabelCountByOptionId(currentActivateBean.getId(), option_type.getOption());
//		if (packBeans == null || packBeans.size() == 0 || count <=0) {
//			hisView.showMsgToast("抱歉，当前的激活打包数据中都没有标签，不可上传", Toast.LENGTH_SHORT);
//			return;
//		}
//
//		showProgressDialog("提示", "正在提交激活打包信息，请稍后……");
//		long deviceId = Long.parseLong(ReadDeviceIDUtil.readScanDecrypt(configBean.getDeviceId()));
//		ThreadPoolManager.getThreadPool().execute(new LiqueurSubmitSingleActivateWithPackThread(mHandler, deviceId, packageBll,
//				getSubLabelBll(), getUploadStatusBll(),currentActivateBean));
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		LogUtil.d(TConfig.Echo, "onActivityResult" + " requestCode:" + requestCode + "  resultCode:" + resultCode);

	}

	@Override
	public void onResume() {
//		packBeans = bll.queryAllActivatePackList();
		// LogUtil.e(TConfig.Echo, "packBeans:" + packBeans.toString());
//		adapter.refreshActivateHisData(packBeans);
//		packBeans = packageBll.queryAllPackPackageDESC(option_id, option_type.getOption());
		adapter.refreshData(packBeans);

	}

	@Override
	public void uploadAllHisList() {
		if (!checkViewAvailable())
			return;
		
		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
		intent.putExtra("optionMode", option_type);
		intent.putExtra("option_id", option_id);
		intent.putExtra("currentActivate", currentActivateBean);
		intent.putExtra("package_id", -1);
		context.startActivity(intent);
//		
//		if (!NetWorkUtils.isNetworkConnected(context)) {
//			DialogUtils.showSetNetDialog(context);
//			return;
//		}
//		ConfigBean configBean = configBll.queryConfigByHandsetMode(currentHansetVersion);
//		if (configBean == null) {
//			hisView.showMsgToast("当前设备还未配置过手持机id，请先配置手持机的设备id", Toast.LENGTH_SHORT);
//			return;
//		}
//		if (packBeans == null || packBeans.size() == 0) {
//			hisView.showMsgToast("抱歉，当前的激活打包数据中都没有标签，不可上传", Toast.LENGTH_SHORT);
//			return;
//		}
//
//		List<PackPackageInfoBean> valid = getValidActivate();
//		if (valid == null || valid.size() == 0) {
//			hisView.showMsgToast("抱歉，当前的激活打包数据中都没有标签，不可上传", Toast.LENGTH_SHORT);
//			return;
//		}
//		showProgressDialog("提示", "正在提交激活打包信息，请稍后……");
//		long deviceId = Long.parseLong(ReadDeviceIDUtil.readScanDecrypt(configBean.getDeviceId()));
//		ThreadPoolManager.getThreadPool().execute(new CommonSubmitAllActivateWithPackThread(mHandler, deviceId, bll,
//				getSingelLabelBll(), getBatchLabelBll(), getUploadStatusBll(), valid));
	}


}
