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

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.FarmersBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20001ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20045ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20045DownFarmers;

public class CommonFarmersUpdateProductListThread implements Runnable {
	private long deviceId;
	private FarmersBll bll;
	private Handler mHandler;

	public CommonFarmersUpdateProductListThread(Handler mHandler,long deviceId,FarmersBll 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()) {
				requestProductList(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 requestProductList(SocketCreate create) throws IOException{
		
		Prot20045DownFarmers p45 = new Prot20045DownFarmers();
		p45.reqProductList(create, new Prot20001ReqBean(String.valueOf(deviceId)));
		Prot20045ResBean resBean = p45.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_FARMERS_PRODUCT_LIST_FAIL;
			msg.obj = resBean;
			mHandler.sendMessage(msg);
			return;
		}
		if (resBean.getList() == null) {
			//产品列表为空
			bll.deleteAllFarmers();
			Message msg = mHandler.obtainMessage();
			msg.what = RequestConstant.REQUEST_PRODUCT_LIST_IS_EMPTY;
			msg.obj = "产品列表为空";
			msg.arg1 = 0;
			mHandler.sendMessage(msg);
			return;
		}
		
		bll.deleteAllFarmers();
		bll.insertFarmersList(resBean.getList());
		mHandler.sendEmptyMessage(RequestConstant.REQUEST_PRODUCT_FARMERS_LIST_SUCCESS);
		
	}


}

