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.TruckAddressBean;
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.common.util.DateUtil;
import cn.net.caoyao.cpzshandset.service.bll.TruckAddressBll;
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.req.Prot20014ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.AddressTABean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20015ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20015ReqTAddress;

public class YthLoginReqTruckStationRunable implements Runnable {
	private Handler mHandler;
	private long deveicedId; 
	private TruckAddressBll addressBll;
	private TruckInvoiceBll truckInvoiceBll;
	private String account;
	public YthLoginReqTruckStationRunable(Handler mHandler, long deveicedId,TruckAddressBll addressBll,TruckInvoiceBll truckInvoiceBll,String account) {
		this.mHandler = mHandler;
		this.deveicedId = deveicedId;
		this.addressBll = addressBll;
		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()) {
				Prot20015ReqTAddress stationReq = new Prot20015ReqTAddress();
				stationReq.dealAddressList(create,new Prot20014ReqBean(account));
				Prot20015ResBean resBean = stationReq.getAddress();
				if (resBean.getResult() == 0) {
					//0：请求成功     1：遇到错误信息，返回错误码
					if (resBean.getList() != null && resBean.getList().size()>0) {
						insertStationInfo(resBean.getList());
					}else {
						mHandler.sendEmptyMessage(RequestConstant.PROT_REQ_ADDRESS_20015_NO_DATA);
					}
				} else {
					Message failMsg = mHandler.obtainMessage();
					failMsg.what = RequestConstant.PROT_REQ_ADDRESS_20015_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 insertStationInfo(List<AddressTABean> ac) {
		List<TruckAddressBean> stations = new ArrayList<TruckAddressBean>();
		for (AddressTABean ps : ac) {
			TruckAddressBean sm = new TruckAddressBean();
			sm.setId(ps.getId());
			sm.setCityCode(ps.getRegion_id());
			sm.setDetailAddress(ps.getAddress());
			sm.setOriginType(1);
			sm.setCreateDate(ps.getCreate_time());
			sm.setUpdateDate(ps.getUpdate_time());
			stations.add(sm);
		}
		addressBll.deleteAllTruckTruckAddress();
		addressBll.insertTruckAddressList(stations);
		
		List<TruckInvoiceBean> invoiceBeans = truckInvoiceBll.queryAllTruckInvoices();
		for (TruckInvoiceBean tB : invoiceBeans) {
			addressBll.insertNativeTruckAddress(getNativeAddress(tB));
		}
		
		mHandler.sendEmptyMessage(RequestConstant.PROT_REQ_ADDRESS_20015_SUCCESS);
	}

	private TruckAddressBean getNativeAddress(TruckInvoiceBean tB) {
		TruckAddressBean b = new TruckAddressBean();
		b.setDetailAddress(tB.getDetailAddress());
		b.setCityCode(tB.getTerminalStationCode());
		b.setOriginType(0);
		b.setUpdateDate(DateUtil.getDateTimeFormat(System.currentTimeMillis()));
		return b;
	}

}
