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

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.bean.UnpackBean;
import cn.net.caoyao.cpzshandset.bean.UploadBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
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.UnPackBll;
import cn.net.caoyao.cpzshandset.service.bll.UploadStatusBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.bean.res.ProtStatusResBean;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20004ReqBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20004Unpack;

public class CommonSubmitUnpackThread implements Runnable {
	private long deviceId;
	private UnPackBll bll;
	private UploadStatusBll uploadStatusBll;
	private Handler mHandler;
	private int option_type = HandsetOperation.OPERATION_UNPACK_CONSTANT.getOption();

	public CommonSubmitUnpackThread(Handler mHandler,long deviceId,UnPackBll bll,UploadStatusBll uploadStatusBll) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		this.bll = bll;
		this.uploadStatusBll = uploadStatusBll;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			LogUtil.d(TConfig.Echo, "登录");
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				submitUnpackInfo(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 submitUnpackInfo(SocketCreate create) throws IOException,SocketTimeoutException{
		Prot20004Unpack unPack = new Prot20004Unpack();
		List<UnpackBean> beans = bll.queryAllUnPackListASC();
		for (UnpackBean b : beans) {
			unPack.submitUnpackInfo(create, new Prot20004ReqBean(b.getLabel_id()));
			ProtStatusResBean resBean = unPack.getResBean();
			if (resBean != null) {
				if (resBean.getResult() == 0) {
					//成功
					uploadStatusBll.insertUploadStatus(getUploadBean(b));
					bll.deleteUnpackById(b);
				} else {
					//失败
					bll.updateUnpackStatusById(Config.SUBMIT_STATUS_SUBMIT_FAIL, resBean.getErrcode(), b.getId());
				}
			} else {
				bll.updateUnpackStatusById(Config.SUBMIT_STATUS_SUBMIT_FAIL, 0, b.getId());
			}
			
		}
		mHandler.sendEmptyMessage(RequestConstant.SUBMIT_ALL_UNPACK_COMPLETE);
		
	}

	private UploadBean getUploadBean(UnpackBean b) {
		UploadBean ub = new UploadBean();
		ub.setLabel_count(1);
		ub.setOption_type(option_type);
		return ub;
	}


}
