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

import java.io.IOException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

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.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20063ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20063ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20063CheckYunCarNumber;
import android.os.Handler;
import android.os.Message;

public class GetCarNo implements Runnable {
	private long deviceId;
	private Handler mHandler;
	private String carNo ;
	public GetCarNo(Handler mHandler,String carNo ,long deviceId) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		this.carNo = carNo; 
		
	}
	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			LogUtil.d(TConfig.Echo, "登录");
			
			create.createLoginCpzsHandset(deviceId);  
			
			if (create.isResult()) {
				requestCarNoList(create);
			} else {
				Message msg = new Message();
				msg.what = RequestConstant.REQUEST_PROT01_LOGIN_FAIL;
				msg.arg1 = create.getiErrCode();
				msg.obj = create.getErrMsg();
				mHandler.sendMessage(msg);
			}
			
		} catch (BindException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (ConnectException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (IOException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		}finally {
			new Prot02Logout().dealLogout(create);
		}
		
	}
	
	private void requestCarNoList(SocketCreate create) throws IOException{
		Prot20063CheckYunCarNumber p63 = new Prot20063CheckYunCarNumber() ;
		p63.dealCarNo(create, new Prot20063ReqBean(carNo));
		Prot20063ResBean resBean  = p63.getResBean();
		if (resBean == null) {
			Message msg = mHandler.obtainMessage();
			msg.what = RequestConstant.REQUEST_PRODUCT_LIST_IS_EMPTY;
			msg.obj = "未请求到数据";
			msg.arg1 = 0;
			mHandler.sendMessage(msg);
			return;
		}
		if (resBean.getResult() == 1) {
			//请求失败   
			Message msg = mHandler.obtainMessage();
			msg.what = RequestConstant.REQUEST_PRODUCT_LIST_IS_EMPTY;
			msg.obj = resBean;
			mHandler.sendMessage(msg);
			return;
		}
		
		Message msg = mHandler.obtainMessage();
		msg.what = RequestConstant.REQUEST_PRODUCT_LIST_SUCCESS;
		msg.obj = resBean;
		mHandler.sendMessage(msg);
		
		//mHandler.sendEmptyMessage(RequestConstant.REQUEST_PRODUCT_LIST_SUCCESS); 
		
	}

}
