package cn.net.caoyao.cpzshandset.service.presenter.impl.shipment.truck.ythPunit;

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.ConfigBean;
import cn.net.caoyao.cpzshandset.bean.DriverInfoBean;
import cn.net.caoyao.cpzshandset.bean.ShipmentProductBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
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.bll.DriverInfoBll;
import cn.net.caoyao.cpzshandset.service.bll.InvoiceBll;
import cn.net.caoyao.cpzshandset.service.bll.ShipmentProductBll;
import cn.net.caoyao.cpzshandset.service.bll.ShipmentUploadStatusBll;
import cn.net.caoyao.cpzshandset.service.bll.config.ConfigBll;
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.ShipmentSingleScannerActivity;
import cn.net.caoyao.cpzshandset.ui.adapter.ShipmentProductListAdapter;
import cn.net.caoyao.cpzshandset.ui.dialog.OperationDialog;
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;
import cn.net.cpzslibs.prot.handset.bean.res.ResponsibleUserInfoBean;

public class YthPunitTruckShipmentSubListPresenter extends OptionHisListStyleConstactImpl {
	private final OptionHisListStyleConstact.View hisView;
	private Context context;
	private ShipmentProductListAdapter adapter;
	private List<ShipmentProductBean> shipmentProducts = new ArrayList<ShipmentProductBean>();
	private DriverInfoBean currentDriverInfoBean;
	private DriverInfoBll driverInfoBll;
	private ShipmentProductBll shipmentProductBll;
	private ConfigBll configBll;
	private ShipmentUploadStatusBll uploadStatusBll;
	private InvoiceBll invoiceBll;
	private TipsDialog tipsDialog;
	private OperationDialog operationDialog;
	private ProgressDialog bProgressDialog = null;
	private int currentHansetVersion = HandsetVersionConstant.HANDSET_YTH_SCANNER_SHIPMENT_PUNIT_VERSION_MODE;
	private HandsetOperation option_type = HandsetOperation.OPERATION_TRUCK_SHIPMENT_CONSTANT;
	private ResponsibleUserInfoBean userInfoBean;
	
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			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;
			}
		};
	};

	public YthPunitTruckShipmentSubListPresenter(Context context, OptionHisListStyleConstact.View hisView,Intent intent) {
		super(hisView, context);
		this.hisView = hisView;
		this.context = context;
		init();
//		intent.putExtra("transId", resBean.getDriver().getId());
//		intent.putExtra("userInfo", uInfoBean);
		userInfoBean = (ResponsibleUserInfoBean) intent.getSerializableExtra("userInfo");
		long driver_id = intent.getLongExtra("transId", 0);
		currentDriverInfoBean = driverInfoBll.queryDriverInfoById(driver_id);
		start();
	}

	private void init() {
		driverInfoBll = new DriverInfoBll(context);
		shipmentProductBll = new ShipmentProductBll(context);
		invoiceBll = new InvoiceBll(context);
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
//		bll = new ActivateWithPackBll(context);
		configBll = new ConfigBll(context);
		hisView.setPageTitle("选择产品");
//		hisView.setMoreOptionText("上传");
		hisView.setMoreOptionVisibility(View.GONE);
		hisView.setBottomBtnVisibility(View.GONE);
		adapter = new ShipmentProductListAdapter(shipmentProducts,invoiceBll);
		hisView.setAdapter(adapter);
	}

	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);
	}

	private ShipmentUploadStatusBll getUploadStatusBll() {
		if (uploadStatusBll == null) {
			uploadStatusBll = new ShipmentUploadStatusBll(context);
		}
		return uploadStatusBll;
	}



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

	@Override
	public void onItemClickListener(int position) {
		if (!checkViewAvailable())
			return;
		if (shipmentProducts.size() > 0) {
			ShipmentProductBean b = shipmentProducts.get(position);
			
			if (b.getNative_status() == Config.SUBMIT_STATUS_SUBMIT_SUCCESS) {
				String tips = "该货物已经提交过发货信息，是否继续在该货物下进行发货";
				showWarnTipDialog(tips, position);
			} else {
				Intent intent = new Intent(context, ShipmentSingleScannerActivity.class);
				// intent.putExtra("shipmentMode", 2);
				intent.putExtra("transId", b.getId());
				intent.putExtra("userInfo", userInfoBean);
				context.startActivity(intent);
			}
			
			
		}
		
		
		// ActivateBean a = shipmentProducts.get(position);
		// LogUtil.d(TConfig.Echo,position + "position:" + a.getProduct_date());
	}

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

	}

	


	protected void showWarnTipDialog(String msg, final int position) {
		if (tipsDialog == null) {
			tipsDialog = new TipsDialog(context);
		}
		tipsDialog.show();
		tipsDialog.setWarnView("提示", msg, false, new DSure() {
			
			@Override
			public void sureClick() {
				ShipmentProductBean b = shipmentProducts.get(position);
				Intent intent = new Intent(context, ShipmentSingleScannerActivity.class);
				// intent.putExtra("shipmentMode", 2);
				intent.putExtra("transId", b.getId());
				intent.putExtra("userInfo", userInfoBean);
				context.startActivity(intent);
				
			}
		});

	}


	@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;
		}
	}

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

	}

	@Override
	public void onResume() {
		
		shipmentProducts = shipmentProductBll.queryShipmentProductByDriverIdAndRepertoryId(currentDriverInfoBean.getId(),userInfoBean.getRepertory_id());
//		shipmentProducts.get(0).setNative_status(Config.SUBMIT_STATUS_SUBMIT_SUCCESS);
//		shipmentProducts.get(1).setNative_status(Config.SUBMIT_STATUS_SUBMIT_SUCCESS);
//		shipmentProducts.get(2).setNative_status(Config.SUBMIT_STATUS_SUBMIT_FAIL);
		adapter.refreshData(shipmentProducts);
	}

	@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", currentDriverInfoBean);
//		intent.putExtra("package_id", -1);
//		context.startActivity(intent);
//		
	}


}
