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

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.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.SingleLabelBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
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.Prot20017CheckLabelStatus;

public class LiqueurCheckLabelThread implements Runnable{
	private Handler mHandler;
	private HandsetOperation option_type;
	private int option_id;
	private SingleLabelBll labelBll;
	private long deviceId;
	private List<String> labels;
	public LiqueurCheckLabelThread(Handler mHandler,long deviceId,HandsetOperation option_type,int option_id,List<String> labels,SingleLabelBll labelBll) {
		this.deviceId = deviceId;
		this.mHandler = mHandler;
		this.option_type = option_type;
		this.option_id = option_id;
		this.labelBll = labelBll;
		this.labels = labels;
		LogUtil.i(TConfig.Echo,"option_type:" + option_type +"  option_id:" + option_id);
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
//			LogUtil.d(TConfig.Echo, "登录");
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				checkLabel(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 checkLabel(SocketCreate create) throws IOException{
		Prot20017CheckLabelStatus prot20017 = new Prot20017CheckLabelStatus();
		prot20017.checkLabel(create, getProt20017ReqBean());
//		LogUtil.d(TConfig.Echo, "发送的数据：" + getProt20017ReqBean().toString());
//		
//		LogUtil.d(TConfig.Echo, "返回的结果："+ prot20017.getResBean().toString());
		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.getOption(), option_id, InternalOptionConstant.SINGLE_LABEL_STATUS_INVALID);
			}
			
			List<Prot20017LabelBean> repeat = prot20017.getResBean().getLabel_double_active();
			if (repeat != null) {
				List<String> repeatLabel = new ArrayList<>();
				for (Prot20017LabelBean prot20017LabelBean : repeat) {
					repeatLabel.add(prot20017LabelBean.getLabel_id());
				}
				labelBll.updateLableStatusById(repeatLabel, option_type.getOption(), option_id, InternalOptionConstant.SINGLE_LABEL_STATUS_REPEAT_ACTIVATE);
			}
			
			List<Prot20017LabelBean> noActivate = prot20017.getResBean().getLabel_not_active();
			if (noActivate != null && option_type.getOption() != HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption()) {
				List<String> noActivateLabel = new ArrayList<>();
				for (Prot20017LabelBean prot20017LabelBean : noActivate) {
					noActivateLabel.add(prot20017LabelBean.getLabel_id());
				}
				labelBll.updateLableStatusById(noActivateLabel, option_type.getOption(), option_id, InternalOptionConstant.SINGLE_LABEL_STATUS_NO_ACTIVATE);
			}
			
			List<Prot20017LabelBean> not_package = prot20017.getResBean().getLabel_not_package();
			if (not_package != null) {
				List<String> notPackageLabel = new ArrayList<>();
				for (Prot20017LabelBean prot20017LabelBean : not_package) {
					notPackageLabel.add(prot20017LabelBean.getLabel_id());
				}
				labelBll.updateLableStatusById(notPackageLabel, option_type.getOption(), option_id, InternalOptionConstant.SINGLE_LABEL_STATUS_NOT_PACKAGE_LABEL);
			}
			
			List<Prot20017LabelBean> label_stack_out = prot20017.getResBean().getLabel_stock_out();
			if (label_stack_out != null) {
				List<String> stackOutLabel = new ArrayList<>();
				for (Prot20017LabelBean prot20017LabelBean : label_stack_out) {
					stackOutLabel.add(prot20017LabelBean.getLabel_id());
				}
				labelBll.updateLableStatusById(stackOutLabel, option_type.getOption(), option_id, InternalOptionConstant.SINGLE_LABEL_STATUS_STACK_OUT_REPEAT);
			}
			
			mHandler.sendEmptyMessage(RequestConstant.PROT_REQ_CHECK_LABEL_SUCCESS);
		} else {
			mHandler.sendEmptyMessage(RequestConstant.PROT_REQ_CHECK_LABEL_FAIL);
		}
		
	}

	private Prot20017ReqBean getProt20017ReqBean() {
		Prot20017ReqBean reqBean = new Prot20017ReqBean();
		reqBean.setLabels(labels);
		reqBean.setOpt(getOptType());
		return reqBean;
	}

	//操作类型1-出库 2-入库 3-销售 4-返库 6-打包 8-激活 9-套标激活
	private int getOptType() {
		int opt = 0;
		switch (option_type) {
		case OPERATION_ACTIVATE_CONSTANT:
		case OPERATION_ACTIVATE_WITH_PACK_CONSTANT:
			opt = 8;
			break;
		case OPERATION_ACTIVATE_WITH_RELATION_CONSTANT:
			//套标激活
			opt = 9;
			break;
		case OPERATION_OUT_WAREHOUSE_CONSTANT:
			opt = 10;
			break;
		case OPERATION_IN_WAREHOUSE_CONSTANT:
			opt = 2;
			break;
		case OPERATION_SALE_CONSTANT:
			opt = 10;
			break;
		case OPERATION_BACK_WAREHOUSE_CONSTANT:
			opt = 4;
			break;
		case OPERATION_PACK_CONSTANT:
			opt = 6;
			break;
		}
		return opt;
	}

}
