package cn.net.communal.cpzshandset.service.thread.impl.ythLogin;

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.communal.cpzshandset.bean.InvoiceBean;
import cn.net.communal.cpzshandset.bean.OrderFail;
import cn.net.communal.cpzshandset.bean.TruckInvoiceBean;
import cn.net.communal.cpzshandset.common.config.Config;
import cn.net.communal.cpzshandset.common.config.RequestConstant;
import cn.net.communal.cpzshandset.common.config.TConfig;
import cn.net.communal.cpzshandset.common.util.LogUtil;
import cn.net.communal.cpzshandset.service.bll.InvoiceBll;
import cn.net.communal.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.bean.req.Prot20025ReqBean;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20071ReqBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20025SubmitHandsetParameter;
import cn.net.cpzslibs.prot.handset.newProt.Prot20071SubmitTAInvoice;

public class YthLoginSubmitInvoiceRunable 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;
	private Prot20025ReqBean reqBean;
	public YthLoginSubmitInvoiceRunable(Handler mHandler, long deveicedId,TruckInvoiceBll truckBll,InvoiceBll invoiceBll,List<TruckInvoiceBean> vialids,Prot20025ReqBean reqBean) {
		this.mHandler = mHandler;
		this.deveicedId = deveicedId;
		this.invoiceBll = invoiceBll;
		this.vialids = vialids;
		this.truckBll = truckBll;
		this.reqBean = reqBean;
//		LogUtil.d(TConfig.Echo, "上传的JSON数据：" + reqBean.toString());
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, String.valueOf(deveicedId));
		try {
			create.createLoginCpzsHandset(deveicedId);
			if (create.isResult()) {
				submitHandsetParameter(create);
				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 submitHandsetParameter(SocketCreate create) throws IOException{
		LogUtil.d(TConfig.Echo, "reqBean:" +reqBean.toString());
		Prot20025SubmitHandsetParameter submitHandsetParameter = new Prot20025SubmitHandsetParameter();
		submitHandsetParameter.submitHandsetParameter(create, reqBean);
	}

	private void submitTruckInvoice(SocketCreate create) throws IOException{
		Prot20071SubmitTAInvoice submitTAInvoice = new Prot20071SubmitTAInvoice();
		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 Prot20071ReqBean getTruckReqBean(TruckInvoiceBean truckInvoice){
		Prot20071ReqBean reqBean = new Prot20071ReqBean();
		reqBean.setTruck_number(truckInvoice.getTruckno());
		reqBean.setAccount(truckInvoice.getResponsiblePersonId());
		reqBean.setRegion_id(truckInvoice.getTerminalStationCode());
		reqBean.setAddress(truckInvoice.getDetailAddress());
		reqBean.setConsignee(truckInvoice.getConsignee());
		reqBean.setTeam(truckInvoice.getGruopInfo());
		reqBean.setUpload_time(truckInvoice.getTimestamp());
		reqBean.setVid(getTruckPalletList(truckInvoice));
		reqBean.setLabel_id(getTruckLabelList(truckInvoice));
		reqBean.setOpt(truckInvoice.getLabelMode());
//		LogUtil.i(TConfig.Echo, "truckNo:" + truckInvoice.getTruckno() +"\nlabelMode:"+truckInvoice.getLabelMode());
		return reqBean;
	}

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

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

}
