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 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.GroupInfoBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20034ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20034ReqLiqueurGroupInfo;

public class LiqueurUpdateGroupInfoThread implements Runnable {
	private long deviceId;
	private GroupInfoBll bll;
	private Handler mHandler;

	public LiqueurUpdateGroupInfoThread(Handler mHandler,long deviceId,GroupInfoBll 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()) {
				requestGroupInfo(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 requestGroupInfo(SocketCreate create) throws IOException{
		Prot20034ReqLiqueurGroupInfo reqGroup = new Prot20034ReqLiqueurGroupInfo();
		reqGroup.reqGroupInfo(create);
		Prot20034ResBean resBean = reqGroup.getResBean();
		if (resBean == null) {
			resBean = new Prot20034ResBean();
			resBean.setResult(1);
			resBean.setErrcode(0);
			Message msg = mHandler.obtainMessage();
			msg.what = RequestConstant.REQUEST_GROUP_INFO_FAIL;
			msg.obj = resBean;
			mHandler.sendMessage(msg);
			return;
		}
		if (resBean.getResult() == 1) {
			//请求产品列表失败
			Message msg = mHandler.obtainMessage();
			msg.what = RequestConstant.REQUEST_GROUP_INFO_FAIL;
			msg.obj = resBean;
			mHandler.sendMessage(msg);
			return;
		}
		if (resBean.getList() == null) {
			//产品列表为空
			Message msg = mHandler.obtainMessage();
			msg.what = RequestConstant.REQUEST_PRODUCT_LIST_IS_EMPTY;
			msg.obj = "班组信息为空，请确认后台是否添加过班组数据";
			msg.arg1 = 0;
			mHandler.sendMessage(msg);
			return;
		}
//		LogUtil.e(TConfig.Echo, "请求班组信息成功：" + resBean.toString());
		bll.deleteAllGroupInfo();
		bll.insertGroupInfoList(resBean.getList());
		mHandler.sendEmptyMessage(RequestConstant.REQUEST_GROUP_INFO_SUCCESS);
			
	}


}
