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

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

import android.os.Handler;
import android.os.Message;
import cn.net.caoyao.cpzshandset.bean.ActivateBean;
import cn.net.caoyao.cpzshandset.bean.SingleLabelOptionBean;
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.ActivateBll;
import cn.net.caoyao.cpzshandset.service.bll.SingleLabelBll;
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.Prot10079ReqBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20007Activate;

public class YthImageSubmitAllActivateThread implements Runnable {
	private long deviceId;
	private ActivateBll bll;
	private SingleLabelBll labelBll;
	private List<ActivateBean> activateBeans;
	private Handler mHandler;

	public YthImageSubmitAllActivateThread(Handler mHandler,long deviceId,ActivateBll bll,SingleLabelBll labelBll,List<ActivateBean> activateBeans) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		this.bll = bll;
		this.activateBeans = activateBeans;
		this.labelBll = labelBll;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			LogUtil.d(TConfig.Echo, "登录");
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				submitActivateInfo(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 submitActivateInfo(SocketCreate create) throws IOException,SocketTimeoutException{
		
		for (ActivateBean activateBean : activateBeans) {
//			Prot10079Activate activate = new Prot10079Activate();
			Prot20007Activate activate = new Prot20007Activate();
			activate.submitActivateInfo(create, getReqBean(activateBean));
			ProtStatusResBean resBean = activate.getResBean();
			
			if (resBean.getResult() == 0) {
				bll.delActivateById(activateBean);
//				bll.delActivateById(activateBean.getId());
//				labelBll.deleteLabelById(HansetOperationConstant.OPERATION_ACTIVATE_CONSTANT, activateBean.getId());
			} else {
				bll.updateActivateErrorById(resBean.getErrcode(), activateBean.getId());
			}
		}
		
		mHandler.sendEmptyMessage(RequestConstant.SUBMIT_ALL_ACTIVATE_COMPLETE);
		/*if (resBean == null) {
			mHandler.sendEmptyMessage(RequestConstant.SUBMIT_ACTIVATE_WITHOUT_RES);
			return;
		}
		
		if (resBean.getResult() == 0) {
			bll.delActivateById(activateBean.getId());
			labelBll.deleteLabelById(HansetOperationConstant.OPERATION_ACTIVATE_CONSTANT, activateBean.getId());
		} else {
			bll.updateActivateErrorById(resBean.getErrcode(), activateBean.getId());
		}
		Message msg = mHandler.obtainMessage(RequestConstant.SUBMIT_ACTIVATE_WITH_RES);
		msg.obj = resBean;
		msg.what = RequestConstant.SUBMIT_ACTIVATE_WITH_RES;
		mHandler.sendMessage(msg);*/
		
	}
	private Prot10079ReqBean getReqBean(ActivateBean currentBean) {
		Prot10079ReqBean reqBean = new Prot10079ReqBean();
		reqBean.setProduct_id(currentBean.getProduct_id());
		reqBean.setProducer(currentBean.getProducer());
		reqBean.setProduce_date(currentBean.getProduct_date());
		reqBean.setProduce_batch(currentBean.getProduct_batch_number());
		reqBean.setGroup_number(currentBean.getGroup_number());
		reqBean.setFactory_code(currentBean.getFactory_code());
		reqBean.setType(Config.SINGLE_OPTION_LABEL_TYPE);
		reqBean.setSingle(getLabels(labelBll.queryAllLabelById(HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption(), currentBean.getId())));
		return reqBean;
	}

	private List<String> getLabels(List<SingleLabelOptionBean> singleOptions) {
		List<String> singles = new ArrayList<>();
		for (SingleLabelOptionBean sLBean : singleOptions) {
			singles.add(sLBean.getLabel_id());
		}
		return singles;
	}


}
