package cn.net.caoyao.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.caoyao.cpzshandset.bean.TruckConsigeeBean;
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.TruckConsigneeBll;
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.handset.bean.ConsigneeBean;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20014ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20013ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20014ReqTAConsignee;

public class YthLoginReqTruckConsigneeRunable implements Runnable {
	private Handler mHandler;
	private long deveicedId; 
	private TruckConsigneeBll consigneeBll;
	private TruckInvoiceBll truckInvoiceBll;
	private String account;
	public YthLoginReqTruckConsigneeRunable(Handler mHandler, long deveicedId,TruckConsigneeBll consigneeBll,TruckInvoiceBll truckInvoiceBll,String account) {
		this.mHandler = mHandler;
		this.deveicedId = deveicedId;
		this.consigneeBll = consigneeBll;
		this.truckInvoiceBll = truckInvoiceBll;
		this.account = account;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, String.valueOf(deveicedId));
		try {
			create.createLoginCpzsHandset(deveicedId);
			if (create.isResult()) {
				Prot20014ReqTAConsignee consigneeReq = new Prot20014ReqTAConsignee();
				consigneeReq.dealConsigneeList(create,new Prot20014ReqBean(account));
				Prot20013ResBean resBean = consigneeReq.getConsignees();
				if (resBean.getResult() == 0) {
					//0：请求成功     1：遇到错误信息，返回错误码
					if (resBean.getCustomer() != null && resBean.getCustomer().size()>0) {
						insertConsigneeInfo(resBean.getCustomer());
					}else {
						mHandler.sendEmptyMessage(RequestConstant.PROT_REQ_CONSIGEE_20013_NO_DATA);
					}
				} else {
					Message failMsg = mHandler.obtainMessage();
					failMsg.what = RequestConstant.PROT_REQ_CONSIGEE_20013_FAIL;
					failMsg.obj = resBean;
					mHandler.sendMessage(failMsg);
				}
				
			} 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 insertConsigneeInfo(List<ConsigneeBean> cs) {
		List<TruckConsigeeBean> consignees = new ArrayList<TruckConsigeeBean>();
		for (ConsigneeBean c : cs) {
			TruckConsigeeBean tcs = new TruckConsigeeBean();
			tcs.setId(c.getId());
			tcs.setConsigneeName(c.getConsignee());
			tcs.setCreateDate(c.getCreate_time());
			tcs.setOriginType(1);
			consignees.add(tcs);
		}
		consigneeBll.deleteAllTruckConsignee();
		consigneeBll.insertConsigneeList(consignees);
		
		List<TruckInvoiceBean> truckInvoiceBeans = truckInvoiceBll.queryAllTruckInvoices();
		for (TruckInvoiceBean tb : truckInvoiceBeans) {
			consigneeBll.insertNativeConsignee(getNativeConsignee(tb));
		}
		
		mHandler.sendEmptyMessage(RequestConstant.PROT_REQ_CONSIGEE_20013_SUCCESS);
	}

	private TruckConsigeeBean getNativeConsignee(TruckInvoiceBean tb) {
		TruckConsigeeBean cBean = new TruckConsigeeBean();
		cBean.setConsigneeName(tb.getConsignee());
		cBean.setOriginType(0);
		return cBean;
	}

}
