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

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

import android.os.Handler;
import android.os.Message;
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.caoyao.cpzshandset.service.bll.DealerBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.res.LiqueurDealerBean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20037ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20037ReqLiqueurDealerInfo;

public class LiqueurUpdateDealerListThread implements Runnable {
	private long deviceId;
	private DealerBll bll;
	private Handler mHandler;

	public LiqueurUpdateDealerListThread(Handler mHandler,long deviceId,DealerBll bll) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		this.bll = bll;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			LogUtil.d(TConfig.Echo, "登录");
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				requestDealerList(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 requestDealerList(SocketCreate create) throws IOException{
		
		Prot20037ReqLiqueurDealerInfo dealerReq = new Prot20037ReqLiqueurDealerInfo();
		dealerReq.reqDealerInfo(create);
		Prot20037ResBean resBean = dealerReq.getResBean();
		if (resBean.getResult() == 0) {
			//0：请求成功     1：遇到错误信息，返回错误码
			if (resBean.getList() != null && resBean.getList().size()>0) {
				insertDealerInfo(resBean.getList());
			}else {
				mHandler.sendEmptyMessage(RequestConstant.PROT_REQ_DEALERINFO_20024_NO_DATA);
			}
		} else {
			Message failMsg = mHandler.obtainMessage();
			failMsg.what = RequestConstant.PROT_REQ_DEALERINFO_20024_FAIL;
			failMsg.obj = resBean;
			mHandler.sendMessage(failMsg);
		}
			
	}
	
	private void insertDealerInfo(List<LiqueurDealerBean> dealers) {
		bll.deleteAllDealer();
		bll.insertLiqueurDealerList(dealers);
		mHandler.sendEmptyMessage(RequestConstant.PROT_REQ_DEALERINFO_20024_SUCCESS);
	}


}
