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

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.SingleLabelOptionBean;
import cn.net.caoyao.cpzshandset.bean.WarehouseBean;
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.SingleLabelBll;
import cn.net.caoyao.cpzshandset.service.bll.UploadStatusBll;
import cn.net.caoyao.cpzshandset.service.bll.WarehouseBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20018ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20018ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20018LabelOption;

/**
 * @author 作者 李扬:
 * @date 创建时间：2017-4-27 下午4:17:14
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class ChinatexSubmitAllWarehouseThread implements Runnable {
	private long deviceId;
	private WarehouseBll bll;
	private SingleLabelBll labelBll;
	// private UploadStatusBll uploadStatusBll;
	private Handler mHandler;
	private HandsetOperation option_type;
	private List<WarehouseBean> warehouseBeans;

	public ChinatexSubmitAllWarehouseThread(Handler mHandler, long deviceId,
			WarehouseBll bll, SingleLabelBll labelBll,
			UploadStatusBll uploadStatusBll, HandsetOperation option_type,
			List<WarehouseBean> warehouseBeans) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		this.bll = bll;
		this.labelBll = labelBll;
		// this.uploadStatusBll = uploadStatusBll;
		this.option_type = option_type;
		this.warehouseBeans = warehouseBeans;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			LogUtil.d(TConfig.Echo, "登录");
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				submitWarehouseInfo(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 submitWarehouseInfo(SocketCreate create) throws IOException {
		for (WarehouseBean b : warehouseBeans) {
			int count = labelBll.queryLabelCountById(option_type.getOption(),
					b.getId());
			if (count > 0) {
				Prot20018LabelOption p218 = new Prot20018LabelOption();
				p218.reqLabelOption(create, getProt20018ReqBean(b));

				Prot20018ResBean resBean = p218.getResBean();
				if (resBean != null) {
					if (resBean.getErrcode() == 0) {
						if (resBean.getiResult() == 0) {
							// 成功
							// uploadStatusBll.insertUploadStatus(getUploadBean(b));更改状态表
							bll.deleteWarehouse(b);// 删除本单信息
						} else {
							// 失败
							// bll.updateWarehouseErrorStatus(status,
							// error_code,
							// id);

							bll.updateWarehouseErrorStatus(
									Config.SUBMIT_STATUS_SUBMIT_FAIL,
									resBean.getiResult(), b.getId());
						}
					} else {

						bll.updateWarehouseErrorStatus(
								Config.SUBMIT_STATUS_SUBMIT_FAIL,
								resBean.getErrcode(), b.getId());
					}

				}

			}
		}

		mHandler.sendEmptyMessage(RequestConstant.SUBMIT_ALL_WAREHOUSE_COMPLETE);
	}

	private Prot20018ReqBean getProt20018ReqBean(WarehouseBean wb) {
		int opt = 0;
		int planNumber = 0;
		switch (option_type) {
		case OPERATION_OUT_WAREHOUSE_CONSTANT:
			opt = 1;// 1 是出库，2是返库
			break;
		case OPERATION_BACK_WAREHOUSE_CONSTANT:
			opt = 2;
			break;
		}
		if (!"".equals(wb.getPlanNumber()) && null != wb.getPlanNumber()) {
			planNumber = Integer.parseInt(wb.getPlanNumber());
		}
		return new Prot20018ReqBean(getLabels(wb), wb.getCreateDate(), opt,
				wb.getOutWarehouseType(), planNumber, wb.getSale_area_code(),
				wb.getDescribe());
	}

	private List<String> getLabels(WarehouseBean wBean) {
		List<String> singles = new ArrayList<>();
		List<SingleLabelOptionBean> singleOptions = labelBll.queryAllLabelById(
				option_type.getOption(), wBean.getId());
		for (SingleLabelOptionBean sLBean : singleOptions) {
			singles.add(sLBean.getLabel_id());
		}
		return singles;
	}

}
