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.ArrayList;
import java.util.List;

import android.os.Handler;
import android.os.Message;
import cn.net.caoyao.cpzshandset.bean.PackBean;
import cn.net.caoyao.cpzshandset.bean.PackPackageInfoBean;
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.InternalOptionConstant;
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.PackPackageBll;
import cn.net.caoyao.cpzshandset.service.bll.PackSubLabelBll;
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.Prot20003ReqBean;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20017ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20017LabelBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20003Pack;
import cn.net.cpzslibs.prot.handset.newProt.Prot20017CheckLabelStatus;

public class CommonSubmitAllPackThread implements Runnable {
	private long deviceId;
	private PackSubLabelBll labelBll;
	private PackPackageBll packageBll;
	private UploadStatusBll uploadStatusBll;
	private Handler mHandler;
	private int option_type = HandsetOperation.OPERATION_PACK_CONSTANT.getOption();
	private List<PackBean> packBeans;

	public CommonSubmitAllPackThread(Handler mHandler,long deviceId,PackPackageBll packageBll,PackSubLabelBll labelBll,UploadStatusBll uploadStatusBll,List<PackBean> packBeans) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		this.packageBll = packageBll;
		this.labelBll = labelBll;
		this.uploadStatusBll = uploadStatusBll;
		this.packBeans = packBeans;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			LogUtil.d(TConfig.Echo, "登录");
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				submitPackInfo(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 submitPackInfo(SocketCreate create) throws IOException{
		for (PackBean packBean : packBeans) {
			List<PackPackageInfoBean> pBs = packageBll.queryAllPackPackageASC(packBean.getId(), option_type);
			Prot20003Pack pack = new Prot20003Pack();
			for (PackPackageInfoBean pB : pBs) {
				int count = labelBll.queryLabelCountByPackageId(pB.getId());
				if (count > 0) {
					pack.submitPackInfo(create, getProt20003ReqBean(packBean,pB));
					ProtStatusResBean resBean = pack.getResBean();
					if (resBean != null ) {
						if (resBean.getResult() == 0) {
							//成功
							uploadStatusBll.insertUploadStatus(getUploadBean(pB));
							packageBll.deletePackPackageById(pB.getId());
						} else {
							//失败
							packageBll.updatePackPackageSubmitStatus(pB.getId(), Config.SUBMIT_STATUS_SUBMIT_FAIL, resBean.getErrcode());
							checkLabel(create,packBean,pB);
						}
					}
				}
			}
		}
		
		mHandler.sendEmptyMessage(RequestConstant.SUBMIT_ALL_PACK_COMPLETE);
	}

	
	private void checkLabel(SocketCreate create,PackBean packBean, PackPackageInfoBean pB) throws IOException{
		Prot20017CheckLabelStatus prot20017 = new Prot20017CheckLabelStatus();
		prot20017.checkLabel(create, getProt20017ReqBean(packBean,pB));
		if (prot20017.getResBean() != null && prot20017.getResBean().getResult() == 0) {
			List<Prot20017LabelBean> invilad = prot20017.getResBean().getLabel_invalid();
			if (invilad != null) {
				List<String> inviladLabel = new ArrayList<>();
				for (Prot20017LabelBean prot20017LabelBean : invilad) {
					inviladLabel.add(prot20017LabelBean.getLabel_id());
				}
//				labelBll.updateLableStatusById(inviladLabel, option_type, packBean.getId(), InternalOptionConstant.SINGLE_LABEL_STATUS_INVALID);
				labelBll.updateLabelListStatus(inviladLabel, pB.getId(), packBean.getId(), option_type, InternalOptionConstant.SINGLE_LABEL_STATUS_INVALID);
			}
			
			List<Prot20017LabelBean> noActivate = prot20017.getResBean().getLabel_not_active();
			if (noActivate != null) {
				List<String> noActivateLabel = new ArrayList<>();
				for (Prot20017LabelBean prot20017LabelBean : noActivate) {
					noActivateLabel.add(prot20017LabelBean.getLabel_id());
				}
//				labelBll.updateLableStatusById(noActivateLabel, option_type, packBean.getId(), InternalOptionConstant.SINGLE_LABEL_STATUS_NO_ACTIVATE);
				labelBll.updateLabelListStatus(noActivateLabel, pB.getId(), packBean.getId(), option_type, InternalOptionConstant.SINGLE_LABEL_STATUS_NO_ACTIVATE);
			}
			
			
		}
		
	}

	//操作类型1-出库 2-入库 3-销售 4-返库 6-打包 8-激活
	private Prot20017ReqBean getProt20017ReqBean(PackBean packBean,PackPackageInfoBean pB) {
		Prot20017ReqBean reqBean = new Prot20017ReqBean();
		reqBean.setLabels(getLabels(packBean,pB));
		reqBean.setOpt(6);
		return reqBean;
	}
	private Prot20003ReqBean getProt20003ReqBean(PackBean packBean,PackPackageInfoBean pB) {
		Prot20003ReqBean reqBean = new Prot20003ReqBean();
		reqBean.setPackage_id(String.valueOf(pB.getPackage_label_id()));
		reqBean.setSingle(getLabels(packBean,pB));
		return reqBean;
	}

	private UploadBean getUploadBean(PackPackageInfoBean pB) {
		UploadBean b = new UploadBean();
		b.setLabel_count(labelBll.queryLabelCountByPackageId(pB.getId()));
		b.setOption_type(option_type);
		return b;
	}


	private List<String> getLabels(PackBean packBean,PackPackageInfoBean pB) {
		
		return labelBll.queryAllSubLabelStrByPackageId(pB.getId(), packBean.getId(), option_type);
	}


}
