package cn.net.caoyao.cpzshandset.service.thread.impl.sheepEx;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.List;

import android.os.Handler;
import android.os.Message;
import cn.net.caoyao.cpzshandset.bean.ExchangeCargoBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
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.service.bll.ExchangeCargoBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.bean.res.ProtStatusResBean;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20002ReqBatchBean;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20029ReqBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20029SheepExchangeCargo;

public class SheepExSubmitCargo implements Runnable {
	private Handler mHandler;
	private long deveicedId; 
	private ExchangeCargoBean cargoBean;
	private ExchangeCargoBll cargoBll;
	private List<String> labels;
	private Prot20002ReqBatchBean batchBean;
	public SheepExSubmitCargo(Handler mHandler, long deveicedId,ExchangeCargoBll cargoBll,ExchangeCargoBean cargoBean,List<String> labels,Prot20002ReqBatchBean batchBean) {
		this.mHandler = mHandler;
		this.deveicedId = deveicedId;
		this.cargoBll = cargoBll;
		this.cargoBean = cargoBean;
		this.labels = labels;
		this.batchBean = batchBean;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, String.valueOf(deveicedId));
		try {
			create.createLoginCpzsHandset(deveicedId);
			if (create.isResult()) {
				submitCargo(create);
			} else {
//				mHandler.sendEmptyMessage(ThreadConstant.)
				Message loginFailMsg = mHandler.obtainMessage();
				loginFailMsg.obj = create.getErrMsg();
				loginFailMsg.arg1 = create.getiErrCode();
				loginFailMsg.what = RequestConstant.REQUEST_PROT01_LOGIN_FAIL;
				mHandler.sendMessage(loginFailMsg);
			}
		}catch (SocketTimeoutException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
		} catch (IOException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} finally{
			new Prot02Logout().dealLogout(create);
		}
	}

	private void submitCargo(SocketCreate create) throws IOException,SocketTimeoutException{
		Prot20029SheepExchangeCargo cargo = new Prot20029SheepExchangeCargo();
		cargo.submitExchangeCargo(create, getProt20029ReqBean());
		ProtStatusResBean resBean = cargo.getResBean();
		if (resBean == null) return;
		if (resBean.getResult() == 0) {
			cargoBll.deleteExchangeCargoById(cargoBean);
			mHandler.sendEmptyMessage(RequestConstant.PROT_SUBMIT_EXCHANGE_CARGO_20029_SUCCESS);
		} else {
			cargoBll.updateExchangeCargoStatusById(Config.SUBMIT_STATUS_SUBMIT_FAIL, resBean.getErrcode(), cargoBean.getId());
//			Message msg = mHandler.obtainMessage(RequestConstant.PROT_SUBMIT_EXCHANGE_CARGO_20029_FAIL);
//			msg.obj = resBean;
//			mHandler.sendMessage(msg);
			mHandler.sendEmptyMessage(RequestConstant.PROT_SUBMIT_EXCHANGE_CARGO_20029_FAIL);
		}
		
	}

	private Prot20029ReqBean getProt20029ReqBean() {
		Prot20029ReqBean reqBean = new Prot20029ReqBean();
		reqBean.setFrom(cargoBean.getFrom_dealer_id());
		reqBean.setTo(cargoBean.getTo_dealer_id());
		reqBean.setArea_code(cargoBean.getTo_dealer_sale_area());
		reqBean.setFlag(cargoBean.getOption_mode());
		if (cargoBean.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
			reqBean.setSingle(labels);
		} else {
			reqBean.setBatcher(batchBean);
		}
		LogUtil.d(TConfig.Echo, "上传的数据：" + reqBean.toString());
		return reqBean;
	}


}
