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

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

import android.os.Handler;
import android.os.Message;
import cn.net.caoyao.cpzshandset.bean.InvoiceBean;
import cn.net.caoyao.cpzshandset.bean.OrderFail;
import cn.net.caoyao.cpzshandset.bean.TruckInvoiceBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.caoyao.cpzshandset.service.bll.InvoiceBll;
import cn.net.caoyao.cpzshandset.service.bll.TruckInvoiceBll;
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.Prot20016ReqBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20016SubmitTAInvoice;

public class YthImageSubmitInvoiceRunable implements Runnable {
	private Handler mHandler;
	private long deveicedId; 
	private TruckInvoiceBll truckBll;
	private InvoiceBll invoiceBll;
	private int truckFail = 0;
	private List<OrderFail> orderFails = new ArrayList<OrderFail>();
	private List<TruckInvoiceBean> vialids;
	public YthImageSubmitInvoiceRunable(Handler mHandler, long deveicedId,TruckInvoiceBll truckBll,InvoiceBll invoiceBll,List<TruckInvoiceBean> vialids) {
		this.mHandler = mHandler;
		this.deveicedId = deveicedId;
		this.invoiceBll = invoiceBll;
		this.vialids = vialids;
		this.truckBll = truckBll;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, String.valueOf(deveicedId));
		try {
			create.createLoginCpzsHandset(deveicedId);
			if (create.isResult()) {
				submitTruckInvoice(create);
				handlerToUI();
			} else {
				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 submitTruckInvoice(SocketCreate create) throws IOException{
		Prot20016SubmitTAInvoice submitTAInvoice = new Prot20016SubmitTAInvoice();
		for (TruckInvoiceBean truckInvoice : vialids) {
			submitTAInvoice.submitInvoice(create, getTruckReqBean(truckInvoice));
			ProtStatusResBean resBean = submitTAInvoice.getResponse();
			
			if (resBean.getResult() == 0) {
				//请求成功时,删除当前订单
				truckBll.deleteTruckInvoiceById(truckInvoice);
			} else {
				truckBll.updateTruckSubmitStatus(Config.SUBMIT_STATUS_SUBMIT_FAIL, resBean.getErrcode(), truckInvoice.getId());
				truckFail ++;//记录当前货车发货订单上传失败数
				orderFails.add(new OrderFail(truckInvoice.getId(), 2,resBean.getErrcode(), resBean.getResult()));
				
			}
		}
	}

	private void handlerToUI() {
		if ( truckFail == 0) {
			mHandler.sendEmptyMessage(RequestConstant.PROT_SUBMIT_INVOICE_10051_SUCCESS);
		} else {
			Message failMsg = mHandler.obtainMessage();
			failMsg.arg2 = truckFail;
			failMsg.obj = orderFails;
			failMsg.what = RequestConstant.PROT_SUBMIT_INVOICE_10051_FAIL;
			mHandler.sendMessage(failMsg);
		}
	}


	private Prot20016ReqBean getTruckReqBean(TruckInvoiceBean truckInvoice){
		Prot20016ReqBean reqBean = new Prot20016ReqBean();
		reqBean.setTruck_number(truckInvoice.getTruckno());
		reqBean.setRegion_id(truckInvoice.getTerminalStationCode());
		reqBean.setAddress(truckInvoice.getDetailAddress());
		reqBean.setConsignee(truckInvoice.getConsignee());
		reqBean.setTeam(truckInvoice.getGruopInfo());
		reqBean.setUpload_time(truckInvoice.getTimestamp());
		reqBean.setLabel_id(getTruckLabelList(truckInvoice));
		return reqBean;
	}

	private List<String> getTruckLabelList(TruckInvoiceBean truckInvoice) {
		List<String> labels = new ArrayList<String>();
		List<InvoiceBean> invoices = invoiceBll.queryTruckInvoices(truckInvoice);
		for (InvoiceBean invoice : invoices) {
			labels.add(invoice.getLabelid());
		}
		return labels;
	}

}
